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,36 +0,0 @@
{
"discord": {
"token": "YOUR-BOT-TOKEN-HERE",
"id": "1038762591805247518",
"trainRole": "965267139902705744",
"trainingRole": "952987191401926697"
},
"telegram": {
"token": "YOUR-BOT-TOKEN-HERE"
},
"reddit": {
"username": "ReVancedBot",
"password": "supers3cur3p4ssw0rd",
"userAgent": "ReVancedBot-Reddit",
"clientId": "CLIENT-ID",
"clientSecret": "CLIENT-SECRET"
},
"server": {
"port": 3000,
"host": "192.168.1.6"
},
"responses": [
{
"label": "DOWNLOAD",
"threshold": 0.85,
"reply": {
"title": "download??",
"desc": "the download?"
}
}
]
}

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

View File

@@ -1,13 +0,0 @@
import { readdirSync } from 'node:fs';
const botFolders = readdirSync('./', { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const runBotOnly = process.argv[2] ? process.argv[2] : null;
for (const botFolder of botFolders) {
if (botFolder === 'node_modules') continue;
if (runBotOnly && runBotOnly !== botFolder) continue;
const botIndex = await import(`./${botFolder}/index.js`);
botIndex.default();
}

2639
bots/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +0,0 @@
{
"name": "bots",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"author": "Reis Can",
"license": "GPL-3.0-or-later",
"dependencies": {
"discord.js": "^14.7.1",
"node-telegram-bot-api": "^0.60.0",
"snoostorm": "^1.5.2"
}
}

View File

@@ -1,37 +0,0 @@
export default {
command: 'train',
async execute(client, helper, item, args) {
console.log(args);
const isAdmin = await client
.getSubreddit('revancedapp')
.getModerators({ name: item.author.name });
if (!isAdmin[0])
return client.getComment(item.id).reply('You\'re not an admin.');
if (!args[0])
return client.getComment(item.id).reply('You didn\'t specifiy the label!');
const isComment = item.parent_id.split('_')[0] === 't1';
if (isComment) {
const commentData = (await client.getComment(item.parent_id).fetch())
.body;
helper.sendTrainData(commentData, args[0].toUpperCase());
} else {
if (!args[1])
return client
.getComment(item.id)
.reply(
'You didn\'t specifiy whether if title or description should be sent!'
);
const postData = await client.getSubmission(item.parent_id).fetch();
helper.sendTrainData(
args[1] === 'title' ? postData.title : postData.selftext,
args[0].toUpperCase()
);
}
return client
.getComment(item.id)
.reply('Sent the training data to the server.');
}
};

View File

@@ -1,38 +0,0 @@
export default {
name: 'aiResponse',
once: false,
async execute(client, config, aiRes) {
if (!aiRes.response) return;
if (!aiRes.response[0]) return;
const ids = aiRes.id.split('/');
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;
switch (ids[0]) {
case 'comment': {
client
.getComment(ids[1])
.reply(
`## ${response.reply.title}\n\n${response.reply.desc}\n\n*Confidence: ${intent.confidence}*\n\nThis bot is currently being tested in production. Ignore it, if it's wrong.`
);
break;
}
case 'post': {
client
.getSubmission(ids[1])
.reply(
`## ${response.reply.title}\n\n${response.reply.desc}\n\n*Confidence: ${intent.confidence}*\n\nThis bot is currently being tested in production. Ignore it, if it's wrong.`
);
break;
}
}
return;
}
};

View File

@@ -1,105 +0,0 @@
import Snoowrap from 'snoowrap';
import { SubmissionStream, CommentStream } from 'snoostorm';
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';
import HelperClient from '../../client/index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export default async () => {
const config = JSON.parse(readFileSync('./config.json', 'utf-8'));
const client = new Snoowrap(config.reddit);
const helper = new HelperClient(config);
helper.connect();
client.commands = new Map();
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 ('command' in command && 'execute' in command) {
client.commands.set(command.command, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
const checkedItems = [];
const args = {
subreddit: 'revancedapp',
limit: 10,
pollTime: 5000
};
const comments = new CommentStream(client, args);
const posts = new SubmissionStream(client, args);
comments.on('item', async (item) => {
await handleItem(item, false);
});
posts.on('item', async (item) => {
await handleItem(item, true);
});
async function handleItem(item, isPost) {
// The "skill issue (refresh)" incident.
if (item.author.name === config.reddit.username) return;
if (checkedItems.includes(item.id)) return;
checkedItems.push(item.id);
if (isPost) {
// It's a post, we have to also send post body.
helper.scanText(item.title.toLowerCase(), `post/${item.id}`);
helper.scanText(item.selftext.toLowerCase(), `post/${item.id}`);
} else {
const body = item.body.toLowerCase();
if (body.startsWith(`u/${config.reddit.username.toLowerCase()}`)) {
const args = body
.replace(`u/${config.reddit.username.toLowerCase()} `, '')
.split(' ');
const command = args[0];
args.shift();
if (!client.commands.get(command)) return;
await client.commands.get(command).execute(client, helper, item, args);
} else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`);
}
}
// 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, ...args)
);
} else {
helper.on(event.name, (...args) =>
event.execute(client, config, ...args)
);
}
}
};

View File

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

View File

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

View File

@@ -1,61 +0,0 @@
export default {
command: /\/train/,
async execute(bot, config, msg) {
const admins = await bot.getChatAdministrators(msg.chat.id);
const isAdmin = admins.find((admin) => admin.user.id === msg.from.id);
if (!isAdmin)
return bot.sendMessage(msg.chat.id, 'You\'re not an admin.', {
message_thread_id: msg.message_thread_id,
reply_to_message_id: msg.message_id
});
if (msg.reply_to_message.message_id === msg.message_thread_id)
return bot.sendMessage(msg.chat.id, 'Please reply to a message!', {
message_thread_id: msg.message_thread_id,
reply_to_message_id: msg.message_id
});
const options = [];
let arrI = 0;
let i = 0;
for (const { label } of config.responses) {
if (arrI === 0 && i === 0) {
options.push([
{
text: label,
callback_data: `label_${label.toLowerCase()}`
}
]);
i++;
} else if (i === 2) {
options.push([
{
text: label,
callback_data: `label_${label.toLowerCase()}`
}
]);
i = 0;
arrI++;
} else {
options[arrI].push({
text: label,
callback_data: `label_${label.toLowerCase()}`
});
i++;
}
}
bot.sendMessage(
msg.chat.id,
'Please select the corresponding label to train the bot.',
{
message_thread_id: msg.message_thread_id,
reply_to_message_id: msg.reply_to_message.message_id,
reply_markup: {
inline_keyboard: options
}
}
);
}
};

View File

@@ -1,28 +0,0 @@
export default {
name: 'aiResponse',
once: false,
async execute(bot, config, aiRes) {
if (!aiRes.response) return;
if (!aiRes.response[0]) return;
const ids = aiRes.id.split('/');
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;
// Because for some reason the markdown parser in TG is a pain in the ass,
// there won't be markdown support for now.
bot.sendMessage(
ids[0],
`## ${response.reply.title}\n\n${response.reply.desc}\n\n_Confidence: ${intent.confidence}_\n\nThis bot is currently being tested in production. Ignore it, if it's wrong.`,
{
message_thread_id: ids[1],
reply_to_message_id: ids[2]
}
);
return;
}
};

View File

@@ -1,26 +0,0 @@
export default {
name: 'callback_query',
once: false,
async execute(bot, helper, cb) {
const admins = await bot.getChatAdministrators(cb.message.chat.id);
const isAdmin = admins.find((admin) => admin.user.id === cb.from.id);
if (!isAdmin)
return bot.sendMessage(cb.message.chat.id, 'You\'re not an admin.', {
message_thread_id: cb.message.message_thread_id,
reply_to_message_id: cb.message.message_id
});
helper.sendTrainData(
cb.message.reply_to_message.text.toLowerCase(),
cb.data.replace('label_', '').toUpperCase()
);
bot.sendMessage(cb.message.chat.id, 'Sent train data to server.', {
message_thread_id: cb.message.message_thread_id,
reply_to_message_id: cb.message.message_id
});
bot.deleteMessage(cb.message.chat.id, cb.message.message_id);
}
};

View File

@@ -1,11 +0,0 @@
export default {
name: 'message',
once: false,
async execute(_, helper, msg) {
if (!msg.text) return;
helper.scanText(
msg.text.toLowerCase(),
`${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}`
);
}
};

View File

@@ -1,69 +0,0 @@
import TelegramBot from 'node-telegram-bot-api';
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 bot = new TelegramBot(config.telegram.token, { polling: true });
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 ('command' in command && 'execute' in command) {
bot.onText(command.command, (...args) =>
command.execute(bot, config, ...args)
);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
const tgEventsPath = join(__dirname, 'events/telegram');
const tgEventFiles = readdirSync(tgEventsPath).filter((file) =>
file.endsWith('.js')
);
for (const file of tgEventFiles) {
const filePath = join(tgEventsPath, file);
const event = (await import(`file://${filePath}`)).default;
if (event.once) {
bot.once(event.name, (...args) => event.execute(bot, helper, ...args));
} else {
bot.on(event.name, (...args) => event.execute(bot, helper, ...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(bot, config, ...args));
} else {
helper.on(event.name, (...args) => event.execute(bot, config, ...args));
}
}
};

View File

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

View File

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