mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 21:56:17 +00:00
feat(discord-bot): event handler
This commit is contained in:
23
bots/discord/events/discord/interactionCreate.js
Normal file
23
bots/discord/events/discord/interactionCreate.js
Normal file
@@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
bots/discord/events/discord/messageCreate.js
Normal file
9
bots/discord/events/discord/messageCreate.js
Normal file
@@ -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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
bots/discord/events/helper/aiResponse.js
Normal file
30
bots/discord/events/helper/aiResponse.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
bots/discord/events/helper/ocrResponse.js
Normal file
7
bots/discord/events/helper/ocrResponse.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export default {
|
||||||
|
name: 'ocrResponse',
|
||||||
|
once: false,
|
||||||
|
execute(client, ocrRes) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ const helper = new HelperClient(config);
|
|||||||
|
|
||||||
helper.connect();
|
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.commands = new Collection();
|
||||||
client.helper = helper;
|
client.helper = helper;
|
||||||
@@ -33,58 +33,32 @@ for (const file of commandFiles) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.on(Events.MessageCreate, async (msg) => {
|
const discordEventsPath = join(__dirname, 'events/discord');
|
||||||
helper.scanText(msg.content, `${msg.channelId}/${msg.id}`);
|
const discordEventFiles = readdirSync(discordEventsPath).filter(file => file.endsWith('.js'));
|
||||||
});
|
|
||||||
|
|
||||||
client.on(Events.InteractionCreate, async (interaction) => {
|
for (const file of discordEventFiles) {
|
||||||
if (!interaction.isMessageContextMenuCommand()) return;
|
const filePath = join(discordEventsPath, file);
|
||||||
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
if (event.once) {
|
||||||
|
client.once(event.name, (...args) => event.execute(...args));
|
||||||
if (!command) {
|
} else {
|
||||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
client.on(event.name, (...args) => event.execute(...args));
|
||||||
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 });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// The ReVanced Helper events.
|
// The ReVanced Helper events.
|
||||||
|
|
||||||
helper.on('aiResponse', async (aiRes) => {
|
const helperEventsPath = join(__dirname, 'events/helper');
|
||||||
const response = config.responses.find(res => res.label === aiRes.predictions[0].label);
|
const helperEventFiles = readdirSync(helperEventsPath).filter(file => file.endsWith('.js'));
|
||||||
if (!response) return;
|
|
||||||
|
|
||||||
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
for (const file of helperEventFiles) {
|
||||||
const ids = aiRes.id.split('/');
|
const filePath = join(helperEventsPath, file);
|
||||||
let channel = client.channels.cache.get(ids[0]);
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
|
if (event.once) {
|
||||||
if (!channel) {
|
helper.once(event.name, (...args) => event.execute(...args));
|
||||||
await client.channels.fetch(ids[0]);
|
} else {
|
||||||
channel = client.channels.cache.get(ids[0]);
|
helper.on(event.name, (...args) => event.execute(...args));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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) => {
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
client.login(config.discord.token);
|
client.login(config.discord.token);
|
||||||
Reference in New Issue
Block a user