Added yet more mode switching, cleaned up code, made userBan work in any guild

pull/3/head
Astoria Floyd 3 years ago
parent 514d530d58
commit 209f1207a5

2
.gitignore vendored

@ -4,3 +4,5 @@ token.lua
launch.json
.vscode
interpreter.lua
docs/reinitialize.lua
docs/mode.lua

@ -1,6 +1,5 @@
---@diagnostic disable: undefined-global
local tools = {}
--initializes or re-initializes the seed of RNG, based on Unix Time.
function tools.seed()
local seed = os.time()
@ -9,15 +8,14 @@ end
--Initializes commands.
function tools.initialize()
if tools.testModeDetection() == false then
if tools.getMode() == "normal" then
local simpleCommands = dofile("./commands/simpleCommands.lua")
local complexCommands = dofile("./commands/complexCommands.lua")
local complexCommands = complexCommands.initialize()
local simpleCommands = simpleCommands.initialize()
local commands = tools.tableMerge(simpleCommands, complexCommands)
return commands
end
if tools.testModeDetection() == true then
elseif tools.getMode() == "test" then
local simpleCommands = dofile("./commands/simpleCommands.lua")
local complexCommands = dofile("./commands/complexCommands.lua")
local experimentalCommands = dofile("./commands/experimentalCommands.lua")
@ -27,16 +25,66 @@ function tools.initialize()
local commandsP1 = tools.tableMerge(simpleCommands, complexCommands)
local commands = tools.tableMerge(commandsP1, experimentalCommands)
return commands
else
local simpleCommands = dofile("./commands/simpleCommands.lua")
local complexCommands = dofile("./commands/complexCommands.lua")
local complexCommands = complexCommands.initialize()
local simpleCommands = simpleCommands.initialize()
local commands = tools.tableMerge(simpleCommands, complexCommands)
return commands
end
end
--Reads testmode.lua
function tools.testModeDetection()
local mode = dofile("./docs/mode.lua")
if mode == "normal" then return false end
if mode == "test" then return true end
--Sets mode.lua
function tools.setMode(mode)
local file = io.open("./docs/mode.lua", "r+")
io.input(file)
io.output(file)
file:seek("set", 7)
if mode == "test" then
file:write("'test' ")
elseif mode == "normal" then
file:write("'normal'")
else
--Default to normal
file:write("'normal'")
end
file:close()
end
function tools.getMode()
local file = io.open("./docs/mode.lua", "r")
io.input(file)
file:seek("set", 7)
local mode = file:read()
file:close()
mode = mode:gsub("%s+", "")
mode = mode:gsub("'", "")
return mode
end
--Sets if we should reinitialize.
function tools.setReinit(reinit)
local reinitFile = io.open("./docs/reinitialize.lua", "r+")
io.input(reinitFile)
io.output(reinitFile)
reinitFile:seek("set", 7)
if type(reinit) == "boolean" then
if reinit == true then
reinitFile:write("true ")
elseif reinit == false then
reinitFile:write("false")
end
else
--default to no
reinitFile:write("return false")
end
reinitFile:close()
end
function tools.getReinit()
return dofile("./docs/reinitialize.lua")
end
--Reads an entire file, outputs as string.
function tools.printFile(file)

@ -8,49 +8,33 @@ client:once("ready", function()
client:setGame("Astoria's bot, very sad!")
print('Logged in as '.. client.user.username)
Commands = Tools.initialize()
if Tools.testModeDetection() == true then
mode = "test"
else
mode = "normal"
end
mode = Tools.getMode()
end)
--Command handler
client:on('messageCreate', function(message)
if message.author.bot then return end
local shouldReinit = false
if Tools.testModeDetection() == true and mode == "normal" then
mode = "test"
shouldReinit = true
elseif Tools.testModeDetection() == false and mode == "test" then
mode = "normal"
shouldReinit = false
end
mode = Tools.getMode()
if mode == "test" then
shouldReinit = true
Tools.setReinit(true)
end
if dofile("./docs/reinitialize.lua") == true then
Commands = Tools.initialize()
Tools = dofile("./API/tools.lua")
if mode == "normal" and Tools.getReinit() == false then
Tools.setReinit(false)
end
if mode == "normal" and Tools.messageDectection(message, "reinitialize") == true then
p("reinitd at "..os.time())
reinitialize(message)
reinitialize()
elseif mode == "test" and Tools.messageDectection(message, "reinitialize") == true then
message:reply("You cannot preform this command because test is on!")
p("Not reinitd at "..os.time())
end
if Tools.getReinit() == true then
Commands = Tools.initialize()
Tools = dofile("./API/tools.lua")
Tools.setReinit(false)
end
local args = message.content:split(" ")
local lowerArgs = args[1]:lower()
local command = Commands[lowerArgs]
if command then
if shouldReinit == true then
Commands = Tools.initialize()
Tools = dofile("./API/tools.lua")
command = Commands[lowerArgs]
p("reinitd at "..os.time())
shouldReinit = false
end
command.exec(message, args)
end
end)
@ -61,26 +45,16 @@ function reinitialize(message)
if not user:hasPermission("administrator") then
message:reply("You cannot re-initialize this bot!")
else
Commands = Tools.initialize()
Tools = dofile("./API/tools.lua")
Tools.setReinit(true)
message:reply("Re-Initialized!")
end
end
end
function selfReinitialize()
Commands = Tools.initialize()
Tools = dofile("./API/tools.lua")
end
--When a user is banned, post a lenney.
client:on('userBan', function()
local banChannel, err = client:getChannel("872283716486066200")
if not banChannel then
p("Attempt to fetch the channel object: ", err)
return
end
banChannel:send("User was banned ( ͡° ͜ʖ ͡°)")
client:on('userBan', function(user, guild)
guild.systemChannel:send("A user was banned ( ͡° ͜ʖ ͡°)")
end)
--Insert Token in a .lua file with simply returns it as a string.

@ -2,11 +2,12 @@ local tools = dofile("./API/tools.lua")
local rng = dofile("./API/rng.lua")
local complexCommands = {}
local prefix = dofile("./docs/key.lua")
local mode = tools.getMode()
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)
if tools.testModeDetection() == true then
if mode == "test" then
message.channel:send(tools.printFile("docs/helpTestMode"))
else
message.channel:send(tools.printFile("docs/help"))
@ -78,39 +79,19 @@ end
if not user:hasPermission("administrator") then
message:reply("You cannot change the mode of this bot!")
else
file = io.open("./docs/mode.lua", "r+")
io.input(file)
io.output(file)
if tools.testModeDetection() == true then
mode = "test"
else
mode = "normal"
end
if not arg[2] then message.channel:send("```fix\nNo argument. Get or Set?```") return end
if arg[2] ~= "get" and arg[2] ~= "set" then message.channel:send("```fix\nMalformed input, you put in '"..arg[2].."' not get or set.```") return end
if arg[2] == "get" then message.channel:send("```fix\nCurrent mode is '"..mode.."'```") return end
if arg[2] == "get" then mode = tools.getMode() message.channel:send("```fix\nCurrent mode is '"..mode.."'```") return end
if arg[2] == "set" then
if arg[3] == "normal" then
message.channel:send("```fix\nSet mode 'normal'```")
file:seek("set", 0)
file:write("return 'normal'")
elseif arg[3] == "test" then
message.channel:send("```fix\nset mode 'test'```")
file:seek("set", 0)
file:write("return 'test' \n")
elseif arg[3] ~= "normal" or "test" then
if arg[3] == "normal" or "test" then
mode = tostring(arg[3])
message.channel:send("```fix\nSet mode '"..mode.."'```")
tools.setMode(mode)
tools.setReinit(true)
else
message.channel:send("```fix\nIncorrect mode, please enter 'normal' or 'test'```")
end
io.close()
file = io.open("./docs/reinitialize.lua", "r+")
io.input(file)
io.output(file)
file:seek("set", 0)
file:write("return true ")
end
io.close()
--message.channel:send()
--message:delete()
end
end
};

@ -1,2 +1 @@
return 'normal'
--normal or test
return 'normal'

@ -1 +1 @@
return true
return false
Loading…
Cancel
Save