From a42ad16135a6ac5debdc8a8351da314a0b164cf2 Mon Sep 17 00:00:00 2001 From: tommy Date: Fri, 12 Jul 2024 08:32:55 -0400 Subject: [PATCH] make sub command handler for easier sub commands --- src/modules/SubCommandHandler.lua | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/modules/SubCommandHandler.lua diff --git a/src/modules/SubCommandHandler.lua b/src/modules/SubCommandHandler.lua new file mode 100644 index 0000000..d743463 --- /dev/null +++ b/src/modules/SubCommandHandler.lua @@ -0,0 +1,41 @@ +--I LOVE USELESS CLASSES! +local Discordia = require('discordia') + +local ApplicationCommandOptionTypes = Discordia.enums.appCommandOptionType + +local SubCommandHandler = {} +SubCommandHandler.__index = SubCommandHandler + +function SubCommandHandler.new() + local self = setmetatable({}, SubCommandHandler) + + self.SubCommandCallbacks = {} + + return self +end + +function SubCommandHandler:AddSubCommand(SubCommandName, SubCommandCallback) + if self.SubCommandCallbacks[SubCommandName] then + return print('no can do') + end + + self.SubCommandCallbacks[SubCommandName] = SubCommandCallback + + return self +end + +function SubCommandHandler:GetMainCommandCallback() + return function(Interaction, Command, Args) + local SubCommandOption = Command.options[1] + if SubCommandOption.type == ApplicationCommandOptionTypes.subCommand then + local SubCommandName = SubCommandOption.name + local SubCommandCallback = self.SubCommandCallbacks[SubCommandName] + local SubCommandArgs = Args[SubCommandName] + if SubCommandCallback then + SubCommandCallback(Interaction, Command, SubCommandArgs) + end + end + end +end + +return SubCommandHandler \ No newline at end of file