From 3d549f1292d8c0265797743f29649fa40d5aee00 Mon Sep 17 00:00:00 2001 From: GramingFoxTeam Date: Fri, 18 Nov 2022 15:07:12 +0300 Subject: [PATCH] refactor(discord-bot): don't use global --- bots/discord/commands/trainAI.js | 6 ++--- bots/discord/commands/trainMessage.js | 13 +++++----- .../events/discord/interactionCreate.js | 4 +-- bots/discord/events/discord/messageCreate.js | 7 ++--- bots/discord/events/helper/aiResponse.js | 10 +++---- bots/discord/index.js | 26 ++++++++++--------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/bots/discord/commands/trainAI.js b/bots/discord/commands/trainAI.js index ea3fc72..cd72423 100644 --- a/bots/discord/commands/trainAI.js +++ b/bots/discord/commands/trainAI.js @@ -4,14 +4,14 @@ export default { data: new SlashCommandBuilder() .setName('train') .setDescription('Train the AI.'), - async execute(interaction) { - if (!interaction.member.roles.cache.get(global.config.discord.trainingRole)) + async execute(helper, config, interaction) { + if (!interaction.member.roles.cache.has(config.discord.trainingRole)) return interaction.reply({ content: 'You don\'t have the permission to do this.', ephemeral: true }); - interaction.client.helper.trainAI(); + helper.trainAI(); interaction.reply({ content: 'Sent the trainAI command to the server.', diff --git a/bots/discord/commands/trainMessage.js b/bots/discord/commands/trainMessage.js index 37e4dc9..668702b 100644 --- a/bots/discord/commands/trainMessage.js +++ b/bots/discord/commands/trainMessage.js @@ -10,12 +10,11 @@ export default { data: new ContextMenuCommandBuilder() .setName('Train Message') .setType(ApplicationCommandType.Message), - async execute(interaction) { - // Prettier and ESLint doesn't like to play nicely here. + async execute(helper, config, interaction) { if ( - interaction.member.roles.highest.position < - interaction.member.guild.roles.cache.get(global.config.discord.trainRole) - .position + interaction.member.roles.highest.comparePositionTo( + interaction.member.guild.roles.cache.get(config.discord.trainRole) + ) < 0 ) return interaction.reply({ content: 'You don\'t have the permission to do this.', @@ -23,7 +22,7 @@ export default { }); const options = []; - for (const { label } of global.config.responses) { + for (const { label } of config.responses) { options.push({ label: label, description: `The ${label} label.`, @@ -49,7 +48,7 @@ export default { }); collector.on('collect', (i) => { - i.client.helper.sendTrainData( + helper.sendTrainData( interaction.targetMessage.content.toLowerCase(), i.values[0].toUpperCase() ); diff --git a/bots/discord/events/discord/interactionCreate.js b/bots/discord/events/discord/interactionCreate.js index 39aa1d7..d483336 100644 --- a/bots/discord/events/discord/interactionCreate.js +++ b/bots/discord/events/discord/interactionCreate.js @@ -3,7 +3,7 @@ import { Events } from 'discord.js'; export default { name: Events.InteractionCreate, once: false, - async execute(interaction) { + async execute(helper, config, interaction) { if (!interaction.isMessageContextMenuCommand()) { if (!interaction.isChatInputCommand()) return; } @@ -18,7 +18,7 @@ export default { } try { - await command.execute(interaction); + await command.execute(helper, config, interaction); } catch (error) { console.error(error); await interaction.reply({ diff --git a/bots/discord/events/discord/messageCreate.js b/bots/discord/events/discord/messageCreate.js index 32de4fd..c31c824 100644 --- a/bots/discord/events/discord/messageCreate.js +++ b/bots/discord/events/discord/messageCreate.js @@ -3,11 +3,8 @@ import { Events } from 'discord.js'; export default { name: Events.MessageCreate, once: false, - execute(msg) { + execute(helper, _, msg) { if (!msg.content || msg.author.bot) return; - msg.client.helper.scanText( - msg.content.toLowerCase(), - `${msg.channelId}/${msg.id}` - ); + helper.scanText(msg.content.toLowerCase(), `${msg.channelId}/${msg.id}`); } }; diff --git a/bots/discord/events/helper/aiResponse.js b/bots/discord/events/helper/aiResponse.js index b3ee448..2701297 100644 --- a/bots/discord/events/helper/aiResponse.js +++ b/bots/discord/events/helper/aiResponse.js @@ -1,8 +1,8 @@ export default { name: 'aiResponse', once: false, - async execute(aiRes) { - const response = global.config.responses.find( + async execute(client, config, aiRes) { + const response = config.responses.find( (res) => res.label === aiRes.predictions[0].label ); if (!response) return; @@ -11,11 +11,11 @@ export default { if (!response.text) return; const ids = aiRes.id.split('/'); - let channel = global.client.channels.cache.get(ids[0]); + let channel = client.channels.cache.get(ids[0]); if (!channel) { - await global.client.channels.fetch(ids[0]); - channel = global.client.channels.cache.get(ids[0]); + await client.channels.fetch(ids[0]); + channel = client.channels.cache.get(ids[0]); } let message = channel.messages.cache.get(ids[1]); diff --git a/bots/discord/index.js b/bots/discord/index.js index 8fdb71e..1ca3d4f 100644 --- a/bots/discord/index.js +++ b/bots/discord/index.js @@ -8,14 +8,13 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); import HelperClient from '../../client/index.js'; -const configJSON = readFileSync('../config.json', 'utf-8'); -global.config = JSON.parse(configJSON); +const config = JSON.parse(readFileSync('../config.json', 'utf-8')); -const helper = new HelperClient(global.config); +const helper = new HelperClient(config); helper.connect(); -global.client = new Client({ +const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, @@ -23,8 +22,7 @@ global.client = new Client({ ] }); -global.client.commands = new Collection(); -global.client.helper = helper; +client.commands = new Collection(); const commandsPath = join(__dirname, 'commands'); const commandFiles = readdirSync(commandsPath).filter((file) => @@ -35,7 +33,7 @@ for (const file of commandFiles) { const filePath = join(commandsPath, file); const command = (await import(`file://${filePath}`)).default; if ('data' in command && 'execute' in command) { - global.client.commands.set(command.data.name, command); + client.commands.set(command.data.name, command); } else { console.log( `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.` @@ -52,9 +50,11 @@ for (const file of discordEventFiles) { const filePath = join(discordEventsPath, file); const event = (await import(`file://${filePath}`)).default; if (event.once) { - global.client.once(event.name, (...args) => event.execute(...args)); + client.once(event.name, (...args) => + event.execute(helper, config, ...args) + ); } else { - global.client.on(event.name, (...args) => event.execute(...args)); + client.on(event.name, (...args) => event.execute(helper, config, ...args)); } } @@ -69,10 +69,12 @@ for (const file of helperEventFiles) { const filePath = join(helperEventsPath, file); const event = (await import(`file://${filePath}`)).default; if (event.once) { - helper.once(event.name, (...args) => event.execute(...args)); + helper.once(event.name, (...args) => + event.execute(client, config, ...args) + ); } else { - helper.on(event.name, (...args) => event.execute(...args)); + helper.on(event.name, (...args) => event.execute(client, config, ...args)); } } -global.client.login(global.config.discord.token); +client.login(config.discord.token);