diff --git a/bots/discord/events/discord/interactionCreate.js b/bots/discord/events/discord/interactionCreate.js new file mode 100644 index 0000000..ea76246 --- /dev/null +++ b/bots/discord/events/discord/interactionCreate.js @@ -0,0 +1,23 @@ +import { Events } from 'discord.js'; + +export default { + name: Events.InteractionCreate, + once: false, + async execute(interaction) { + if (!interaction.isMessageContextMenuCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } + } +} \ No newline at end of file diff --git a/bots/discord/events/discord/messageCreate.js b/bots/discord/events/discord/messageCreate.js new file mode 100644 index 0000000..622f3f1 --- /dev/null +++ b/bots/discord/events/discord/messageCreate.js @@ -0,0 +1,9 @@ +import { Events } from 'discord.js'; + +export default { + name: Events.MessageCreate, + once: false, + execute(msg) { + msg.client.helper.scanText(msg.content, `${msg.channelId}/${msg.id}`); + } +} \ No newline at end of file diff --git a/bots/discord/events/helper/aiResponse.js b/bots/discord/events/helper/aiResponse.js new file mode 100644 index 0000000..1518806 --- /dev/null +++ b/bots/discord/events/helper/aiResponse.js @@ -0,0 +1,30 @@ +export default { + name: 'aiResponse', + once: false, + async execute(aiRes) { + console.log(aiRes); + const response = config.responses.find(res => res.label === aiRes.predictions[0].label); + if (!response) return; + + if (Number(aiRes.predictions[0].score) >= response.threshold) { + const ids = aiRes.id.split('/'); + let channel = client.channels.cache.get(ids[0]); + + if (!channel) { + await client.channels.fetch(ids[0]); + channel = client.channels.cache.get(ids[0]); + } + + let message = channel.messages.cache.get(ids[1]); + + if (!message) { + await channel.messages.fetch(ids[1]); + message = channel.messages.cache.get(ids[1]); + } + + message.reply(response.text); + + return; + } + } +} \ No newline at end of file diff --git a/bots/discord/events/helper/ocrResponse.js b/bots/discord/events/helper/ocrResponse.js new file mode 100644 index 0000000..737e8df --- /dev/null +++ b/bots/discord/events/helper/ocrResponse.js @@ -0,0 +1,7 @@ +export default { + name: 'ocrResponse', + once: false, + execute(client, ocrRes) { + // TODO + } +} \ No newline at end of file diff --git a/bots/discord/index.js b/bots/discord/index.js index bea58f4..bc9ebbd 100644 --- a/bots/discord/index.js +++ b/bots/discord/index.js @@ -15,7 +15,7 @@ const helper = new HelperClient(config); helper.connect(); -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); +global.client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.commands = new Collection(); client.helper = helper; @@ -33,58 +33,32 @@ for (const file of commandFiles) { } } -client.on(Events.MessageCreate, async (msg) => { - helper.scanText(msg.content, `${msg.channelId}/${msg.id}`); -}); +const discordEventsPath = join(__dirname, 'events/discord'); +const discordEventFiles = readdirSync(discordEventsPath).filter(file => file.endsWith('.js')); -client.on(Events.InteractionCreate, async (interaction) => { - if (!interaction.isMessageContextMenuCommand()) return; - - const command = interaction.client.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; +for (const file of discordEventFiles) { + const filePath = join(discordEventsPath, file); + const event = (await import(`file://${filePath}`)).default; + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); } - - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); - } -}); +} // The ReVanced Helper events. -helper.on('aiResponse', async (aiRes) => { - const response = config.responses.find(res => res.label === aiRes.predictions[0].label); - if (!response) return; +const helperEventsPath = join(__dirname, 'events/helper'); +const helperEventFiles = readdirSync(helperEventsPath).filter(file => file.endsWith('.js')); - if (Number(aiRes.predictions[0].score) >= response.threshold) { - const ids = aiRes.id.split('/'); - let channel = client.channels.cache.get(ids[0]); - - if (!channel) { - await client.channels.fetch(ids[0]); - channel = client.channels.cache.get(ids[0]); - } - - let message = channel.messages.cache.get(ids[1]); - - if (!message) { - await channel.messages.fetch(ids[1]); - message = channel.messages.cache.get(ids[1]); - } - - message.reply(response.text); - - return; - } -}); - -helper.on('ocrResponse', async (aiRes) => { - -}); +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)); + } else { + helper.on(event.name, (...args) => event.execute(...args)); + } +} client.login(config.discord.token); \ No newline at end of file