From 584a073ee07e133ee39d67ba928d5b4af4c7800e Mon Sep 17 00:00:00 2001 From: Foxinatel Date: Sat, 14 May 2022 09:57:05 +0100 Subject: [PATCH] Offload event handlers to their own seperate files --- events/error.js | 5 ++ events/interactionCreate.js | 25 +++++++++ events/messageCreate.js | 30 +++++++++++ events/ready.js | 58 ++++++++++++++++++++ events/warn.js | 5 ++ start.js | 103 +++++------------------------------- 6 files changed, 137 insertions(+), 89 deletions(-) create mode 100644 events/error.js create mode 100644 events/interactionCreate.js create mode 100644 events/messageCreate.js create mode 100644 events/ready.js create mode 100644 events/warn.js diff --git a/events/error.js b/events/error.js new file mode 100644 index 0000000..c09e250 --- /dev/null +++ b/events/error.js @@ -0,0 +1,5 @@ +module.exports = { + event: require('path').parse(__filename).name, + once: false, + listener: () => e => { console.log(`[ERROR] ${e}`); } +}; diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..81241f4 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,25 @@ +module.exports = { + event: require('path').parse(__filename).name, + once: false, + listener: (client, config) => + async (interaction) => { + if (interaction.isApplicationCommand()) { + try { + await client.commands.get(interaction.commandName)?.parseInteraction(client, config, interaction); + } catch (error) { + console.error(error); + interaction.channel.send({ + embeds: [{ + title: '<:AnitroxError:809651936563429416> **Something went wrong!**', + description: error.stack, + color: 13632027, + footer: { + icon_url: interaction.user.displayAvatarURL(), + text: config.footerTxt + } + }] + }); + } + } + } +}; diff --git a/events/messageCreate.js b/events/messageCreate.js new file mode 100644 index 0000000..a8cdc43 --- /dev/null +++ b/events/messageCreate.js @@ -0,0 +1,30 @@ +module.exports = { + event: require('path').parse(__filename).name, + once: false, + listener: (client, config) => + async (message) => { + if (!message.content.startsWith(config.prefix) || message.author.bot) return; + + const args = message.content.slice(config.prefix.length).split(/\s+/); + const command = args.shift()?.toLowerCase() ?? ''; + + if (!client.commands.has(command)) return; + + try { + await client.commands.get(command)?.parseMessage(client, config, message, args); + } catch (error) { + console.error(error); + message.channel.send({ + embeds: [{ + title: '<:AnitroxError:809651936563429416> **Something went wrong!**', + description: error.stack, + color: 13632027, + footer: { + icon_url: message.author.displayAvatarURL(), + text: config.footerTxt + } + }] + }); + } + } +}; diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..fac68ac --- /dev/null +++ b/events/ready.js @@ -0,0 +1,58 @@ +const { Collection } = require('discord.js'); + +module.exports = { + event: require('path').parse(__filename).name, + once: true, + listener: (client, config) => + async () => { + const sandboxSettings = config.sandbox; + const localCommands = client.guilds.cache.get(sandboxSettings.id)?.commands; + const globalCommands = client.application?.commands; + let existingLocal = await localCommands?.fetch(); + let existingGlobal = await globalCommands?.fetch(); + + if (sandboxSettings.enabled) { + if (sandboxSettings.refreshLocal && localCommands) { + console.log('deleting previous local commands'); + existingLocal?.forEach(async (x) => { + await localCommands?.delete(x); + }); + existingLocal = new Collection(); + } + + if (sandboxSettings.refreshGlobal) { + console.log('deleting previous global commands'); + existingGlobal?.forEach(async x => { + await client.application?.commands.delete(x); + }); + existingGlobal = new Collection(); + } + } + + client.commands.forEach(async (command) => { + if (sandboxSettings.enabled && !existingLocal?.map(x => x.name).includes(command.name)) { + await localCommands?.create(command); + // console.log(`created new local command ${command.name}`); + } + if (!existingGlobal?.map(x => x.name).includes(command.name)) { + await globalCommands?.create(command); + // console.log(`created new global command ${command.name}`); + } + }); + + console.clear(); + console.log(' ___ _ __ '); + console.log(' / | ____ (_) /__________ _ __'); + console.log(' / /| | / __ \\/ / __/ ___/ __ \\| |/_/'); + console.log(' / ___ |/ / / / / /_/ / / /_/ /> < '); + console.log('/_/ |_/_/ /_/_/\\__/_/ \\____/_/|_| '); + console.log(`${config.release}, ${config.build}`); + console.log('Bot online. | Anitrox by IDeletedSystem64 | ALL MY CODE KEEPS BLOWING UP!'); + // Statuses + setInterval(async () => { + // Picks a status from the config file + const index = Math.floor(Math.random() * config.statuses.length); + await client.user?.setActivity(config.statuses[index]); + }, 20000); + } +}; diff --git a/events/warn.js b/events/warn.js new file mode 100644 index 0000000..dcefdf5 --- /dev/null +++ b/events/warn.js @@ -0,0 +1,5 @@ +module.exports = { + event: require('path').parse(__filename).name, + once: false, + listener: () => e => (`[WARN] ${e}`) +}; diff --git a/start.js b/start.js index 768ee3d..6d2403b 100755 --- a/start.js +++ b/start.js @@ -3,14 +3,24 @@ const fs = require('fs'); const Discord = require('discord.js'); const config = require('./config.json'); + console.log('Starting!'); const client = new Discord.Client({ intents: config.intents.map(intent => eval(`Discord.Intents.FLAGS.${intent}`)) }); client.commands = new Discord.Collection(); -fs.readdirSync('./commands').filter(file => file.endsWith('.js')).forEach(file => { - const command = require(`./commands/${file}`); - client.commands.set(command.name, command); -}); +fs.readdirSync('./commands') + .filter(file => file.endsWith('.js')) + .forEach(file => { + const command = require(`./commands/${file}`); + client.commands.set(command.name, command); + }); + +fs.readdirSync('./events') + .filter(file => file.endsWith('.js')) + .map(file => require(`./events/${file}`)) + .forEach(({ once, event, listener }) => { + client[once ? 'once' : 'on'](event, listener(client, config)); + }); client.generateErrorMessage = (errorMsg, avatarURL) => ({ embeds: [{ @@ -29,89 +39,4 @@ client.generateErrorMessage = (errorMsg, avatarURL) => ({ }] }); -client.on('error', (e) => console.log(`[ERROR] ${e}`)); -client.on('warn', (e) => (`[WARN] ${e}`)); -client.once('ready', async () => { - const sandboxSettings = config.sandbox; - const localCommands = client.guilds.cache.get(sandboxSettings.id)?.commands; - const globalCommands = client.application.commands; - let existingLocal = await localCommands?.fetch(); - let existingGlobal = await globalCommands?.fetch(); - - if (sandboxSettings.enabled) { - if (sandboxSettings.refreshLocal) { - console.log('deleting previous local commands'); - existingLocal?.forEach(async x => { - await localCommands?.delete(x); - }); - existingLocal = new Discord.Collection(); - } - - if (sandboxSettings.refreshGlobal) { - console.log('deleting previous global commands'); - existingGlobal?.forEach(async x => { - await client.application?.commands.delete(x); - }); - existingGlobal = new Discord.Collection(); - } - } - - client.commands.forEach(async command => { - if (sandboxSettings.enabled && !existingLocal?.map(x => x.name).includes(command.name)) { - await localCommands?.create(command); - // console.log(`created new local command ${command.name}`); - } - if (!existingGlobal?.map(x => x.name).includes(command.name)) { - await globalCommands?.create(command); - // console.log(`created new global command ${command.name}`); - } - }); - - console.clear(); - console.log(' ___ _ __ '); - console.log(' / | ____ (_) /__________ _ __'); - console.log(' / /| | / __ \\/ / __/ ___/ __ \\| |/_/'); - console.log(' / ___ |/ / / / / /_/ / / /_/ /> < '); - console.log('/_/ |_/_/ /_/_/\\__/_/ \\____/_/|_| '); - console.log(`${config.release}, ${config.build}`); - console.log('Bot online. | Anitrox by IDeletedSystem64 | ALL MY CODE KEEPS BLOWING UP!'); - // Statuses - setInterval(async () => { - // Picks a status from the config file - const index = Math.floor(Math.random() * config.statuses.length); - await client.user?.setActivity(config.statuses[index]); - }, 20000); -}); - -// Begin Command Handler -client.on('messageCreate', async (message) => { - if (!message.content.startsWith(config.prefix) || message.author.bot) return; - - const args = message.content.slice(config.prefix.length).split(/\s+/); - const command = args.shift().toLowerCase(); - - if (!client.commands.has(command)) return; - - try { - await client.commands.get(command).parseMessage(client, config, message, args); - } catch (error) { - console.log(error); - message.channel.send({ - embeds: [{ - title: '<:AnitroxError:809651936563429416> **Something went wrong!**', - description: error.stack, - color: 13632027, - footer: { - icon_url: message.author.displayAvatarURL(), - text: config.footerTxt - } - }] - }); - } -}); - -client.on('interactionCreate', async (interaction) => { - client.commands.get(interaction.commandName).parseInteraction(client, config, interaction); -}); - client.login(config.token);