From de6b9ec4e495755ca1a56cf44ff2490f18bd08c9 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Tue, 16 May 2023 13:24:42 -0400 Subject: [PATCH] Adding dot commands from nodbot --- dot-commands/_template | 10 ++++++ dot-commands/ping.js | 11 +++++++ dot-commands/servers.js | 17 ++++++++++ main.js | 21 ++++++++++++ modules/functions.js | 73 +++++++++++++++++++++++++++++++++++++++++ modules/utils.js | 2 +- 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 dot-commands/_template create mode 100644 dot-commands/ping.js create mode 100644 dot-commands/servers.js diff --git a/dot-commands/_template b/dot-commands/_template new file mode 100644 index 0000000..7b8f500 --- /dev/null +++ b/dot-commands/_template @@ -0,0 +1,10 @@ +const fn = require('../modules/functions.js'); + +module.exports = { + name: "", + description: "", + usage: "", + execute(message, commandData) { + // Code here... + } +} \ No newline at end of file diff --git a/dot-commands/ping.js b/dot-commands/ping.js new file mode 100644 index 0000000..d18ae1c --- /dev/null +++ b/dot-commands/ping.js @@ -0,0 +1,11 @@ +const fn = require('../modules/functions.js'); + +module.exports = { + name: "ping", + description: "pong", + usage: "ping pong", + async execute(message, commandData) { + // Code here... + await message.reply("Pong!"); + } +} \ No newline at end of file diff --git a/dot-commands/servers.js b/dot-commands/servers.js new file mode 100644 index 0000000..d199e3e --- /dev/null +++ b/dot-commands/servers.js @@ -0,0 +1,17 @@ +const fn = require('../modules/functions.js'); + +module.exports = { + name: "servers", + description: "Get a list of servers the bot is in", + usage: ".servers", + async execute(message, commandData) { + let servers = []; + const count = JSON.stringify(message.client.guilds.cache.size); + servers.push("I'm currently in " + count + " servers."); + const guilds = await message.client.guilds.cache; + await guilds.each(g => { + servers.push(g.name + "," + g.ownerId); + }); + await message.reply(servers.join("\n")); + } +} \ No newline at end of file diff --git a/main.js b/main.js index 1e091c9..e298c79 100755 --- a/main.js +++ b/main.js @@ -29,6 +29,8 @@ const isDev = process.env.DEBUG; client.once('ready', async () => { fn.collectionBuilders.slashCommands(client); + fn.collectionBuilders.dotCommands(client); + fn.collectionBuilders.setvalidCommands(client); await fn.collectionBuilders.guildInfos(client); await fn.collectionBuilders.messageCollectors(client); // checkRateLimits(); @@ -92,6 +94,25 @@ client.on('messageUpdate', async (oldMessage, message) => { client.on('messageCreate', async message => { await fn.messages.updateHandler(message).catch(e => console.error(e)); + + // Dot Command Handling + // Some basic checking to prevent running unnecessary code + if (message.author.bot) return; + + // Break the message down into its components and analyze it + const commandData = fn.dotCommands.getCommandData(message); + if (isDev) console.log(console.log(commandData)); + + if (commandData.isValid && commandData.isCommand && (message.author.id == process.env.ownerId)) { + try { + client.dotCommands.get(commandData.command).execute(message, commandData); + } + catch (error) { + console.error(error); + message.reply('There was an error trying to execute that command.'); + } + } + return; }); async function checkRateLimits(hi) { diff --git a/modules/functions.js b/modules/functions.js index 7c32ae0..ed7a1d2 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -17,6 +17,7 @@ const { GuildInfo } = require('./CustomClasses'); const config = require('../data/config.json'); const strings = require('../data/strings.json'); const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js')); +const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js')); const dbfn = require('./dbfn.js'); const { finished } = require('stream'); @@ -35,6 +36,44 @@ const functions = { } if (isDev) console.log('Slash Commands Collection Built'); }, + dotCommands(client) { + // If the dotCommands collection doesn't exist already, create it + if (!client.dotCommands) client.dotCommands = new Discord.Collection(); + // Empty the dotcommands collection + client.dotCommands.clear(); + // Iterate over each file within ./dot-commands that ends with .js + for (const file of dotCommandFiles) { + // Pull the file into a temporary variable + const dotCommand = require(`../dot-commands/${file}`); + // Create a new entry in the collection with the key of the command name, value of the command itself + client.dotCommands.set(dotCommand.name, dotCommand); + // Old code from NodBot to implement aliases for dot commands. Unused as of 5/16/23 + // if (Array.isArray(dotCommand.alias)) { + // dotCommand.alias.forEach(element => { + // client.dotCommands.set(element, dotCommand); + // }); + // } else if (dotCommand.alias != undefined) { + // client.dotCommands.set(dotCommand.alias, dotCommand); + // } + } + if (isDev) console.log('Dot Commands Collection Built'); + }, + setvalidCommands(client) { + // Iterate over every entry in the dotCommands collection + for (const entry of client.dotCommands.map(command => command)) { + // Add the command's name to the valid commands list for validation later + config.validCommands.push(entry.name); + // Old code from NodBot to implement aliases for dot commands. Unused as of 5/16/23 + // if (Array.isArray(entry.alias)) { + // entry.alias.forEach(element => { + // config.validCommands.push(element); + // }); + // } else if (entry.alias != undefined) { + // config.validCommands.push(entry.alias); + // } + } + if (isDev) console.log(`Valid Commands Added to Config\n${config.validCommands}`); + }, async guildInfos(client) { const guildInfos = await dbfn.getAllGuildInfos(); if (!client.guildInfos) client.guildInfos = new Discord.Collection(); @@ -194,6 +233,40 @@ const functions = { return messageContents; } }, + dotCommands: { + getCommandData(message) { + const commandData = {}; + // Split the message content at the final instance of a period + const finalPeriod = message.content.lastIndexOf('.'); + if(isDev) console.log(message.content); + // If the final period is the last character, or doesn't exist + if (finalPeriod < 0) { + if (isDev) console.log(finalPeriod); + commandData.isCommand = false; + return commandData; + } + commandData.isCommand = true; + // Get the first part of the message, everything leading up to the final period + commandData.args = message.content.slice(0,finalPeriod).toLowerCase(); + // Get the last part of the message, everything after the final period + commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase(); + commandData.author = `${message.author.username}#${message.author.discriminator}`; + return this.checkCommand(commandData); + }, + checkCommand(commandData) { + if (commandData.isCommand) { + const validCommands = require('./config.json').validCommands; + commandData.isValid = validCommands.includes(commandData.command); + // Add exceptions for messages that contain only a link + if (commandData.args.startsWith('http')) commandData.isValid = false; + } + else { + commandData.isValid = false; + console.error('Somehow a non-command made it to checkCommands()'); + } + return commandData; + } + }, rankings: { parse(interaction, guildInfo) { return new Promise((resolve, reject) => { diff --git a/modules/utils.js b/modules/utils.js index 5f20230..d9c94c7 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -30,7 +30,7 @@ client.once('ready', async () => { console.log("I'm currently in " + count + " servers."); const guilds = client.guilds.cache; guilds.each(g => { - console.log(g.name); + console.log(g.name + "," + g.ownerId); }); process.exit(); });