]> git.lizzy.rs Git - furrybot-discord.git/blob - common.js
a098c329037462bac933c837fd14944006649504
[furrybot-discord.git] / common.js
1 const fs = require("fs")
2 const google_images = require("free-google-images")
3
4 /*
5 const furrybot.list_change_command(cmd, list_name, title, status)
6         furrybot.commands[cmd] = {
7                 operator = true,
8                 func = function(name, target)
9                         if target then
10                                 if furrybot[list_name][target] == status then
11                                         furrybot.error_message(name, "Player " .. (status and "already" or "not") .. " " .. title .. ": ", target)
12                                 else
13                                         furrybot[list_name][target] = status
14                                         storage:set_string(list_name, minetest.serialize(furrybot[list_name]))
15                                         furrybot.ping_message(name, "Successfully " .. cmd .. (cmd:sub(#cmd, #cmd) == "e" and "" or "e") .. "d " .. target, furrybot.colors.system)
16                                 end
17                         else
18                                 furrybot.error_message(name, "You need to specify a player")
19                         end
20                 end,
21         }
22 end
23
24 function furrybot.list_command(cmd, list_name, title)
25         furrybot.commands[cmd] = {
26                 func = function()
27                         local names = {}
28
29                         for name in pairs(furrybot[list_name]) do
30                                 table.insert(names, name)
31                         end
32
33                         furrybot.send("List of " .. title .. ": " .. table.concat(names, ", "), furrybot.colors.system)
34                 end,
35         }
36 end
37
38 function furrybot.choose(list, color)
39         return furrybot.colors.random .. list[math.random(#list)] .. color
40 end
41
42 function furrybot.random(min, max, color)
43         return furrybot.colors.random .. math.random(min, max) .. color
44 end
45
46 function furrybot.strrandom(str, seed, ...)
47         local v = 0
48         local pr = PseudoRandom(seed)
49         for i = 1, #str do
50                 v = v + str:byte(i) * pr:next()
51         end
52         return PseudoRandom(v):next(...)
53 end
54
55 function furrybot.repeat_string(str, times)
56         local msg = ""
57         for i = 1, times do
58                 msg = msg .. str
59         end
60         return msg
61 end
62
63 */
64
65 const getPing = module.exports.getPing = (msg, ping, allowSelf) => {
66         if (ping && ping.startsWith("<@!") && ping.endsWith(">")) {
67                 const id = ping.slice("<@!".length, -">".length)
68
69                 if (!allowSelf && id == msg.author.id) {
70                         msg.reply("Please mention a user other than yourself")
71                         return
72                 }
73                 
74                 if (msg.guild.members.cache.get(id))
75                         return id
76         }
77
78         msg.reply("Please mention a user")
79 }
80
81 module.exports.uppercase = str => str.slice(0, 1).toUpperCase() + str.slice(1)
82
83 module.exports.requestCommand = (help, onRequest, onAccept) => new Object({
84         params: "<player>",
85         help: "Request to " + help + " another user",
86         func: (msg, [targetPing], {requests}) => {
87                 const target = getPing(msg, targetPing, false)
88
89                 if (target) {
90                         const err = onRequest(msg, target)
91
92                         if (err)
93                                 msg.reply(err)
94                         else
95                                 requests[target] = {
96                                         origin: msg.author.id,
97                                         func: onAccept,
98                                 }
99                 }
100                         
101         }
102 })
103
104 module.exports.soloRoleplayCommand = (help, action) => new Object({
105         help: help,
106         func: msg => google_images.searchRandom(help)
107                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
108 })
109
110 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
111         params: "<user>",
112         help: help + " another user",
113         func: (msg, [targetPing]) => {
114                 const target = getPing(msg, targetPing, false)
115
116                 if (target)
117                         google_images.searchRandom(help)
118                                 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
119         }
120 })
121
122 module.exports.storageLoad = name => {
123         try {
124                 return require(`storage/${name}.json`)
125         } catch {}
126 }
127
128 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
129
130 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
131
132 module.exports.chooseWeighted = (arr, rng = Math) => {
133         let accum = 0
134         let edges = []
135
136         arr.forEach((v, k) => {
137                 edges[k] = (accum += v[1])
138         })
139
140         const r = Math.floor(rng.random() * accum)
141         return arr.find((_, k) => r < edges[k])[0]
142 }