1 const fs = require("fs")
2 const google_images = require("free-google-images")
4 const getPing = module.exports.getPing = (msg, ping, allowSelf) => {
5 if (ping && ping.startsWith("<\\@!") && ping.endsWith(">")) {
6 const id = ping.slice("<\\@!".length, -">".length)
8 if (!allowSelf && id == msg.author.id) {
9 msg.reply("Please mention a user other than yourself")
13 if (msg.guild.members.cache.get(id))
17 msg.reply("Please mention a user")
20 module.exports.uppercase = str => str.slice(0, 1).toUpperCase() + str.slice(1)
22 module.exports.requestCommand = (help, info, onRequest, onAccept) => new Object({
24 help: "Request to " + help,
25 func: (msg, [targetPing], {requests}) => {
26 const target = getPing(msg, targetPing, false)
29 const err = onRequest(msg, target)
34 msg.channel.send(`<@!${target}>: <@!${msg.author.id}> ${info}. Type !accept to accept or !deny to deny.`)
37 origin: msg.author.id,
46 module.exports.soloRoleplayCommand = (help, action) => new Object({
48 func: msg => google_images.searchRandom(help)
49 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
52 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
54 help: help + " another user",
55 func: (msg, [targetPing]) => {
56 const target = getPing(msg, targetPing, false)
59 google_images.searchRandom(help)
60 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
64 module.exports.storageLoad = name => {
66 return require(`./storage/${name}.json`)
70 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
72 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
74 module.exports.chooseWeighted = (arr, rng = Math) => {
78 arr.forEach((v, k) => {
79 edges[k] = (accum += v[1])
82 const r = Math.floor(rng.random() * accum)
83 return arr.find((_, k) => r < edges[k])[0]
86 module.exports.listCommand = (title, list) => new Object({
87 help: "Show list of " + title,
88 func: (msg, _, fb) => msg.reply(`List of ${title}: ${Object.keys(fb[list]).map(entry => "<@!" + entry + ">").join(", ")}`)
91 module.exports.listChangeCommand = (action, list, status) => new Object({
93 func: (msg, [targetPing], fb) => {
94 const target = getPing(msg, targetPing, true)
97 if (fb[list][target] == status) {
98 msg.reply(`<@!${target}> ${status ? "already" : "not"} ${action}.`)
100 fb[list][target] = status
101 module.exports.storageSave(list, fb[list])
102 msg.reply(`Successfully ${action} <@!${target}>.`)