From 1c50d562109f92b514b94715143d484e89309b23 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Fri, 26 Nov 2021 20:58:11 +0400 Subject: [PATCH] fixed commands crashing --- discordia.log | 29 +++++++++ gateway.json | 1 + libraries/classes/command-acl.lua | 88 +++++++++++++++++++++++++++ libraries/classes/command-class.lua | 0 libraries/classes/command-handler.lua | 13 +++- libraries/classes/command.lua | 7 ++- libraries/classes/server-handler.lua | 11 +++- libraries/fake_message.lua | 48 +++++++++++++++ 8 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 discordia.log create mode 100644 gateway.json create mode 100644 libraries/classes/command-acl.lua create mode 100644 libraries/classes/command-class.lua create mode 100644 libraries/fake_message.lua diff --git a/discordia.log b/discordia.log new file mode 100644 index 0000000..8579b21 --- /dev/null +++ b/discordia.log @@ -0,0 +1,29 @@ +2021-11-26 20:38:37 | [INFO] | Discordia 2.8.4 +2021-11-26 20:38:37 | [INFO] | Connecting to Discord... +2021-11-26 20:38:38 | [INFO] | Authenticated as 512mb.org#1582 +2021-11-26 20:38:38 | [INFO] | Launching shard 0 (1 out of 1)... +2021-11-26 20:38:39 | [INFO] | Shard 0 : Connected to wss://gateway.discord.gg +2021-11-26 20:38:39 | [INFO] | Shard 0 : Received HELLO +2021-11-26 20:38:39 | [INFO] | Shard 0 : Received READY +2021-11-26 20:42:01 | [INFO] | Discordia 2.8.4 +2021-11-26 20:42:01 | [INFO] | Connecting to Discord... +2021-11-26 20:42:01 | [INFO] | Authenticated as 512mb.org#1582 +2021-11-26 20:42:01 | [INFO] | Launching shard 0 (1 out of 1)... +2021-11-26 20:42:01 | [INFO] | Shard 0 : Connected to wss://gateway.discord.gg +2021-11-26 20:42:01 | [INFO] | Shard 0 : Received HELLO +2021-11-26 20:42:02 | [INFO] | Shard 0 : Received READY +2021-11-26 20:43:30 | [INFO] | Discordia 2.8.4 +2021-11-26 20:43:30 | [INFO] | Connecting to Discord... +2021-11-26 20:43:30 | [INFO] | Authenticated as 512mb.org#1582 +2021-11-26 20:43:30 | [INFO] | Launching shard 0 (1 out of 1)... +2021-11-26 20:43:31 | [INFO] | Shard 0 : Connected to wss://gateway.discord.gg +2021-11-26 20:43:31 | [INFO] | Shard 0 : Received HELLO +2021-11-26 20:43:31 | [INFO] | Shard 0 : Received READY +2021-11-26 20:49:51 | [INFO] | Discordia 2.8.4 +2021-11-26 20:49:51 | [INFO] | Connecting to Discord... +2021-11-26 20:49:51 | [INFO] | Authenticated as 512mb.org#1582 +2021-11-26 20:49:51 | [INFO] | Launching shard 0 (1 out of 1)... +2021-11-26 20:49:51 | [INFO] | Shard 0 : Connected to wss://gateway.discord.gg +2021-11-26 20:49:51 | [INFO] | Shard 0 : Received HELLO +2021-11-26 20:49:52 | [INFO] | Shard 0 : Received READY +2021-11-26 20:50:10 | [ERROR] | 400 - Bad Request : GET https://discord.com/api/v7/guilds/640251445949759499/members/nil diff --git a/gateway.json b/gateway.json new file mode 100644 index 0000000..8bfc425 --- /dev/null +++ b/gateway.json @@ -0,0 +1 @@ +{"url":"wss://gateway.discord.gg","913818433748086784":{"owner":{"discriminator":"7452","public_flags":0,"id":"245973168257368076","username":"Yessiest","avatar":"f8e9fa5b64a0333da12b9ece6d636db3","flags":0},"timestamp":1637944718,"shards":1}} \ No newline at end of file diff --git a/libraries/classes/command-acl.lua b/libraries/classes/command-acl.lua new file mode 100644 index 0000000..46a46e4 --- /dev/null +++ b/libraries/classes/command-acl.lua @@ -0,0 +1,88 @@ +--Generic ACL extended to work with discord-specific features +local acl = import("classes.acl") +local command_acl = acl:extend("CommandACL") +local table_utils = import("table-utils") +--The following method extends the ACL class to work with rule-specific features, +--such as the role position +function command_acl:check_group(roles) + local found = false + local highest_role = nil + local highest_role_status = nil + for k,v in pairs(roles) do + if self.group_rules[v.id] then + found = true + if not highest_role then + highest_role = v + highest_role_status = self.group_rules[v.id] + end + if v.position > highest_role.position then + highest_role = v + highest_role_status = self.group_rules[v.id] + end + end + end + local allow = highest_role_status + return found,(allow and allow == 1) +end +--The following methods extend the ACL class to add the "perm" permissions +--(the fallback when no rule/user permissions are found) +function command_acl:__init() + self.user_rules = {} + self.group_rules = {} + self.perm_rules = {} +end +function command_acl:check_perm(perms) + local output = true + for k,v in pairs(self.perm_rules) do + if not perms[v] then + output = false + end + end + return output +end +function command_acl:set_perm_rules(list) + assert(type(list)=="table","table expected, got "..type(list)) + self.perm_rules = list +end +function command_acl:export_all_lists() + local lists = { + users = "", + groups = "", + perm = "" + } + for k,v in pairs(self.user_rules) do + lists.users = lists.users..k..":"..tostring(v)..";\n" + end + for k,v in pairs(self.group_rules) do + lists.groups = lists.groups..k..":"..tostring(v)..";\n" + end + for k,v in pairs(self.perm_rules) do + lists.perm = lists.perm..k..":"..tostring(v)..";\n" + end + return lists +end +function command_acl:export_perm_list() + local list = "" + for k,v in pairs(self.perm_rules) do + list = list..k..":"..tostring(v)..";\n" + end + return list +end +function command_acl:export_snapshot() + return { + user_rules = table_utils.deepcopy(self.user_rules), + group_rules = table_utils.deepcopy(self.group_rules), + perm_rules = table_utils.deepcopy(self.perm_rules) + } +end +function command_acl:import_snapshot(t) + self.user_rules = t.user_rules + self.group_rules = t.group_rules + self.perm_rules = t.perm_rules +end +function command_acl:import_perm_list() + list:gsub("(%w+):(%d+)",function(id,status) + self.perm_rules[id] = status + end) +end +return command_acl diff --git a/libraries/classes/command-class.lua b/libraries/classes/command-class.lua new file mode 100644 index 0000000..e69de29 diff --git a/libraries/classes/command-handler.lua b/libraries/classes/command-handler.lua index 3cf09ac..f5ce14e 100644 --- a/libraries/classes/command-handler.lua +++ b/libraries/classes/command-handler.lua @@ -14,6 +14,7 @@ function command_handler:__init(parent_server) self.prefixes = {} self.command_meta = { plugins = {}, + categories = {} } end function command_handler:add_prefix(prefix) @@ -43,6 +44,10 @@ function command_handler:add_command(command) if not self.command_meta.plugins[command.parent.name] then self.command_meta.plugins[command.parent.name] = {} end + if not self.command_meta.categories[command.options.category] then + self.command_meta.categories[command.options.category] = {} + end + table.insert(self.command_meta.categories[command.options.category],command.name) table.insert(self.command_meta.plugins[command.parent.name],command.name) return command end @@ -56,6 +61,10 @@ function command_handler:remove_command(command) if #self.command_meta.plugins[command.parent.name] == 0 then self.command_meta.plugins[command.parent.name] = nil end + table_utils.remove_value(self.command_meta.categories[command.options.category],command.name) + if #self.command_meta.categories[command.options.category] == 0 then + self.command_meta.categories[command.options.category] = nil + end self.command_pool[purified_name] = nil return true else @@ -81,7 +90,9 @@ function command_handler:get_commands_metadata() return table_utils.deepcopy(self.command_meta) end function command_handler:handle(message) - for name,command in pairs(self.command_pool) do + print("msg: "..tostring(message.content)) + print("author: "..tostring(message.author.name)) + for name,command in pairs(self.command_pool) do if command.options.regex then if message.content:match(command.options.regex) then command:exec(message) diff --git a/libraries/classes/command.lua b/libraries/classes/command.lua index c0f2012..3774875 100644 --- a/libraries/classes/command.lua +++ b/libraries/classes/command.lua @@ -5,13 +5,15 @@ local table_utils = import("table-utils") local class = import("classes.baseclass") local command = class("Command") local acl = import("classes.command-acl") +local discordia = import("discordia") function command:__init(name,callback) self.rules = acl() self.name = name - self.timer = os.time() + self.timer = discordia.Date():toMilliseconds() self.options = { allow_bots = false, --allow bots to execute the command typing_decorator = false, --set if the bot should be "typing" while the command executes + category = "None", --set category for the command prefix = true, --if true and if regex isn't enabled, check for prefix at the start. if not, don't check for prefix regex = false, --check if the message matches this regular expression (should be a string) no_parsing = false, --check if you want to disable the message argument parsing process @@ -141,7 +143,7 @@ function command:exec(message,args,opts) if status then self.callback(message,args,opts) else - msg:reply(err) + message:reply(err) end end end @@ -155,6 +157,7 @@ end function command:get_properties() return { name = self.name, + category = self.options.category, args = table_utils.deepcopy(self.args), help = table_utils.deepcopy(self.help), prefix = self.prefix diff --git a/libraries/classes/server-handler.lua b/libraries/classes/server-handler.lua index e6327ee..96cb266 100644 --- a/libraries/classes/server-handler.lua +++ b/libraries/classes/server-handler.lua @@ -42,8 +42,17 @@ function server_handler:__init(client,guild,options) self.default_plugins = options.default_plugins or {"test"} self.default_prefixes = options.default_prefixes or {"<@!"..self.client.user.id..">","&"} - self.config_path = self.config_path:gsub("%%id",self.id) + self.config_path = self.config_path:gsub("%id",self.id) self:load_config() + self.message_counter = 0 + if autosave then + self.client:on("messageCreate",function(msg) + self.message_counter = self.message_counter + 1 + if math.fmod(self.message_counter,self.autosave_frequency) == 0 then + self:save_config() + end + end) + end if not file.existsDir(self.config_path) then os.execute("mkdir -p "..self.config_path) end diff --git a/libraries/fake_message.lua b/libraries/fake_message.lua new file mode 100644 index 0000000..a9abd39 --- /dev/null +++ b/libraries/fake_message.lua @@ -0,0 +1,48 @@ +return function(message,overrides) + assert(type(message) == "table","table expected, got "..type(message)) + assert(type(overrides) == "table","table expected for arg#2, got "..type(overrides)) + local fake = {content = message.content, + author = message.author, + member = message.guild:getMember(message.author.id), + channel = message.channel, + client = message.client, + guild = message.guild, + delete = function() message:delete() end, + reply = function(thing,...) + message.channel:send(...) + end, + createdAt = message.createdAt, + _parent = message.parent, + parent = message.parent, + id = message.id, + attachment = message.attachment, + attachments = message.attachments, + cleanContent = message.cleanContent, + editedTimestamp = message.editedTimestamp, + embed = message.embed, + embeds = message.embeds, + link = message.link, + mentionedChannels = message.mentionedChannels, + mentionedEmojis = message.mentionedEmojis, + mentionedRoles = message.mentionedRoles, + mentionedUsers = message.mentionedUsers, + nonce = message.nonce, + oldContent = message.oldContent, + pinned = message.pinned, + reactions = message.reactions, + tts = message.tts, + type = message.type, + webhookId = message.webhookId, + addReaction = function(...) + message:reactionAdd(...) + end, + removeReaction = function(...) + message:reactionRemove(...) + end, + emulated = true + } + for k,v in pairs(overrides) do + fake[k] = v + end + return fake +end