]> git.lizzy.rs Git - furrybot.git/blobdiff - bot.lua
Japanese waifu names
[furrybot.git] / bot.lua
diff --git a/bot.lua b/bot.lua
index aa2e6d5b8d1559ceab5fda178b68acd400e3a628..e97d1b84c4ee612e4986214061d7fbd02e098f29 100644 (file)
--- a/bot.lua
+++ b/bot.lua
@@ -1,22 +1,76 @@
 furrybot.commands = {}
+furrybot.requests = {}
 
+local http, env, storage
 local C = minetest.get_color_escape_sequence
-local http = minetest.get_http_api()
 
+furrybot.colors = {
+       ping = C("#00DCFF"),
+       system = C("#FFFA00"),
+       error = C("#D70029"),
+       detail = C("#FF6683"),
+       roleplay = C("#FFD94E"),
+       braces = C("#FFFAC0"),
+       info = C("#00FFC3"),
+       fun = C("#A0FF24"),
+       random = C("#A300BE"),
+       money = C("#A11600"),
+}
+
+-- helper functions
 function furrybot.send(msg, color)
-       minetest.send_chat_message("/me " .. C("#00FF3C") .. "[" .. C(color or "#FFFA00") .. msg .. C("#00FF3C") .. "]")
+       minetest.send_chat_message("/me " .. furrybot.colors.braces .. "[" .. color .. msg .. furrybot.colors.braces .. "]")
+end
+
+function furrybot.ping(player, color)
+       return furrybot.colors.ping .. "@" .. player .. color
 end
 
-function furrybot.ping(player)
-       return C("#00DCFF") .. "@" .. player .. C("#FFFA00")
+function furrybot.ping_message(player, message, color)
+       furrybot.send(furrybot.ping(player, color) .. ": " .. message, "")
 end
 
-function furrybot.ping_player(player, message)
-       furrybot.send(furrybot.ping(player) .. ": " .. message)
+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.ping_player_error(player, err, detail)
-       furrybot.ping_player(player, C("#D70029") .. " " .. err ..  " " .. (detail and C("#FF6683") .. "'" .. detail .. "'" .. C("#D70029") or "") .. ".")
+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)
@@ -27,113 +81,223 @@ function furrybot.player_online(name)
        end
 end
 
-function furrybot.check_online(name, target)
-       if name == target then
-               furrybot.ping_player_error(name, "You need to specify another player")
-       elseif furrybot.player_online(target) then
+function furrybot.online_or_error(name, other, allow_self)
+       if not other then
+               furrybot.error_message(name, "You need to specify a player")
+       elseif name == other and not allow_self then
+               furrybot.error_message(name, "You need to specify a different player than yourself")
+       elseif furrybot.player_online(other) then
                return true
        else
-               furrybot.ping_player_error(name, "Player not online", target)
+               furrybot.error_message(name, "Player not online", other)
        end
 end
 
-function furrybot.recieve(msg)
-       msg = minetest.strip_colors(msg)
-       if msg:find("<") == 1 then
-               local idx = msg:find(">")
-               local player = msg:sub(2, 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
-                               func(player, unpack(args))
-                       else
-                               furrybot.ping_player_error(player, "Invalid command", cmd)
-                       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.http_request(url, name, callback)
+       http.fetch({url = url}, function(res)
+               if res.succeeded then
+                       callback(res.data)
+               else
+                       furrybot.error_message(name, "Request failed with code", res.code)
                end
-       end
+       end)
 end
 
-function furrybot.commands.hug(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.send(name .. " hugs " .. target .. ".")
-       end
+function furrybot.json_http_request(url, name, callback)
+       furrybot.http_request(url, name, function(raw)
+               local data = minetest.parse_json(raw)
+               callback(data[1] or data)
+       end)
 end
 
-furrybot.commands.cuddle = furrybot.commands.hug
+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.commands.kiss(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.send(name .. " kisses " .. target .. ".")
+function furrybot.repeat_string(str, times)
+       local msg = ""
+       for i = 1, times do
+               msg = msg .. str
        end
+       return msg
 end
 
-furrybot.target_list = {}
+function furrybot.uppercase(str)
+       return str:sub(1, 1):upper() .. str:sub(2, #str)
+end
 
-function furrybot.commands.bang(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.target_list[target] = function()
-                       furrybot.send(ping(name) .. " and " .. ping(target) .. " are having sex! OwO")
+function furrybot.interactive_roleplay_command(cmd, action)
+       furrybot.commands[cmd] = {
+               params = "<player>",
+               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,
-               furrybot.ping_player(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.")
-       end
+       }
 end
 
-furrybot.commands.sex = furrybot.commands.bang
-furrybot.commands.fuck = furrybot.commands.bang
+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.commands.accept(name)
-       local func = furrybot.target_list[name]
-       if func then
-               func()
-       else
-               furrybot.ping_player_error(name, "Nothing to accept")
-       end
+function furrybot.request_command(cmd, help, on_request, on_accept, unsafe)
+       furrybot.commands[cmd] = {
+               unsafe = true,
+               params = "<player>",
+               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.commands.deny(name)
-       if furrybot.target_list[name] then
-               furrybot.target_list[name] = nil
-               furrybot.ping_player(name, "Denied request")
-       else
-               furrybot.ping_player_error(name, "Nothing to deny")
-       end
+function furrybot.is_operator(name)
+       return name == minetest.localplayer:get_name() or furrybot.operators[name]
 end
 
-function furrybot.commands.hit(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.send(name .. " hits " .. target)
-       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
 
-furrybot.commands.slap = furrybot.commands.hit
-furrybot.commands.beat = furrybot.commands.hit
+function furrybot.list_command(cmd, list_name, title)
+       furrybot.commands[cmd] = {
+               func = function()
+                       local names = {}
 
-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, ", "))
-end
+                       for name in pairs(furrybot[list_name]) do
+                               table.insert(names, name)
+                       end
 
-function furrybot.commands.verse(name)
-       local req = {
-               url = "https://labs.bible.org/api/?type=json&passage=random",
+                       furrybot.send("List of " .. title .. ": " .. table.concat(names, ", "), furrybot.colors.system)
+               end,
        }
-       local res = http.fetch_sync(req)
-       if res.succeeded then
-               local data = minetest.parse_json(res.data)[1]
-               furrybot.send(data.text .. C("#00FFC3") .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]")
+end
+
+furrybot.commands.cmd = {
+       ignore = true,
+}
+
+furrybot.commands.status = {
+       ignore = true,
+}
+
+furrybot.commands.help = {
+       params = "[<command>]",
+       help = "Display help for a commands or show list of available commands",
+       func = function(name, command)
+               if command then
+                       local def = furrybot.commands[command]
+
+                       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 = {}
+
+                       for cmd in pairs(furrybot.commands) do
+                               table.insert(commands, cmd)
+                       end
+
+                       table.sort(commands)
+
+                       furrybot.send("Available commands: " .. table.concat(commands, ", "), furrybot.colors.system)
+               end
+       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,
+}
+
+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,
+}
+
+return function(_http, _env, _storage)
+       http, env, storage = _http, _env, _storage
+
+       furrybot.operators = minetest.deserialize(storage:get_string("operators")) or {}
+       furrybot.ignored = minetest.deserialize(storage:get_string("ignored")) or {}
+
+       for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http", "operator", "bullshit", "marriage", "waifu"} do
+               local func, err = env.loadfile("clientmods/furrybot/" .. f .. ".lua")
+
+               if not func then
+                       return false, err
+               end
+
+               func()(http, env, storage)
+       end
+
+       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.ping_player_error(name, "Request failed with code", res.code)
+               furrybot.loaded = true
        end
-end
 
-if furrybot.loaded then
-       furrybot.send("Reloaded")
-else
-       furrybot.loaded = true
+       return true
 end