Skip to content

Instantly share code, notes, and snippets.

@veeponym
Created February 15, 2023 04:47
Show Gist options
  • Save veeponym/3df4319f1e386524226bcd8df929cf3b to your computer and use it in GitHub Desktop.
Save veeponym/3df4319f1e386524226bcd8df929cf3b to your computer and use it in GitHub Desktop.
-- Require the discordia library
local discordia = require('discordia')
-- Create a new class called CogManager
local CogManager = discordia.class('CogManager')
-- Define a constructor for the CogManager class
function CogManager:__init(__dirname)
-- Set the __dirname property to the argument
self.__dirname = __dirname
-- Create a table to store the cogs
self.cogs = {}
-- Create a table to store the cog commands
self.cogCommands = {}
end
-- Define a method to load a cog from a file
function CogManager:loadCog(filename)
-- Require the cog file and pass the CogManager object
local cog = require(self.__dirname .. 'cogs/' .. filename)(self)
-- Check if the cog is valid
if cog and cog.name and cog.description and cog.commands then
-- Add the cog to the cogs table
self.cogs[cog.name] = cog
-- Loop through the cog commands
for name, command in pairs(cog.commands) do
-- Check if the command is valid
if command and command.description and command.execute then
-- Add the command to the cogCommands table
self.cogCommands[name] = command
end
end
-- Return true to indicate success
return true
else
-- Return false to indicate failure
return false
end
end
-- Define a method to load all the cogs from the cogs folder
function CogManager:loadCogs()
-- Get the file system module
local fs = require('fs')
-- Get the list of files in the cogs folder
local files = fs.readdirSync(self.__dirname .. 'cogs')
-- Loop through the files
for _, file in ipairs(files) do
-- Check if the file is a lua file
if file:match('%.lua$') then
-- Load the cog from the file
local success = self:loadCog(file)
-- Log a message depending on the result
if success then
print('Loaded cog: ' .. file)
else
print('Failed to load cog: ' .. file)
end
end
end
end
-- Define a method to check if a command is a cog command
function CogManager:isCogCommand(name)
-- Return true if the name is in the cogCommands table, false otherwise
return self.cogCommands[name] ~= nil
end
-- Define a method to execute a cog command
function CogManager:executeCogCommand(name, message, args)
-- Get the command from the cogCommands table
local command = self.cogCommands[name]
-- Check if the command exists
if command then
-- Call the execute function of the command with the message and args
command:execute(message, args)
end
end
-- Return the CogManager class as a module
return CogManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment