Skip to content

Instantly share code, notes, and snippets.

@veeponym
Last active February 15, 2023 15:17
Show Gist options
  • Save veeponym/a64f3a20c40b3be7e65d63e78e39788a to your computer and use it in GitHub Desktop.
Save veeponym/a64f3a20c40b3be7e65d63e78e39788a to your computer and use it in GitHub Desktop.
-- Require the Discordia library
local discordia = require("discordia")
-- Create a new bot client
local client = discordia.Client()
-- Require the config.lua file that contains the bot's prefix, token, and owner ID
local config = require("config")
-- Require the cogmanager.lua file that contains the code for loading and managing cogs
local cogmanager = require("cogmanager")
-- Load the cogs from the cogs folder using the loadCogs function
cogmanager.loadCogs(client)
-- Register the slash commands using the bot:registerSlashCommands function
client:registerSlashCommands()
-- Add an event handler for the ready event
client:on("ready", function()
-- Print a message when the bot is ready
print("Logged in as " .. client.user.username)
end)
-- Add an event handler for the slashCommand event
client:on("slashCommand", function(interaction)
-- Get the name of the slash command
local name = interaction.data.name
-- Get the cog that corresponds to the slash command
local cog = cogmanager.getCog(name)
-- If the cog exists, execute its run function
if cog then
cog:run(interaction)
else
-- Otherwise, reply with an error message
interaction:reply("Sorry, I don't recognize that command.")
end
end)
-- Add an event handler for the messageCreate event
client:on("messageCreate", function(message)
-- Ignore messages from bots
if message.author.bot then return end
-- Get the content of the message
local content = message.content
-- Check if the message starts with the bot's prefix
if content:sub(1, #config.prefix) == config.prefix then
-- Get the command and the arguments from the message
local command, args = content:match("^"..config.prefix.."(%S+)%s*(.*)")
-- Get the cog that corresponds to the command
local cog = cogmanager.getCog(command)
-- If the cog exists, execute its run function
if cog then
cog:run(message, args)
else
-- Otherwise, reply with an error message
message:reply("Sorry, I don't recognize that command.")
end
end
end)
-- Start the bot using the bot:run function
client:run("Bot " .. config.token)
-- 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
-- Define an echo object
local echo = {}
-- Define a function to register the commands
function echo:registerCommands(client, slash)
-- Define a slash command for echo
slash:registerCommand('echo', 'Repeat a message', function(interaction)
-- Get the message from the interaction
local message = interaction.data.options[1].value
-- Reply with the message
interaction:reply(message)
end, {
-- Define an option for the message
{
name = 'message',
description = 'The message to repeat',
type = slash.enums.optionType.string,
required = true
}
})
end
-- Define a function to unregister the commands
function echo:unregisterCommands(client, slash)
-- Unregister the slash command for echo
slash:unregisterCommand('echo')
end
-- Return the echo object
return echo
-- Define a help object
local help = {}
-- Define a function to register the commands
function help:registerCommands(client, slash)
-- Define a slash command for help
slash:registerCommand('help', 'Get the list of commands', function(interaction)
-- Get the list of commands from the slash object
local commands = slash:getCommands()
-- Create a table to store the command names and descriptions
local commandList = {}
-- Loop through the commands
for _, command in ipairs(commands) do
-- Add the command name and description to the table
table.insert(commandList, command.name .. ': ' .. command.description)
end
-- Join the table elements with a newline
local commandString = table.concat(commandList, '\n')
-- Reply with the command list
interaction:reply('Here are the commands you can use:\n' .. commandString)
end)
end
-- Define a function to unregister the commands
function help:unregisterCommands(client, slash)
-- Unregister the slash command for help
slash:unregisterCommand('help')
end
-- Return the help object
return help
-- Require the http library
local http = require('http')
-- Require the json library
local json = require('json')
-- Define a joke object
local joke = {}
-- Define a function to register the commands
function joke:registerCommands(client, slash)
-- Define a slash command for joke
slash:registerCommand('joke', 'Tell a random joke', function(interaction)
-- Make a http request to the joke API
http.request('GET', 'https://official-joke-api.appspot.com/random_joke', {}, function(res)
-- Check if the status code is 200
if res.statusCode == 200 then
-- Read the response data
res:on('data', function(data)
-- Parse the data as JSON
local jokeData = json.parse(data)
-- Get the setup and punchline from the joke data
local setup = jokeData.setup
local punchline = jokeData.punchline
-- Reply with the joke
interaction:reply(setup .. '\n' .. punchline)
end)
else
-- Reply with an error message
interaction:reply('Sorry, I could not get a joke for you.')
end
end)
end)
end
-- Define a function to unregister the commands
function joke:unregisterCommands(client, slash)
-- Unregister the slash command for joke
slash:unregisterCommand('joke')
end
-- Return the joke object
return joke
-- Define a ping object
local ping = {}
-- Define a function to register the commands
function ping:registerCommands(client, slash)
-- Define a slash command for ping
slash:registerCommand('ping', 'Get the bot latency', function(interaction)
-- Get the timestamp of the interaction
local timestamp = interaction.data.timestamp
-- Get the current time
local now = os.time()
-- Calculate the latency
local latency = now - timestamp
-- Reply with the latency
interaction:reply('Pong! The latency is ' .. latency .. ' seconds.')
end)
end
-- Define a function to unregister the commands
function ping:unregisterCommands(client, slash)
-- Unregister the slash command for ping
slash:unregisterCommand('ping')
end
-- Return the ping object
return ping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment