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, onRequest, onAccept) => new Object({
24 help: "Request to " + help + " another user",
25 func: (msg, [targetPing], {requests}) => {
26 const target = getPing(msg, targetPing, false)
29 const err = onRequest(msg, target)
35 origin: msg.author.id,
43 module.exports.soloRoleplayCommand = (help, action) => new Object({
45 func: msg => google_images.searchRandom(help)
46 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
49 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
51 help: help + " another user",
52 func: (msg, [targetPing]) => {
53 const target = getPing(msg, targetPing, false)
56 google_images.searchRandom(help)
57 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
61 module.exports.storageLoad = name => {
63 return require(`./storage/${name}.json`)
67 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
69 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
71 module.exports.chooseWeighted = (arr, rng = Math) => {
75 arr.forEach((v, k) => {
76 edges[k] = (accum += v[1])
79 const r = Math.floor(rng.random() * accum)
80 return arr.find((_, k) => r < edges[k])[0]
83 module.exports.listCommand = (title, list) => new Object({
84 help: "Show list of " + title,
85 func: (msg, _, fb) => msg.reply(`List of ${title}: ${Object.keys(fb[list]).map(entry => "<@!" + entry + ">").join(", ")}`)
88 module.exports.listChangeCommand = (action, list, status) => new Object({
90 func: (msg, [targetPing], fb) => {
91 const target = getPing(msg, targetPing, true)
94 if (fb[list][target] == status) {
95 msg.reply(`<@!${target}> ${status ? "already" : "not"} ${action}.`)
97 fb[list][target] = status
98 module.exports.storageSave(list, fb[list])
99 msg.reply(`Successfully ${action} <@!${target}>.`)