1 const fs = require("fs")
2 const google_images = require("free-google-images")
5 const furrybot.list_change_command(cmd, list_name, title, status)
6 furrybot.commands[cmd] = {
8 func = function(name, target)
10 if furrybot[list_name][target] == status then
11 furrybot.error_message(name, "Player " .. (status and "already" or "not") .. " " .. title .. ": ", target)
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)
18 furrybot.error_message(name, "You need to specify a player")
24 function furrybot.list_command(cmd, list_name, title)
25 furrybot.commands[cmd] = {
29 for name in pairs(furrybot[list_name]) do
30 table.insert(names, name)
33 furrybot.send("List of " .. title .. ": " .. table.concat(names, ", "), furrybot.colors.system)
38 function furrybot.choose(list, color)
39 return furrybot.colors.random .. list[math.random(#list)] .. color
42 function furrybot.random(min, max, color)
43 return furrybot.colors.random .. math.random(min, max) .. color
46 function furrybot.strrandom(str, seed, ...)
48 local pr = PseudoRandom(seed)
50 v = v + str:byte(i) * pr:next()
52 return PseudoRandom(v):next(...)
55 function furrybot.repeat_string(str, times)
65 const getPing = module.exports.getPing = (msg, ping, allowSelf) => {
66 if (ping && ping.startsWith("<@!") && ping.endsWith(">")) {
67 const id = ping.slice("<@!".length, -">".length)
69 if (!allowSelf && id == msg.author.id) {
70 msg.reply("Please mention a user other than yourself")
74 if (msg.guild.members.cache.get(id))
78 msg.reply("Please mention a user")
81 module.exports.uppercase = str => str.slice(0, 1).toUpperCase() + str.slice(1)
83 module.exports.requestCommand = (help, onRequest, onAccept) => new Object({
85 help: "Request to " + help + " another user",
86 func: (msg, [targetPing], {requests}) => {
87 const target = getPing(msg, targetPing, false)
90 const err = onRequest(msg, target)
96 origin: msg.author.id,
104 module.exports.soloRoleplayCommand = (help, action) => new Object({
106 func: msg => google_images.searchRandom(help)
107 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action}.\n${result.image.url}`))
110 module.exports.interactiveRoleplayCommand = (help, action) => new Object({
112 help: help + " another user",
113 func: (msg, [targetPing]) => {
114 const target = getPing(msg, targetPing, false)
117 google_images.searchRandom(help)
118 .then(result => msg.channel.send(`<@!${msg.author.id}> ${action} <@!${target}>.\n${result.image.url}`))
122 module.exports.storageLoad = name => {
124 return require(`storage/${name}.json`)
128 module.exports.storageSave = (name, data) => fs.writeFileSync(`storage/${name}.json`, JSON.stringify(data))
130 module.exports.choose = (arr, rng = Math) => arr[Math.floor(rng.random() * arr.length)]
132 module.exports.chooseWeighted = (arr, rng = Math) => {
136 arr.forEach((v, k) => {
137 edges[k] = (accum += v[1])
140 const r = Math.floor(rng.random() * accum)
141 return arr.find((_, k) => r < edges[k])[0]