]> git.lizzy.rs Git - furrybot-discord.git/blob - common.js
4e950e67e06c514032742c3261d9a94bda105689
[furrybot-discord.git] / common.js
1 const fs = require("fs")
2 const google_images = require("free-google-images")
3
4 const getPing = module.exports.getPing = (msg, ping, allowSelf) => {
5         if (ping && ping.startsWith("<@!") && ping.endsWith(">")) {
6                 const id = ping.slice("<@!".length, -">".length)
7
8                 if (!allowSelf && id == msg.author.id) {
9                         msg.reply("Please mention a user other than yourself")
10                         return
11                 }
12                 
13                 if (msg.guild.members.cache.get(id))
14                         return id
15         }
16
17         msg.reply("Please mention a user")
18 }
19
20 module.exports.uppercase = str => str.slice(0, 1).toUpperCase() + str.slice(1)
21
22 module.exports.requestCommand = (help, onRequest, onAccept) => new Object({
23         params: "<player>",
24         help: "Request to " + help + " another user",
25         func: (msg, [targetPing], {requests}) => {
26                 const target = getPing(msg, targetPing, false)
27
28                 if (target) {
29                         const err = onRequest(msg, target)
30
31                         if (err)
32                                 msg.reply(err)
33                         else
34                                 requests[target] = {
35                                         origin: msg.author.id,
36                                         func: onAccept,
37                                 }
38                 }
39                         
40         }
41 })
42
43 module.exports.soloRoleplayCommand = (help, action) => new Object({
44         help: help,
45         func: msg => google_images.searchRandom(help)
46                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
47 })
48
49 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
50         params: "<user>",
51         help: help + " another user",
52         func: (msg, [targetPing]) => {
53                 const target = getPing(msg, targetPing, false)
54
55                 if (target)
56                         google_images.searchRandom(help)
57                                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
58         }
59 })
60
61 module.exports.storageLoad = name => {
62         try {
63                 return require(`./storage/${name}.json`)
64         } catch {}
65 }
66
67 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
68
69 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
70
71 module.exports.chooseWeighted = (arr, rng = Math) => {
72         let accum = 0
73         let edges = []
74
75         arr.forEach((v, k) => {
76                 edges[k] = (accum += v[1])
77         })
78
79         const r = Math.floor(rng.random() * accum)
80         return arr.find((_, k) => r < edges[k])[0]
81 }
82
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(", ")}`)
86 })
87
88 module.exports.listChangeCommand = (action, list, status) => new Object({
89         operator: true,
90         func: (msg, [targetPing], fb) => {
91                 const target = getPing(msg, targetPing, true)
92
93                 if (target) {
94                         if (fb[list][target] == status) {
95                                 msg.reply(`<@!${target}> ${status ? "already" : "not"} ${action}.`)
96                         } else {
97                                 fb[list][target] = status
98                                 module.exports.storageSave(list, fb[list])
99                                 msg.reply(`Successfully ${action} <@!${target}>.`)
100                         }
101                 }
102         }
103 })