]> git.lizzy.rs Git - furrybot.git/blobdiff - bot.lua
Add extinct command
[furrybot.git] / bot.lua
diff --git a/bot.lua b/bot.lua
index de71d4ae36495c1d958f550d61108ddba5b8b0fb..e75613999110647f6367ceaf82e3d2120aaacaa9 100644 (file)
--- a/bot.lua
+++ b/bot.lua
@@ -1,42 +1,77 @@
 furrybot.commands = {}
+furrybot.requests = {}
+furrybot.unsafe_commands = {}
 
+local http, env, storage
 local C = minetest.get_color_escape_sequence
 
+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)
-       return C("#00DCFF") .. "@" .. player .. C("#FFFA00")
+function furrybot.ping(player, color)
+       return furrybot.colors.ping .. "@" .. player .. color
 end
 
-function furrybot.ping_player(player, message)
-       furrybot.send(furrybot.ping(player) .. ": " .. message)
+function furrybot.ping_message(player, message, color)
+       furrybot.send(furrybot.ping(player, color) .. ": " .. message, "")
 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.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(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))
+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 func = furrybot.commands[cmd]
+               if func then
+                       if furrybot.unsafe_commands[cmd] and discord then
+                               furrybot.error_message(player, "Sorry, you cannot run this command from discord: ", cmd)
                        else
-                               furrybot.ping_player_error(player, "Invalid command", cmd)
+                               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
@@ -45,28 +80,32 @@ function furrybot.player_online(name)
        end
 end
 
-function furrybot.check_online(name, target)
-       if not target then
-               furrybot.ping_player_error(name, "You need to specify a player")
-       elseif name == target then
-               furrybot.ping_player_error(name, "You need to specify a different player than yourself")
-       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.choose(list)
-       return list[math.random(#list)]
+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)
-       furrybot.http.fetch({url = url}, function(res)
+       http.fetch({url = url}, function(res)
                if res.succeeded then
                        callback(res.data)
                else
-                       furrybot.ping_player_error(name, "Request failed with code", res.code)
+                       furrybot.error_message(name, "Request failed with code", res.code)
                end
        end)
 end
@@ -78,7 +117,7 @@ function furrybot.json_http_request(url, name, callback)
        end)
 end
 
-function furrybot.rand(str, seed, ...)
+function furrybot.strrandom(str, seed, ...)
        local v = 0
        local pr = PseudoRandom(seed)
        for i = 1, #str do
@@ -87,142 +126,98 @@ function furrybot.rand(str, seed, ...)
        return PseudoRandom(v):next(...)
 end
 
--- commands
-
-function furrybot.commands.hug(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.send(name .. " hugs " .. target .. ".")
-       end
-end
-
-furrybot.commands.cuddle = furrybot.commands.hug
-
-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.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")
-               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.commands.accept(name)
-       local func = furrybot.target_list[name]
-       if func then
-               func()
-       else
-               furrybot.ping_player_error(name, "Nothing to accept")
+function furrybot.interactive_roleplay_command(action)
+       return function(name, target)
+               if furrybot.online_or_error(name, target) then
+                       furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.roleplay)
+               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")
+function furrybot.solo_roleplay_command(action)
+       return function(name)
+               furrybot.send(name .. " " .. action .. ".", furrybot.colors.roleplay)
        end
 end
 
-function furrybot.commands.hit(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.send(name .. " hits " .. target)
+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
 end
 
-furrybot.commands.slap = furrybot.commands.hit
-furrybot.commands.beat = furrybot.commands.hit
+-- General purpose commands
 
 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.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
 end
 
-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 .. C("#00FFC3") .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]")
-       end)
-end
-
-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.ping_player_error(name, "Error in parsing response")
-                       else
-                               furrybot.send(C("#00FFC3") .. word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. C("#FFFA00") .. selected[1].definition)
-                       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.ping_player_error(name, "You need to specify a word")
+               furrybot.error_message(name, "Nothing to accept")
        end
 end
+furrybot.unsafe_commands.accept = true
 
-function furrybot.commands.insult(name, target)
-       if furrybot.check_online(name, target) then
-               furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data)
-                       furrybot.ping_player(target, data)
-               end)
+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
 
-function furrybot.commands.rolldice(name)
-       furrybot.ping_player(name, "rolled a dice and got a " .. C("#AAFF43") .. math.random(6))
-end
-
-function furrybot.commands.coinflip(name)
-       furrybot.ping_player(name, "flipped a coin and got " .. C("#AAFF43") .. furrybot.choose({"Heads", "Tails"}))
-end
-
+-- don't bug players that are running ClamityBot commands from discord
 function furrybot.commands.status()
 end
 
 function furrybot.commands.cmd()
 end
 
-function furrybot.commands.cocksize(name, target)
-       target = target or name
-       local msg = C("#FF4DE1")
-       local size = furrybot.rand(target, 31242, 2, 10)
-       for i = 1, size do
-               msg = msg .. "="
+return function(_http, _env, _storage)
+       http, env, storage = _http, _env, _storage
+
+       for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http", "operator"} do
+               local func, err = env.loadfile("clientmods/furrybot/" .. f .. ".lua")
+
+               if not func then
+                       return false, err
+               end
+
+               func()(http, env, storage)
        end
-       msg = msg .. "D"
-       furrybot.send(msg .. C("#FFFA00") .. "  <= " .. furrybot.ping(target) .. "'s Cock")
-end
 
-furrybot.commands.dicksize = furrybot.commands.cocksize
+       furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
 
-function furrybot.commands.joke(name, first, last)
-       if not first then
-               first = "Chuck"
-               last = "Norris"
+       if furrybot.loaded then
+               furrybot.send("Reloaded", furrybot.colors.system)
+       else
+               furrybot.loaded = true
        end
-       furrybot.json_http_request("http://api.icndb.com/jokes/random?firstName=" .. first .. "&lastName=" .. (last or ""), name, function(data)
-               local joke = data.value.joke:gsub("&quot;", "\""):gsub("  ", " ")
-               furrybot.send(joke)
-       end)
-end
 
-if furrybot.loaded then
-       furrybot.send("Reloaded")
-else
-       furrybot.loaded = true
+       return true
 end