]> git.lizzy.rs Git - furrybot-discord.git/blob - common.js
Add bad apple
[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, info, onRequest, onAccept) => new Object({
23         params: "<player>",
24         help: "Request to " + help,
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                                 msg.channel.send(`<@!${target}>: <@!${msg.author.id}> ${info}. Type !accept to accept or !deny to deny.`)
35
36                                 requests[target] = {
37                                         origin: msg.author.id,
38                                         func: onAccept,
39                                 }
40                         }
41                 }
42
43         }
44 })
45
46 module.exports.soloRoleplayCommand = (help, action) => new Object({
47         help: help,
48         func: msg => google_images.searchRandom(help)
49                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
50 })
51
52 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
53         params: "<user>",
54         help: help + " another user",
55         func: (msg, [targetPing]) => {
56                 const target = getPing(msg, targetPing, false)
57
58                 if (target)
59                         google_images.searchRandom(help)
60                                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
61         }
62 })
63
64 module.exports.storageLoad = name => {
65         try {
66                 return require(`./storage/${name}.json`)
67         } catch {}
68 }
69
70 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
71
72 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
73
74 module.exports.chooseWeighted = (arr, rng = Math) => {
75         let accum = 0
76         let edges = []
77
78         arr.forEach((v, k) => {
79                 edges[k] = (accum += v[1])
80         })
81
82         const r = Math.floor(rng.random() * accum)
83         return arr.find((_, k) => r < edges[k])[0]
84 }
85
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(", ")}`)
89 })
90
91 module.exports.listChangeCommand = (action, list, status) => new Object({
92         operator: true,
93         func: (msg, [targetPing], fb) => {
94                 const target = getPing(msg, targetPing, true)
95
96                 if (target) {
97                         if (fb[list][target] == status) {
98                                 msg.reply(`<@!${target}> ${status ? "already" : "not"} ${action}.`)
99                         } else {
100                                 fb[list][target] = status
101                                 module.exports.storageSave(list, fb[list])
102                                 msg.reply(`Successfully ${action} <@!${target}>.`)
103                         }
104                 }
105         }
106 })