refactor(discord-bot): don't use global

This commit is contained in:
GramingFoxTeam
2022-11-18 15:07:12 +03:00
parent edba5f7f3a
commit 3d549f1292
6 changed files with 32 additions and 34 deletions

View File

@@ -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.',

View File

@@ -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()
);

View File

@@ -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({

View File

@@ -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}`);
}
};

View File

@@ -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]);

View File

@@ -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);