X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=bot.lua;h=e97d1b84c4ee612e4986214061d7fbd02e098f29;hb=HEAD;hp=bf88852101e363a48b021f6c35a9a118ebd909a3;hpb=7df117a6d4f8f2942aaa819bf58b57497d0243e6;p=furrybot.git diff --git a/bot.lua b/bot.lua index bf88852..e97d1b8 100644 --- a/bot.lua +++ b/bot.lua @@ -1,7 +1,7 @@ furrybot.commands = {} furrybot.requests = {} -furrybot.unsafe_commands = {} +local http, env, storage local C = minetest.get_color_escape_sequence furrybot.colors = { @@ -9,7 +9,7 @@ furrybot.colors = { system = C("#FFFA00"), error = C("#D70029"), detail = C("#FF6683"), - rpg = C("#FFD94E"), + roleplay = C("#FFD94E"), braces = C("#FFFAC0"), info = C("#00FFC3"), fun = C("#A0FF24"), @@ -18,7 +18,6 @@ furrybot.colors = { } -- helper functions - function furrybot.send(msg, color) minetest.send_chat_message("/me " .. furrybot.colors.braces .. "[" .. color .. msg .. furrybot.colors.braces .. "]") end @@ -35,31 +34,45 @@ function furrybot.error_message(player, error, detail) furrybot.ping_message(player, error .. (detail and furrybot.colors.detail .. " '" .. detail .. "'" .. furrybot.colors.error or "") .. ".", furrybot.colors.error) end -function furrybot.recieve(rawmsg) - local msg = minetest.strip_colors(rawmsg) - local nameidx = msg:find("<") - local first_byte = rawmsg:byte(1) - if nameidx and (first_byte == 60 or first_byte == 27) then - local idx = msg:find(">") - local player = msg:sub(nameidx + 1, idx - 1) - local message = msg:sub(idx + 3, #msg) - if message:find("!") == 1 then - local args = message:sub(2, #message):split(" ") - local cmd = table.remove(args, 1) - local func = furrybot.commands[cmd] - if func then - if furrybot.unsafe_commands[cmd] and first_byte == 27 and rawmsg:sub(2, 12) == "(c@#63d269)" and nameidx == 1 then - furrybot.error_message(player, "Sorry, you cannot run this command from discord", cmd) - else - func(player, unpack(args)) - end - else - furrybot.error_message(player, "Invalid command", cmd) +function furrybot.parse_message(player, message, discord) + if message:find("!") == 1 and not furrybot.ignored[player] then + local args = message:sub(2, #message):split(" ") + local cmd = table.remove(args, 1) + local def = furrybot.commands[cmd] + + if def then + if (def.unsafe or def.operator) and discord then + furrybot.error_message(player, "Sorry, you cannot run this command from discord: ", cmd) + elseif def.operator and not furrybot.is_operator(player) then + furrybot.error_message(player, "Sorry, you need to be an operator run this command: ", cmd) + elseif not def.ignore then + def.func(player, unpack(args)) end + else + furrybot.error_message(player, "Invalid command", cmd) end end end +function furrybot.reload() + local func, err = env.loadfile("clientmods/furrybot/bot.lua") + if func then + local old_fb = table.copy(furrybot) + local status, init = pcall(func) + + if status then + status, init = init(http, env, storage) + end + + if not status then + furrybot = old_fb + furrybot.send("Error: " .. furrybot.colors.detail .. init, furrybot.colors.error) + end + else + furrybot.send("Syntax error: " .. furrybot.colors.detail .. err, furrybot.colors.error) + end +end + function furrybot.player_online(name) for _, n in ipairs(minetest.get_player_names()) do if name == n then @@ -89,7 +102,7 @@ function furrybot.random(min, max, color) end function furrybot.http_request(url, name, callback) - furrybot.http.fetch({url = url}, function(res) + http.fetch({url = url}, function(res) if res.succeeded then callback(res.data) else @@ -122,273 +135,169 @@ function furrybot.repeat_string(str, times) return msg end -function furrybot.interactive_rpg_command(action) - return function(name, target) - if furrybot.online_or_error(name, target) then - furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.rpg) - end - end -end - -function furrybot.solo_rpg_command(action) - return function(name) - furrybot.send(name .. " " .. action .. ".", furrybot.colors.rpg) - end -end - -function furrybot.request_command(on_request, on_accept) - return function(name, target) - if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then - furrybot.requests[target] = { - origin = name, - func = on_accept, - } - end - end +function furrybot.uppercase(str) + return str:sub(1, 1):upper() .. str:sub(2, #str) end -function furrybot.get_money(name) - local key = name .. ".money" - if furrybot.storage:contains(key) then - return furrybot.storage:get_int(key) - else - return 100 - end -end - -function furrybot.set_money(name, money) - furrybot.storage:set_int(name .. ".money", money) +function furrybot.interactive_roleplay_command(cmd, action) + furrybot.commands[cmd] = { + params = "", + help = furrybot.uppercase(cmd) .. " another player", + func = function(name, target) + if furrybot.online_or_error(name, target) then + furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.roleplay) + end + end, + } +end + +function furrybot.solo_roleplay_command(cmd, action, help) + furrybot.commands[cmd] = { + help = furrybot.uppercase(cmd), + func = function(name) + furrybot.send(name .. " " .. action .. ".", furrybot.colors.roleplay) + end, + } +end + +function furrybot.request_command(cmd, help, on_request, on_accept, unsafe) + furrybot.commands[cmd] = { + unsafe = true, + params = "", + help = "Request to " .. help, + func = function(name, target) + if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then + furrybot.requests[target] = { + origin = name, + func = on_accept, + } + end + end, + } end -function furrybot.add_money(name, add) - local money = furrybot.get_money(name) - furrybot.set_money(name, money + add) +function furrybot.is_operator(name) + return name == minetest.localplayer:get_name() or furrybot.operators[name] end -function furrybot.take_money(name, remove) - local money = furrybot.get_money(name) - local new = money - remove - if new < 0 then - return false - else - furrybot.set_money(name, new) - return true - end +function 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.money(money, color) - return furrybot.colors.money .. "$" .. money .. color -end +function furrybot.list_command(cmd, list_name, title) + furrybot.commands[cmd] = { + func = function() + local names = {} --- Commands + for name in pairs(furrybot[list_name]) do + table.insert(names, name) + end --- system -function furrybot.commands.help() - local keys = {} - for k in pairs(furrybot.commands) do - table.insert(keys, k) - end - furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system) + furrybot.send("List of " .. title .. ": " .. table.concat(names, ", "), furrybot.colors.system) + end, + } end -function furrybot.commands.accept(name) - local tbl = furrybot.requests[name] - if tbl then - furrybot.requests[name] = nil - tbl.func(tbl.origin, name) - else - furrybot.error_message(name, "Nothing to accept") - end -end -furrybot.unsafe_commands.accept = true +furrybot.commands.cmd = { + ignore = true, +} -function furrybot.commands.deny(name) - local tbl = furrybot.requests[name] - if tbl then - furrybot.requests[name] = nil - furrybot.ping_message(name, "Denied request", furrybot.colors.system) - else - furrybot.error_message(name, "Nothing to deny") - end -end -furrybot.unsafe_commands.deny = true +furrybot.commands.status = { + ignore = true, +} --- don't bug players that are running ClamityBot commands from discord -function furrybot.commands.status() -end +furrybot.commands.help = { + params = "[]", + help = "Display help for a commands or show list of available commands", + func = function(name, command) + if command then + local def = furrybot.commands[command] -function furrybot.commands.cmd() -end + if def then + furrybot.send("!" .. command .. (def.params and " " .. def.params or "") .. ": " .. (def.help or "No description given"), furrybot.colors.system) + else + furrybot.error_message(name, "Invalid command", command) + end + else + local commands = {} --- rpg -furrybot.commands.cry = furrybot.solo_rpg_command("cries") -furrybot.commands.laugh = furrybot.solo_rpg_command("laughs") -furrybot.commands.confused = furrybot.solo_rpg_command("is confused") -furrybot.commands.smile = furrybot.solo_rpg_command("smiles") -furrybot.commands.hug = furrybot.interactive_rpg_command("hugs") -furrybot.commands.cuddle = furrybot.interactive_rpg_command("cuddles") -furrybot.commands.kiss = furrybot.interactive_rpg_command("kisses") -furrybot.commands.hit = furrybot.interactive_rpg_command("hits") -furrybot.commands.slap = furrybot.interactive_rpg_command("slaps") -furrybot.commands.beat = furrybot.interactive_rpg_command("beats") -furrybot.commands.lick = furrybot.interactive_rpg_command("licks") - -furrybot.commands.sex = furrybot.request_command(function(name, target) - furrybot.ping_message(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.", furrybot.colors.system) -end, function(name, target) - furrybot.send(name .. " and " .. target .. " are having sex! OwO", furrybot.colors.rpg) -end) -furrybot.commands.bang = furrybot.commands.sex -furrybot.commands.fuck = furrybot.commands.sex - -furrybot.commands.marry = furrybot.request_command(function(name, target) - if furrybot.storage:contains(name .. ".partner", target) then - furrybot.error_message(name, "You are already married to", furrybot.storage:get_string(name .. ".partner")) - return false - elseif furrybot.storage:contains(target .. ".partner", name) then - furrybot.error_message(name, target .. " is already married to", furrybot.storage:get_string(name .. ".partner")) - return false - else - furrybot.ping_message(target, name .. " proposes to you. Type !accept to accept or !deny to deny.", furrybot.colors.system) - end -end, function(name, target) - furrybot.storage:set_string(name .. ".partner", target) - furrybot.storage:set_string(target .. ".partner", name) - furrybot.send("Congratulations, " .. furrybot.ping(name, furrybot.colors.rpg) .. "&" .. furrybot.ping(target, furrybot.colors.rpg) .. ", you are married. You may now kiss :).", furrybot.colors.rpg) -end) -furrybot.commands.propose = furrybot.commands.marry -furrybot.unsafe_commands.marry = true -furrybot.unsafe_commands.propose = true - -function furrybot.commands.divorce(name) - if furrybot.storage:contains(name .. ".partner") then - local partner = furrybot.storage:get_string(name .. ".partner") - furrybot.storage:set_string(name .. ".partner", "") - furrybot.storage:set_string(partner .. ".partner", "") - furrybot.ping_message(name, "divorces from " .. partner .. " :(", furrybot.colors.rpg) - else - furrybot.error_message(name, "You are not married") - end -end -furrybot.unsafe_commands.divorce = true + for cmd in pairs(furrybot.commands) do + table.insert(commands, cmd) + end -function furrybot.commands.partner(name, target) - target = target or name - if furrybot.storage:contains(target .. ".partner") then - furrybot.ping_message(name, (target == name and "You are" or target .. " is") .. " married to " .. furrybot.storage:get_string(target .. ".partner"), furrybot.colors.system) - else - furrybot.error_message(name, (target == name and "You are" or target .. " is") .. " not married") - end -end -furrybot.commands.married = furrybot.commands.partner + table.sort(commands) --- misc -function furrybot.commands.rolldice(name) - furrybot.ping_message(name, "rolled a dice and got a " .. furrybot.random(1, 6, furrybot.colors.system) .. ".", furrybot.colors.system) -end + furrybot.send("Available commands: " .. table.concat(commands, ", "), furrybot.colors.system) + end + end, +} -function furrybot.commands.coinflip(name) - furrybot.ping_message(name, "flipped a coin and got " .. furrybot.choose({"Heads", "Tails"}, furrybot.colors.system) .. ".", furrybot.colors.system) -end +furrybot.commands.accept = { + unsafe = true, + help = "Accept a request", + func = function(name) + local tbl = furrybot.requests[name] + if tbl then + furrybot.requests[name] = nil + tbl.func(tbl.origin, name) + else + furrybot.error_message(name, "Nothing to accept") + end + end, +} -function furrybot.commands.choose(name, ...) - local options = {...} - if #options > 1 then - furrybot.ping_message(name, "I choose " .. furrybot.choose(options, "", furrybot.colors.system) .. ".", furrybot.colors.system) - else - furrybot.error_message(name, "Not enough options") - end -end +furrybot.commands.deny = { + unsafe = true, + help = "Deny a request", + func = function(name) + local tbl = furrybot.requests[name] + if tbl then + furrybot.requests[name] = nil + furrybot.ping_message(name, "Denied request", furrybot.colors.system) + else + furrybot.error_message(name, "Nothing to deny") + end + end, +} -function furrybot.commands.dicksize(name, target) - target = target or name - local size = furrybot.strrandom(target, 31242, 2, 10) - local dick = furrybot.repeat_string("=", size) .. "D" - furrybot.send(dick .. furrybot.colors.system .. " <= " .. furrybot.ping(target, furrybot.colors.system) .. "'s Dick", C("#FF4DE1")) -end -furrybot.commands.cocksize = furrybot.commands.dicksize +return function(_http, _env, _storage) + http, env, storage = _http, _env, _storage --- fun -function furrybot.commands.verse(name) - furrybot.json_http_request("https://labs.bible.org/api/?type=json&passage=random", name, function(data) - furrybot.send(data.text .. furrybot.colors.info .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]", furrybot.colors.fun) - end) -end + furrybot.operators = minetest.deserialize(storage:get_string("operators")) or {} + furrybot.ignored = minetest.deserialize(storage:get_string("ignored")) or {} -function furrybot.commands.define(name, word) - if word then - furrybot.json_http_request("https://api.dictionaryapi.dev/api/v1/entries/en_US/" .. word, name, function(data) - local meaning = data.meaning - local selected = meaning.exclamation or meaning.noun or meaning.verb or meaning.adjective or meaning["transitive verb"] or meaning.adverb or meaning["relative adverb"] - if not selected then - print(dump(meaning)) - furrybot.error_message(name, "Error in parsing response") - else - furrybot.send(word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. furrybot.colors.fun .. selected[1].definition, furrybot.colors.info) - end - end) - else - furrybot.error_message(name, "You need to specify a word") - end -end + for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http", "operator", "bullshit", "marriage", "waifu"} do + local func, err = env.loadfile("clientmods/furrybot/" .. f .. ".lua") -function furrybot.commands.insult(name, target) - if furrybot.online_or_error(name, target, true) then - furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data) - furrybot.ping_message(target, data, furrybot.colors.fun) - end) - end -end + if not func then + return false, err + end -function furrybot.commands.joke(name, first, last) - if not first then - first = "Chuck" - last = "Norris" - elseif not last then - last = "" + func()(http, env, storage) end - furrybot.json_http_request("http://api.icndb.com/jokes/random?firstName=" .. first .. "&lastName=" .. last, name, function(data) - local joke = data.value.joke:gsub(""", "\""):gsub(" ", " ") - furrybot.send(joke, furrybot.colors.fun) - end) -end - -function furrybot.commands.question(name) - furrybot.json_http_request("https://8ball.delegator.com/magic/JSON/anything", name, function(data) - furrybot.ping_message(name, data.magic.answer, furrybot.colors.fun) - end) -end -furrybot.commands["8ball"] = furrybot.commands.question --- economy -function furrybot.commands.money(name, target) - target = target or name - furrybot.ping_message(name, (target == name and "You have " or target .. " has ") .. furrybot.money(furrybot.get_money(target), furrybot.colors.system) .. ".", furrybot.colors.system) -end -furrybot.commands.balance = furrybot.commands.money + furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system) -function furrybot.commands.pay(name, target, number) - if furrybot.online_or_error(name, target) then - local money = tonumber(number or "") - if not money or money <= 0 or math.floor(money) ~= money then - furrybot.error_message(name, "Invalid amount of money") - else - if furrybot.take_money(name, money) then - furrybot.add_money(target, money) - furrybot.ping_message(target, name .. " has payed you " .. furrybot.money(money, furrybot.colors.system) .. ".", furrybot.colors.system) - else - furrybot.error_message(name, "You don't have enough money") - end - end + if furrybot.loaded then + furrybot.send("Reloaded", furrybot.colors.system) + else + furrybot.loaded = true end -end -furrybot.unsafe_commands.pay = true - --- send load message -furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system) -if furrybot.loaded then - furrybot.send("Reloaded", furrybot.colors.system) -else - furrybot.loaded = true + return true end