From dfc19783688ca6688f37a3254715c5090f94cc46 Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Fri, 9 Dec 2022 23:40:41 +1100 Subject: [PATCH 1/4] oopsie comma was missing --- botconfig.json.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/botconfig.json.example b/botconfig.json.example index bcb6497..1fb7e72 100644 --- a/botconfig.json.example +++ b/botconfig.json.example @@ -18,7 +18,7 @@ "database": "", "password": "", "port": 0000 - } + }, "emojis": { "success": "<:success:466995111885144095>", From b94fc32f46d5ad6a8f0a786feecb884a7ed8abce Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Fri, 9 Dec 2022 23:41:29 +1100 Subject: [PATCH 2/4] file layout changes + interaction passoff --- bot/event_modules/eventHandler.js | 68 ++++++++++ .../interactionCreate/messageHandler.js | 121 ++++++++++++++++++ bot/index.js | 18 ++- 3 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 bot/event_modules/eventHandler.js create mode 100644 bot/event_modules/interactionCreate/messageHandler.js diff --git a/bot/event_modules/eventHandler.js b/bot/event_modules/eventHandler.js new file mode 100644 index 0000000..a943203 --- /dev/null +++ b/bot/event_modules/eventHandler.js @@ -0,0 +1,68 @@ +/* eslint-disable indent */ + +class EventHandler { + constructor (client) { + this.client = client; + } + + handle (wsEvent, param_1, param_2) { + switch (wsEvent) { + case 'ready': { + const readyModules = this.client.eventModules.filter(module => module.wsEvent === 'ready'); + readyModules.forEach(module => module.run(this.client)); + break; + } + + // param_1 - error message + case 'error': { + const errorModules = this.client.eventModules.filter(module => module.wsEvent === 'error'); + errorModules.forEach(module => module.run(this.client, param_1)); + break; + } + + // param_1 - message object + case 'messageCreate': { + const mCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'messageCreate'); + mCreateModules.forEach(module => module.run(this.client, param_1)); + break; + } + + // param_1 - guild object + case 'guildCreate': { + const gCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'guildCreate'); + gCreateModules.forEach(module => module.run(this.client, param_1)); + break; + } + + // param_1 - guild object + case 'guildDelete': { + const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent === 'guildDelete'); + gDeleteModules.forEach(module => module.run(this.client, param_1)); + break; + } + + // param_1 - guild object | param_2 - member object + case 'guildMemberAdd': { + const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberAdd'); + gMemberAddModules.forEach(module => module.run(this.client, param_1, param_2)); + break; + } + + // param_1 - guild object | param_2 - member object + case 'guildMemberRemove': { + const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberRemove'); + gMemberRemoveModules.forEach(module => module.run(this.client, param_1, param_2)); + break; + } + + // param_1 - old voice state | param_2 - new voice state + case 'voiceStateUpdate': { + const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent === 'voiceStateUpdate'); + vStateUpdateModules.forEach(module => module.run(this.client)); + break; + } + } + } +} + +module.exports = EventHandler; \ No newline at end of file diff --git a/bot/event_modules/interactionCreate/messageHandler.js b/bot/event_modules/interactionCreate/messageHandler.js new file mode 100644 index 0000000..29da3c1 --- /dev/null +++ b/bot/event_modules/interactionCreate/messageHandler.js @@ -0,0 +1,121 @@ +class MessageHandler { + constructor (client) { + this.client = client; + } + + async handle (message) { + // Ignore messages from bots, and messages in DM's + if (message.author.bot) return; + if (!message.guild) return; + + // Request all the data we need from the database + const data = {}; + data.user = await this.client.db.getUser(message.author.id); + data.guild = await this.client.db.getGuild(message.guild.id); + data.member = await this.client.db.getMember(message.guild.id, message.author.id); + + // Ignore users on the guild blocklist + if (data.guild.blocklist.includes(message.author.id)) return; + + // If a user pings Woomy, respond to them with the prefixes they can use + if (message.content === `<@${this.client.user.id}>` || message.content === `<@!${this.client.user.id}>`) { + return message.channel.send( + `Hi! The prefix for this server is \`${data.guild.prefix}\`, and your personal prefix is \`${data.user.prefix}\`. You can also ping me ^-^` + ); + } + + // All the prefixes Woomy will respond to + const prefixes = [ + data.user.prefix, + data.guild.prefix, + `<@${this.client.user.id}> `, + `<@!${this.client.user.id}> ` + ]; + + let prefix; + + // Check the message content to see if it starts with one of our prefixes + for (const thisPrefix of prefixes) { + if (message.content.startsWith(thisPrefix)) { + prefix = thisPrefix; + break; + } + } + + // Ignore the message if it doesn't start with a valid prefix + if (!prefix) return; + + // Save prefix so we can use it later (mostly for help command) + if (prefix === `<@${this.client.user.id}> ` || prefix === `<@!${this.client.user.id}> `) { + message.prefix = '@Woomy '; + } else (message.prefix = prefix); + + // Turn the message content into an array (excluding the prefix) + const args = message.content.slice(prefix.length).trim().split(/ +/g); + + // Find the command + const commandName = args.shift().toLowerCase(); + const command = this.client.commands.get(commandName) || this.client.commands.get(this.client.aliases.get(commandName)); + + // Return if a command (or its aliases) are not found + if (!command) return; + + // Both of these blocks check if the command is disabled/in a disabled category + if (data.guild.disabledcommands.includes(command.name)) return message.channel.send( + this.client.config.emojis.permError + ' This command has been disabled by a server administrator.' + ); + + if (data.guild.disabledcategories.includes(command.category)) return message.channel.send( + this.client.config.emojis.permError + ' The category this command is apart of has been disabled by a server administrator.' + ); + + // Both of these blocks check the permissions of the user, and reply with missing perms if any are found + const missingUserPerms = this.client.functions.checkPermissions(message.channel, message.author.id, command.userPerms); + if (missingUserPerms) return message.channel.send( + `${this.client.config.emojis.permError} You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\`` + ); + + const missingBotPerms = this.client.functions.checkPermissions(message.channel, this.client.user.id, command.botPerms); + if (missingBotPerms) return message.channel.send( + `${this.client.config.emojis.permError} I can't run this command because I lack these permissions: \`${missingBotPerms.join('`, `')}\`` + ); + + // Return if the command is disabled globally + if (command.enabled === false) return message.channel.send( + this.client.config.emojis.permError + ' This command has been disabled by my developers.' + ); + + // Return if the command is restricted to developers (and the user is not a developer) + if (command.devOnly === true && this.client.config.ownerIDs.includes(message.author.id) !== true) { + return message.channel.send( + `${this.client.config.emojis.permError} ${message.author.username} is not in the sudoers file. This incident will be reported.` + ); + } + + // Cooldown + if (this.client.cooldowns.get(command.name).has(message.author.id)) { + const timestamp = this.client.cooldowns.get(command.name).get(message.author.id); + const currentTime = Date.now(); + const cooldown = command.cooldown / 1000; + const timePassed = Math.floor((currentTime - timestamp) / 1000); + return message.channel.send( + `${this.client.config.emojis.wait} ${message.author.mention}, you need to wait ${cooldown - timePassed} seconds before using this command again.` + ); + } else { + this.client.cooldowns.get(command.name).set(message.author.id, new Date()); + setTimeout(() => { + this.client.cooldowns.get(command.name).delete(message.author.id); + }, this.client.commands.get(command.name).cooldown); + } + + try { + command.run(this.client, message, args, data); + this.client.logger.command(`Ran ${command.name}`); + } catch (error) { + this.client.logger.error('COMMAND_EXECUTION_ERROR', `${command.name}: ${error.stack}`); + message.channel.send(`${this.client.config.emojis.botError} An error occured when I was trying to run this command. I've sent through the details of the error to my developers.`); + } + } +} + +module.exports = MessageHandler; \ No newline at end of file diff --git a/bot/index.js b/bot/index.js index 2df2429..56670d1 100644 --- a/bot/index.js +++ b/bot/index.js @@ -3,10 +3,10 @@ const Discord = require('discord.js'); const CommandLoader = require('./util/commandLoader'); const EventLoader = require('./util/eventLoader'); -const EventHandler = require('./util/handlers/eventHandler'); -const MessageHandler = require('./util/handlers/messageHandler'); +const EventHandler = require('./event_modules/eventHandler'); +const MessageHandler = require('./event_modules/interactionCreate/messageHandler'); const Functions = require('./util/functions'); -// const Database = require('./util/database'); +const Database = require('./util/database'); const Logger = require('./util/logger'); const sentry = require('@sentry/node'); const config = require('../botconfig.json'); @@ -23,7 +23,7 @@ class WoomyClient extends Discord.Client { // Essential modules this.logger = Logger; this.MessageEmbed = Discord.MessageEmbed; - //this.db = new Database(this); + this.db = new Database(this); this.functions = new Functions(this); this.commandLoader = new CommandLoader(this); this.eventLoader = new EventLoader(this); @@ -41,7 +41,8 @@ class WoomyClient extends Discord.Client { createEventListeners () { this.on('ready', this.runReadyModules); this.on('error', this.runErrorModules); - this.on('messageCreate', this.runMessageCreateModules); + this.on('interactionCreate', this.runInteractionModules); + /// this.on('messageCreate', this.runMessageCreateModules); this.on('guildCreate', this.runGuildCreateModules); this.on('guildDelete', this.runGuildDeleteModules); this.on('guildMemberAdd', this.runGuildMemberAddModules); @@ -67,8 +68,11 @@ class WoomyClient extends Discord.Client { this.mainEventListener('error', error); } + runInteractionCreateModules (interaction) { + this.mainEventListener('interactionCreate', interaction); + } + runMessageCreateModules (message) { - this.messageHandler.handle(message); this.mainEventListener('messageCreate', message); } @@ -142,7 +146,7 @@ process.on('unhandledRejection', err => { client.logger.error('UNHANDLED_PROMISE_ERROR', err.stack); }); -// Shut down gracefully when SIGINT is recieved +// Shut down gracefully when SIGINT is received process.on('SIGINT', () => { client.functions.shutdown(); }); \ No newline at end of file From 7e623c36fd0cc94f214859b72c59c8866dfecb53 Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Fri, 9 Dec 2022 23:41:47 +1100 Subject: [PATCH 3/4] oopsie cant spell --- bot/commands/Developer/restart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/commands/Developer/restart.js b/bot/commands/Developer/restart.js index 54d55dd..8b9bb8e 100644 --- a/bot/commands/Developer/restart.js +++ b/bot/commands/Developer/restart.js @@ -20,7 +20,7 @@ module.exports = class { } run (client, message, args, data) { //eslint-disable-line no-unused-vars - client.logger.success('RESTART', 'Restart command recieved. ' + exitQuotes.random()); + client.logger.success('RESTART', 'Restart command received. ' + exitQuotes.random()); client.disconnect(); client.functions.wait(); From dfc1674bad56bbb6c9072369db071f06b5586b8f Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Fri, 9 Dec 2022 23:42:09 +1100 Subject: [PATCH 4/4] i cant remember what half of these were even for --- bot/util/dbvalidator.js | 0 bot/util/handlers/botListHandler.js | 0 bot/util/handlers/eventHandler.js | 68 ---------------- bot/util/handlers/messageHandler.js | 121 ---------------------------- 4 files changed, 189 deletions(-) delete mode 100644 bot/util/dbvalidator.js delete mode 100644 bot/util/handlers/botListHandler.js delete mode 100644 bot/util/handlers/eventHandler.js delete mode 100644 bot/util/handlers/messageHandler.js diff --git a/bot/util/dbvalidator.js b/bot/util/dbvalidator.js deleted file mode 100644 index e69de29..0000000 diff --git a/bot/util/handlers/botListHandler.js b/bot/util/handlers/botListHandler.js deleted file mode 100644 index e69de29..0000000 diff --git a/bot/util/handlers/eventHandler.js b/bot/util/handlers/eventHandler.js deleted file mode 100644 index a943203..0000000 --- a/bot/util/handlers/eventHandler.js +++ /dev/null @@ -1,68 +0,0 @@ -/* eslint-disable indent */ - -class EventHandler { - constructor (client) { - this.client = client; - } - - handle (wsEvent, param_1, param_2) { - switch (wsEvent) { - case 'ready': { - const readyModules = this.client.eventModules.filter(module => module.wsEvent === 'ready'); - readyModules.forEach(module => module.run(this.client)); - break; - } - - // param_1 - error message - case 'error': { - const errorModules = this.client.eventModules.filter(module => module.wsEvent === 'error'); - errorModules.forEach(module => module.run(this.client, param_1)); - break; - } - - // param_1 - message object - case 'messageCreate': { - const mCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'messageCreate'); - mCreateModules.forEach(module => module.run(this.client, param_1)); - break; - } - - // param_1 - guild object - case 'guildCreate': { - const gCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'guildCreate'); - gCreateModules.forEach(module => module.run(this.client, param_1)); - break; - } - - // param_1 - guild object - case 'guildDelete': { - const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent === 'guildDelete'); - gDeleteModules.forEach(module => module.run(this.client, param_1)); - break; - } - - // param_1 - guild object | param_2 - member object - case 'guildMemberAdd': { - const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberAdd'); - gMemberAddModules.forEach(module => module.run(this.client, param_1, param_2)); - break; - } - - // param_1 - guild object | param_2 - member object - case 'guildMemberRemove': { - const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberRemove'); - gMemberRemoveModules.forEach(module => module.run(this.client, param_1, param_2)); - break; - } - - // param_1 - old voice state | param_2 - new voice state - case 'voiceStateUpdate': { - const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent === 'voiceStateUpdate'); - vStateUpdateModules.forEach(module => module.run(this.client)); - break; - } - } - } -} - -module.exports = EventHandler; \ No newline at end of file diff --git a/bot/util/handlers/messageHandler.js b/bot/util/handlers/messageHandler.js deleted file mode 100644 index 29da3c1..0000000 --- a/bot/util/handlers/messageHandler.js +++ /dev/null @@ -1,121 +0,0 @@ -class MessageHandler { - constructor (client) { - this.client = client; - } - - async handle (message) { - // Ignore messages from bots, and messages in DM's - if (message.author.bot) return; - if (!message.guild) return; - - // Request all the data we need from the database - const data = {}; - data.user = await this.client.db.getUser(message.author.id); - data.guild = await this.client.db.getGuild(message.guild.id); - data.member = await this.client.db.getMember(message.guild.id, message.author.id); - - // Ignore users on the guild blocklist - if (data.guild.blocklist.includes(message.author.id)) return; - - // If a user pings Woomy, respond to them with the prefixes they can use - if (message.content === `<@${this.client.user.id}>` || message.content === `<@!${this.client.user.id}>`) { - return message.channel.send( - `Hi! The prefix for this server is \`${data.guild.prefix}\`, and your personal prefix is \`${data.user.prefix}\`. You can also ping me ^-^` - ); - } - - // All the prefixes Woomy will respond to - const prefixes = [ - data.user.prefix, - data.guild.prefix, - `<@${this.client.user.id}> `, - `<@!${this.client.user.id}> ` - ]; - - let prefix; - - // Check the message content to see if it starts with one of our prefixes - for (const thisPrefix of prefixes) { - if (message.content.startsWith(thisPrefix)) { - prefix = thisPrefix; - break; - } - } - - // Ignore the message if it doesn't start with a valid prefix - if (!prefix) return; - - // Save prefix so we can use it later (mostly for help command) - if (prefix === `<@${this.client.user.id}> ` || prefix === `<@!${this.client.user.id}> `) { - message.prefix = '@Woomy '; - } else (message.prefix = prefix); - - // Turn the message content into an array (excluding the prefix) - const args = message.content.slice(prefix.length).trim().split(/ +/g); - - // Find the command - const commandName = args.shift().toLowerCase(); - const command = this.client.commands.get(commandName) || this.client.commands.get(this.client.aliases.get(commandName)); - - // Return if a command (or its aliases) are not found - if (!command) return; - - // Both of these blocks check if the command is disabled/in a disabled category - if (data.guild.disabledcommands.includes(command.name)) return message.channel.send( - this.client.config.emojis.permError + ' This command has been disabled by a server administrator.' - ); - - if (data.guild.disabledcategories.includes(command.category)) return message.channel.send( - this.client.config.emojis.permError + ' The category this command is apart of has been disabled by a server administrator.' - ); - - // Both of these blocks check the permissions of the user, and reply with missing perms if any are found - const missingUserPerms = this.client.functions.checkPermissions(message.channel, message.author.id, command.userPerms); - if (missingUserPerms) return message.channel.send( - `${this.client.config.emojis.permError} You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\`` - ); - - const missingBotPerms = this.client.functions.checkPermissions(message.channel, this.client.user.id, command.botPerms); - if (missingBotPerms) return message.channel.send( - `${this.client.config.emojis.permError} I can't run this command because I lack these permissions: \`${missingBotPerms.join('`, `')}\`` - ); - - // Return if the command is disabled globally - if (command.enabled === false) return message.channel.send( - this.client.config.emojis.permError + ' This command has been disabled by my developers.' - ); - - // Return if the command is restricted to developers (and the user is not a developer) - if (command.devOnly === true && this.client.config.ownerIDs.includes(message.author.id) !== true) { - return message.channel.send( - `${this.client.config.emojis.permError} ${message.author.username} is not in the sudoers file. This incident will be reported.` - ); - } - - // Cooldown - if (this.client.cooldowns.get(command.name).has(message.author.id)) { - const timestamp = this.client.cooldowns.get(command.name).get(message.author.id); - const currentTime = Date.now(); - const cooldown = command.cooldown / 1000; - const timePassed = Math.floor((currentTime - timestamp) / 1000); - return message.channel.send( - `${this.client.config.emojis.wait} ${message.author.mention}, you need to wait ${cooldown - timePassed} seconds before using this command again.` - ); - } else { - this.client.cooldowns.get(command.name).set(message.author.id, new Date()); - setTimeout(() => { - this.client.cooldowns.get(command.name).delete(message.author.id); - }, this.client.commands.get(command.name).cooldown); - } - - try { - command.run(this.client, message, args, data); - this.client.logger.command(`Ran ${command.name}`); - } catch (error) { - this.client.logger.error('COMMAND_EXECUTION_ERROR', `${command.name}: ${error.stack}`); - message.channel.send(`${this.client.config.emojis.botError} An error occured when I was trying to run this command. I've sent through the details of the error to my developers.`); - } - } -} - -module.exports = MessageHandler; \ No newline at end of file