From c3be53ce451b3ac961a9ebcc69278db74d6a6ece Mon Sep 17 00:00:00 2001 From: Astoria Floyd Date: Sat, 7 Aug 2021 12:51:07 -0500 Subject: [PATCH] Refactored code for new detection system, now supports sub-files for commands! --- .gitignore | 2 + basicCommands.lua | 27 ---------- bot.lua | 124 ++++++-------------------------------------- complexCommands.lua | 51 ++++++++++++++++++ rng.lua | 3 +- simpleCommands.lua | 48 +++++++++++++++++ tools.lua | 42 +++++++++++++++ 7 files changed, 161 insertions(+), 136 deletions(-) delete mode 100644 basicCommands.lua create mode 100644 complexCommands.lua create mode 100644 simpleCommands.lua diff --git a/.gitignore b/.gitignore index 2b93db8..fa8f83f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ discordia.log gateway.json token.lua +launch.json +.vscode diff --git a/basicCommands.lua b/basicCommands.lua deleted file mode 100644 index 28b3a6d..0000000 --- a/basicCommands.lua +++ /dev/null @@ -1,27 +0,0 @@ -local basicCommands = {} - ---Hello World! -function basicCommands.helloWorld() - return("Hello world!") -end - ---Executes Figlet on the host machine. -function basicCommands.figlet(string) - local figleted = io.popen("figlet ".. string) - local result = figleted:read("*all") - return result -end - ---Executes Cowsay on the host machine. -function basicCommands.cowsay(string) - local cowsaid = io.popen("cowsay ".. string) - local result = cowsaid:read("*all") - return result -end - ---Takes input and makes it output. GIGO. -function basicCommands.echo(string) - return string -end - -return basicCommands diff --git a/bot.lua b/bot.lua index 58c4bbd..d2b2c50 100644 --- a/bot.lua +++ b/bot.lua @@ -1,113 +1,31 @@ local discordia = require('discordia') local client = discordia.Client() -local basicCommands = require("./basicCommands.lua") local tools = require("./tools.lua") -local rng = require("./rng.lua") +discordia.extensions() + +local simpleCommands = require("./simpleCommands.lua") +local complexCommands = require("./complexCommands.lua") + +--Initialize commands, CLEAN THIS UP, MAKE IT DO MORE THEN TWO, CURRENTLY WE NEED TO JUST DO THIS OVER AND OVER +local complexCommands = complexCommands.initialize() +local simpleCommands = simpleCommands.initialize() +local commands = tools.tableMerge(simpleCommands, complexCommands) + --Setup bot here, initializes bot client:once("ready", function() client:setGame("Astoria's bot, very sad!") print('Logged in as '.. client.user.username) end) ---Everything past this point is commands, initialized here in bot.lua, but its good practise to have the actual functions outside, may be converted to one main call. May be laggier that way, but less cluttered. Perhaps a table? - ---Creates a lenny face -client:on('messageCreate', function(message) - if tools.messageDectection(message, "lenny") == true then - message.channel:send("( ͡° ͜ʖ ͡°)") - message:delete() - end -end) ---Posts an image of glomping, may add RNG later. +--Command handler client:on('messageCreate', function(message) - if tools.messageDectection(message, "glomp") == true then - message.channel:send("https://tenor.com/view/tv-shows-television-tackle-hug-hug-glomping-gif-14859564") - end -end) - ---Ping Pong function, generally just to see if its on or not without triggering a more complex function. Also tests if its really borked. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "ping") == true then - message.channel:send('Pong!') - end -end) - ---Places elmo on fire in chat. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "fire") == true then - message.channel:send('https://tenor.com/view/elmo-fire-burn-flame-gif-5042503') - end -end) - ---Dumps docs/help to chat, took me forever to figure out. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "helpme") == true then - message.channel:send(tools.printFile("docs/help")) - end -end) - ---Rolls a d20, check RNG for more info. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "roll") == true then - message.channel:send(rng.d20()) - end -end) - ---Posts time to channel, stuck in military time, perhaps use a io.popen() to get actual system time in a more human readable format? -client:on('messageCreate', function(message) - if tools.messageDectection(message, "time") == true then - message.channel:send('The current time in military time is ' .. os.date() .. ' atleast in Chicago!') - end -end) - ---Prints contents and attachments to console. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "analyze") == true then - print(message.content) - print(message.attachments) - end -end) - ---Parrots input to cowsay, then echos it to the same channel you are in. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "figlet ") == true then - local figletthis = string.sub(message.content, 9) - local figlet = basicCommands.figlet(figletthis) - local result = "```fix" .. "\n" .. figlet .. "```" - message.channel:send(result) - end -end) - ---Parrots input to cowsay, then echos it to the same channel you are in. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "cowsay ") == true then - local cowsay = string.sub(message.content, 9) - local cowsaid = basicCommands.cowsay(cowsay) - local result = "```fix" .. "\n" .. cowsaid .. "```" - message.channel:send(result) - end -end) - ---Echo's what you said back out, in a fix codeblock. Could be against TOS. -client:on('messageCreate', function(message) - if tools.messageDectection(message, "echo ") == true then - local echo = string.sub(message.content, 7) - local echoed = basicCommands.echo(echo) - local result = "```fix" .. "\n" .. echoed .. "```" - message.channel:send(result) - message:delete() - end -end) - ---When anyone preforms welsh, send gif. You know the drill -client:on('messageCreate', function(message) - if tools.messageDectection(message, "welsh") == true then - image = "https://cdn.discordapp.com/attachments/748713417489252503/770289379586867231/image0.gif" - message.channel:send(image) - message:delete() + if message.author.bot then return end + local args = message.content:split(" ") + local command = commands[args[1]] + if command then + command.exec(message) end end) ---Non command stuff past this line --When a user is banned, post a lenney. client:on('userBan', function() @@ -119,14 +37,6 @@ client:on('userBan', function() banChannel:send("User was banned ( ͡° ͜ʖ ͡°)") end) ---Every time someone says "gif" or some varient of that, often in gifs themselves, it pastes that text. May replace with a copypasta. -client:on('messageCreate', function(message) - if message.author.bot then return end - if tools.messageDectectionAnywhere(message, "gif") == true then - message.channel:send("GIF IS PRONOUNCED WITH A HARD G, ITS NOT JIF OR YIFF") - end -end) - --Insert Token in a .lua file with simply returns it as a string. local token = require("./token.lua") -client:run('Bot '..token) +client:run('Bot '..token) \ No newline at end of file diff --git a/complexCommands.lua b/complexCommands.lua new file mode 100644 index 0000000..7c1f985 --- /dev/null +++ b/complexCommands.lua @@ -0,0 +1,51 @@ +local tools = require("./tools.lua") +local rng = require("./rng.lua") +local complexCommands = {} + +local prefix = 'test!' + +local commands = { -- Define commands its a table that will contain our commands +[prefix..'helpme'] = { -- Dumps docs/help to chat, took me forever to figure out. + exec = function (message) + message.channel:send(tools.printFile("docs/help")) + end + }; +[prefix..'roll'] = { -- Rolls a d20, check RNG for more info. + exec = function (message) + message.channel:send(rng.d20()) + end + }; +[prefix..'figlet'] = { -- Parrots input to figlet, then echos it to the same channel you are in. + exec = function (message) + local prefixLength = string.len(prefix) + local figletthis = string.sub(message.content, 7+prefixLength) + local figlet = tools.figlet(figletthis) + local result = "```fix" .. "\n" .. figlet .. "```" + message.channel:send(result) + end + }; +[prefix..'cowsay'] = { -- Parrots input to cowsay, then echos it to the same channel you are in. + exec = function (message) + local prefixLength = string.len(prefix) + local cowsay = string.sub(message.content, 7+prefixLength) + local cowsaid = tools.cowsay(cowsay) + local result = "```fix" .. "\n" .. cowsaid .. "```" + message.channel:send(result) + end + }; +[prefix..'echo'] = { -- Echo's what you said back out, in a fix codeblock. Could be against TOS. + exec = function (message) + local prefixLength = string.len(prefix) + local echo = string.sub(message.content, 5+prefixLength) + local echoed = tools.echo(echo) + local result = "```fix" .. "\n" .. echoed .. "```" + message.channel:send(result) + message:delete() + end + }; +} + +function complexCommands.initialize() +return commands +end +return complexCommands \ No newline at end of file diff --git a/rng.lua b/rng.lua index 668e34b..478048e 100644 --- a/rng.lua +++ b/rng.lua @@ -3,10 +3,9 @@ local tools = require("./tools.lua") --Rolls a d20, if output is 1, Critical miss(or Shit), if output is 20, critical hit. function rng.d20() - tools.reseed() + tools.seed() local dice = math.random(20) local messagePart1 = ('You rolled a ' .. dice .. ' out of 20') - print(messagePart1) local messagepart2 = '' if dice==20 then messagepart2 = ('Nat 20! Crititcal Hit') diff --git a/simpleCommands.lua b/simpleCommands.lua new file mode 100644 index 0000000..a2b033b --- /dev/null +++ b/simpleCommands.lua @@ -0,0 +1,48 @@ +local basicCommands = {} +local prefix = "test!" +local commands = { -- Define commands its a table that will contain our commands +[prefix..'lenny'] = { -- Creates a lenny face + exec = function (message) + message.channel:send('( ͡° ͜ʖ ͡°)') + end + }; +[prefix..'glomp'] = { -- Posts an image of glomping, may add RNG later. + exec = function (message) + message.channel:send('https://tenor.com/view/tv-shows-television-tackle-hug-hug-glomping-gif-14859564') + end + }; +[prefix..'ping'] = { -- Ping Pong function, generally just to see if its on or not without triggering a more complex function. Also tests if its really borked. + exec = function (message) + message.channel:send("Pong!") + end + }; +[prefix..'fire'] = { -- Places elmo on fire in chat. + exec = function (message) + message.channel:send("https://tenor.com/view/elmo-fire-burn-flame-gif-5042503") + end + }; + +[prefix..'time'] = { -- Posts time to channel, stuck in military time, perhaps use a io.popen() to get actual system time in a more human readable format? + exec = function (message) + message.channel:send('The current time in military time is ' .. os.date() .. ' atleast in Chicago!') + end + }; +[prefix..'analyze'] = { -- Prints contents and attachments to console. + exec = function (message) + p(message.content) + p(message.attachments) + end + }; +[prefix..'welsh'] = { -- + exec = function (message) + message.channel:send("https://cdn.discordapp.com/attachments/748713417489252503/770289379586867231/image0.gif") + message:delete() + end + }; +} + + +function basicCommands.initialize() + return commands + end +return basicCommands diff --git a/tools.lua b/tools.lua index 4a5172b..c0c1ab4 100644 --- a/tools.lua +++ b/tools.lua @@ -15,7 +15,30 @@ function tools.printFile(file) return message end +function tools.tableConcat(t1,t2) + for i=1,#t2 do + t1[#t1+1] = t2[i] + end + return t1 +end + +function tools.tableMerge(t1, t2) + for k,v in pairs(t2) do + if type(v) == "table" then + if type(t1[k] or false) == "table" then + tools.tableMerge(t1[k] or {}, t2[k] or {}) + else + t1[k] = v + end + else + t1[k] = v + end + end + return t1 +end + --Message detection logic. If string.find detects both the desired string, and the key, starting at position 1, do the thing. +--Now deprecated function tools.messageDectection(message, search) local distinctMessage = string.lower(message.content) local key = "!" @@ -37,4 +60,23 @@ function tools.messageDectectionAnywhere(message, search) end end +--Executes Figlet on the host machine. +function tools.figlet(string) + local figleted = io.popen("figlet ".. string) + local result = figleted:read("*all") + return result + end + + --Executes Cowsay on the host machine. + function tools.cowsay(string) + local cowsaid = io.popen("cowsay ".. string) + local result = cowsaid:read("*all") + return result + end + + --Takes input and makes it output. GIGO. + function tools.echo(string) + return string + end + return tools \ No newline at end of file