make sub command handler for easier sub commands

This commit is contained in:
tommy 2024-07-12 08:32:55 -04:00
parent a479e88823
commit a42ad16135
Signed by: tommy
GPG Key ID: 894E02570CE82334

View File

@ -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