diff options
Diffstat (limited to 'common.js')
-rw-r--r-- | common.js | 83 |
1 files changed, 22 insertions, 61 deletions
@@ -1,67 +1,6 @@ const fs = require("fs") const google_images = require("free-google-images") -/* -const furrybot.list_change_command(cmd, list_name, title, status) - furrybot.commands[cmd] = { - operator = true, - func = function(name, target) - if target then - if furrybot[list_name][target] == status then - furrybot.error_message(name, "Player " .. (status and "already" or "not") .. " " .. title .. ": ", target) - else - furrybot[list_name][target] = status - storage:set_string(list_name, minetest.serialize(furrybot[list_name])) - furrybot.ping_message(name, "Successfully " .. cmd .. (cmd:sub(#cmd, #cmd) == "e" and "" or "e") .. "d " .. target, furrybot.colors.system) - end - else - furrybot.error_message(name, "You need to specify a player") - end - end, - } -end - -function furrybot.list_command(cmd, list_name, title) - furrybot.commands[cmd] = { - func = function() - local names = {} - - for name in pairs(furrybot[list_name]) do - table.insert(names, name) - end - - furrybot.send("List of " .. title .. ": " .. table.concat(names, ", "), furrybot.colors.system) - end, - } -end - -function furrybot.choose(list, color) - return furrybot.colors.random .. list[math.random(#list)] .. color -end - -function furrybot.random(min, max, color) - return furrybot.colors.random .. math.random(min, max) .. color -end - -function furrybot.strrandom(str, seed, ...) - local v = 0 - local pr = PseudoRandom(seed) - for i = 1, #str do - v = v + str:byte(i) * pr:next() - end - return PseudoRandom(v):next(...) -end - -function furrybot.repeat_string(str, times) - local msg = "" - for i = 1, times do - msg = msg .. str - end - return msg -end - -*/ - const getPing = module.exports.getPing = (msg, ping, allowSelf) => { if (ping && ping.startsWith("<@!") && ping.endsWith(">")) { const id = ping.slice("<@!".length, -">".length) @@ -140,3 +79,25 @@ module.exports.chooseWeighted = (arr, rng = Math) => { const r = Math.floor(rng.random() * accum) return arr.find((_, k) => r < edges[k])[0] } + +module.exports.listCommand = (title, list) => new Object({ + help: "Show list of " + title, + func: msg => msg.reply(`List of ${title}: ${Object.keys(list).join(", ")}`) +}) + +module.exports.listChangeCommand = (action, list, listName, status) => new Object({ + operator: true, + func: (msg, [targetPing]) => { + const target = getPing(msg, targetPing, true) + + if (target) { + if (list[target] == status) { + msg.reply(`<@!${target}> ${status ? "already" : "not"} ${action}.`) + } else { + list[target] = status + module.exports.storageSave(listName, list) + msg.reply(`Successfully ${action} <@!${target}>.`) + } + } + } +}) |