feat: refactor and new features (#7)

* feat: refactor and new features

+ Refactored the codebase
+ OCR support in bots
+ Server sends training data every minute
+ Not using collectors for Discord feedback buttons anymore
+ Fixed grammar mistakes
+ Configs are now seperated
+ Tokens are no longer in configs
- Like feedback doesn't work for Discord yet

* feat: remove feedback button once voted

* feat: role blacklist

* feat: thread name check

* feat: error handler for training

* fix: bot crashing when a webhook msg is sent

* refactor: remove debugging lines

* feat: allow fixing mistake at votes in discord bot
This commit is contained in:
reis
2023-06-23 21:29:00 +03:00
committed by GitHub
parent f5214a6ace
commit 8b9f45dc22
60 changed files with 1962 additions and 1591 deletions

View File

@@ -1,21 +0,0 @@
import { ContextMenuCommandBuilder, ApplicationCommandType } from 'discord.js';
import trainAISelectMenu from '../utils/trainAISelectMenu.js';
export default {
data: new ContextMenuCommandBuilder()
.setName('Train Message')
.setType(ApplicationCommandType.Message),
async execute(helper, config, interaction) {
if (
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.',
ephemeral: true
});
trainAISelectMenu(interaction, config, helper);
}
};

View File

@@ -1,30 +0,0 @@
import { Events } from 'discord.js';
export default {
name: Events.InteractionCreate,
once: false,
async execute(helper, config, interaction) {
if (!interaction.isMessageContextMenuCommand()) {
if (!interaction.isChatInputCommand()) 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(helper, config, interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
}
};

View File

@@ -1,13 +0,0 @@
import { Events } from 'discord.js';
export default {
name: Events.MessageCreate,
once: false,
execute(helper, _, msg) {
if (!msg.content || msg.author.bot) return;
helper.scanText(
msg.content.toLowerCase().replace(/<.*?>/g, ''),
`${msg.channelId}/${msg.id}`
);
}
};

View File

@@ -1,88 +0,0 @@
import {
EmbedBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle
} from 'discord.js';
import trainAISelectMenu from '../../utils/trainAISelectMenu.js';
export default {
name: 'aiResponse',
once: false,
async execute(client, config, helper, aiRes) {
if (!aiRes.response) return;
if (!aiRes.response[0]) return;
try {
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]);
}
const intent = aiRes.response.reduce((a, b) =>
a.confidence > b.confidence ? a : b
);
const response = config.responses.find(
(res) => res.label === intent.name
);
if (response.threshold > intent.confidence) return;
if (!response.reply) return;
const embed = new EmbedBuilder()
.setTitle(response.reply.title)
.setDescription(response.reply.desc)
.setColor(14908858)
.setFooter({ text: `Confidence: ${intent.confidence}` });
const feedbackRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('fb-like')
.setEmoji('👍')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId('fb-dislike')
.setEmoji('👎')
.setStyle(ButtonStyle.Danger)
);
const reply = await message.reply({
embeds: [embed],
components: [feedbackRow]
});
const filter = (i) =>
i.member.roles.highest.comparePositionTo(
i.member.guild.roles.cache.get(config.discord.trainRole)
) > 0;
const collector = reply.createMessageComponentCollector({
filter,
time: 15_000
});
collector.on('collect', (i) => {
if (i.customId == 'fb-like') {
// We train it using the label the AI gave.
helper.sendTrainData(message.content);
i.reply({ ephemeral: true, content: 'Sent train data to server.' });
} else {
// We ask the trainer to train it using the select menu.
trainAISelectMenu(i, config, helper, message);
}
});
return;
} catch (e) {
console.log(e);
}
}
};

View File

@@ -1,7 +0,0 @@
export default {
name: 'ocrResponse',
once: false,
execute() {
// TODO
}
};

View File

@@ -1,85 +0,0 @@
import { Client, GatewayIntentBits, Collection } from 'discord.js';
import { readFileSync, readdirSync } from 'node:fs';
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import HelperClient from '../../client/index.js';
export default async () => {
const config = JSON.parse(readFileSync('./config.json', 'utf-8'));
const helper = new HelperClient(config);
helper.connect();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.commands = new Collection();
const commandsPath = join(__dirname, 'commands');
const commandFiles = readdirSync(commandsPath).filter((file) =>
file.endsWith('.js')
);
for (const file of commandFiles) {
const filePath = join(commandsPath, file);
const command = (await import(`file://${filePath}`)).default;
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
const discordEventsPath = join(__dirname, 'events/discord');
const discordEventFiles = readdirSync(discordEventsPath).filter((file) =>
file.endsWith('.js')
);
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(helper, config, ...args)
);
} else {
client.on(event.name, (...args) =>
event.execute(helper, config, ...args)
);
}
}
// The ReVanced Helper events.
const helperEventsPath = join(__dirname, 'events/helper');
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
file.endsWith('.js')
);
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(client, config, helper, ...args)
);
} else {
helper.on(event.name, (...args) =>
event.execute(client, config, helper, ...args)
);
}
}
client.login(config.discord.token);
};

View File

@@ -1,13 +0,0 @@
{
"name": "discord-bot",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "discord-bot",
"version": "1.0.0",
"license": "GPL-3.0-or-later"
}
}
}

View File

@@ -1,13 +0,0 @@
{
"name": "discord-bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Reis Can",
"license": "GPL-3.0-or-later"
}

View File

@@ -1,35 +0,0 @@
import { REST, Routes } from 'discord.js';
import { readdirSync, readFileSync } from 'node:fs';
const configJSON = readFileSync('../config.json', 'utf-8');
const config = JSON.parse(configJSON);
const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandFiles = readdirSync('./commands').filter((file) =>
file.endsWith('.js')
);
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = await import(`./commands/${file}`);
commands.push(command.default.data.toJSON());
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(config.discord.token);
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(Routes.applicationCommands(config.discord.id), {
body: commands
});
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}

View File

@@ -1,48 +0,0 @@
import {
ActionRowBuilder,
StringSelectMenuBuilder,
ComponentType
} from 'discord.js';
export default async function trainAISelectMenu(
interaction,
config,
helper,
message
) {
const options = [];
for (const { label } of config.responses) {
options.push({
label: label,
description: `The ${label} label.`,
value: label.toLowerCase()
});
}
const row = new ActionRowBuilder().addComponents(
new StringSelectMenuBuilder()
.setCustomId('select')
.setPlaceholder('Nothing selected')
.addOptions(options)
);
const reply = await interaction.reply({
content: 'Please select the corresponding label to train the bot.',
components: [row],
ephemeral: true
});
const collector = reply.createMessageComponentCollector({
componentType: ComponentType.StringSelect,
time: 15000
});
const interactedMessage = message
? message.content
: interaction.targetMessage.content.toLowerCase();
collector.on('collect', (i) => {
helper.sendTrainData(interactedMessage, i.values[0]);
i.reply({ content: 'Sent train data to server.', ephemeral: true });
});
}