diff --git a/bots/config.example.json b/bots/config.example.json index 7fb6225..db6b642 100644 --- a/bots/config.example.json +++ b/bots/config.example.json @@ -1,11 +1,12 @@ { "discord": { "token": "YOUR-BOT-TOKEN-HERE", - "id": "1038762591805247518" + "id": "1038762591805247518", + "trainRole": "965267139902705744" }, "telegram": { - "token": "YOUR-BOT-TOKEN-HERE" + "token": "YOUR-BOT-TOKEN-HERE" }, "server": { diff --git a/bots/discord/commands/trainMessage.js b/bots/discord/commands/trainMessage.js index 5a45c5a..4674ead 100644 --- a/bots/discord/commands/trainMessage.js +++ b/bots/discord/commands/trainMessage.js @@ -5,11 +5,17 @@ import { SelectMenuBuilder, ComponentType } from 'discord.js'; + export default { data: new ContextMenuCommandBuilder() .setName('Train Message') .setType(ApplicationCommandType.Message), async execute(interaction) { + if (!interaction.member.roles.cache.get(global.config.discord.trainRole)) + return interaction.reply({ + content: 'You don\'t have the permission to do this.', + ephemeral: true + }); const options = []; for (const { label } of global.config.responses) { diff --git a/bots/discord/events/discord/messageCreate.js b/bots/discord/events/discord/messageCreate.js index 804c680..32de4fd 100644 --- a/bots/discord/events/discord/messageCreate.js +++ b/bots/discord/events/discord/messageCreate.js @@ -4,7 +4,10 @@ export default { name: Events.MessageCreate, once: false, execute(msg) { - if (!msg.content) return; - msg.client.helper.scanText(msg.content.toLowerCase(), `${msg.channelId}/${msg.id}`); + if (!msg.content || msg.author.bot) return; + msg.client.helper.scanText( + msg.content.toLowerCase(), + `${msg.channelId}/${msg.id}` + ); } }; diff --git a/bots/telegram/commands/train.js b/bots/telegram/commands/train.js index 489aa69..aeca3aa 100644 --- a/bots/telegram/commands/train.js +++ b/bots/telegram/commands/train.js @@ -1,30 +1,40 @@ export default { - command: /\/train/, - async execute(msg) { - console.log(msg); - if (msg.reply_to_message.message_id === msg.message_thread_id) return global.bot.sendMessage(msg.chat.id, 'Please reply to a message!', { - message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id - }); + command: /\/train/, + async execute(msg) { + console.log(msg); + if (msg.reply_to_message.message_id === msg.message_thread_id) + return global.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 = []; + const options = []; - for (const { label } of global.config.responses) { + for (const { label } of global.config.responses) { options.push({ text: label, callback_data: `label_${label.toLowerCase()}` }); } - const admins = await global.bot.getChatAdministrators(msg.chat.id); - const isAdmin = admins.find(admin => admin.user.id === msg.from.id); + const admins = await global.bot.getChatAdministrators(msg.chat.id); + const isAdmin = admins.find((admin) => admin.user.id === msg.from.id); - if (!isAdmin) return global.bot.sendMessage(msg.chat.id, 'You\'re not an admin.', { - message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id - }); - global.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] - } - }); - } -} \ No newline at end of file + if (!isAdmin) + return global.bot.sendMessage(msg.chat.id, 'You\'re not an admin.', { + message_thread_id: msg.message_thread_id, + reply_to_message_id: msg.message_id + }); + global.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] + } + } + ); + } +}; diff --git a/bots/telegram/events/helper/aiResponse.js b/bots/telegram/events/helper/aiResponse.js index 8ac024a..0d2f29d 100644 --- a/bots/telegram/events/helper/aiResponse.js +++ b/bots/telegram/events/helper/aiResponse.js @@ -9,10 +9,13 @@ export default { if (Number(aiRes.predictions[0].score) >= response.threshold) { const ids = aiRes.id.split('/'); - - global.bot.sendMessage(ids[0], response.text, { message_thread_id: ids[1], reply_to_message_id: ids[2] }); - - return; + + global.bot.sendMessage(ids[0], response.text, { + message_thread_id: ids[1], + reply_to_message_id: ids[2] + }); + + return; } } }; diff --git a/bots/telegram/events/telegram/callbackQuery.js b/bots/telegram/events/telegram/callbackQuery.js index 159dfa4..e1a7c9a 100644 --- a/bots/telegram/events/telegram/callbackQuery.js +++ b/bots/telegram/events/telegram/callbackQuery.js @@ -1,18 +1,30 @@ export default { - name: 'callback_query', - once: false, - async execute(cb) { - const admins = await global.bot.getChatAdministrators(cb.message.chat.id); - const isAdmin = admins.find(admin => admin.user.id === cb.message.from.id); + name: 'callback_query', + once: false, + async execute(cb) { + const admins = await global.bot.getChatAdministrators(cb.message.chat.id); + const isAdmin = admins.find( + (admin) => admin.user.id === cb.message.from.id + ); - if (!isAdmin) return global.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 - });; + if (!isAdmin) + return global.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 + } + ); - global.helper.sendTrainData(cb.message.reply_to_message.text.toLowerCase(), cb.data.replace('label_', '').toUpperCase()); + global.helper.sendTrainData( + cb.message.reply_to_message.text.toLowerCase(), + cb.data.replace('label_', '').toUpperCase() + ); - global.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 - }); - } -} \ No newline at end of file + global.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 + }); + } +}; diff --git a/bots/telegram/events/telegram/message.js b/bots/telegram/events/telegram/message.js index 231d77a..f1c26ac 100644 --- a/bots/telegram/events/telegram/message.js +++ b/bots/telegram/events/telegram/message.js @@ -1,8 +1,11 @@ export default { - name: 'message', - once: false, - async execute(msg) { - if (!msg.text) return; - global.helper.scanText(msg.text.toLowerCase(), `${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}`); - } -} \ No newline at end of file + name: 'message', + once: false, + async execute(msg) { + if (!msg.text) return; + global.helper.scanText( + msg.text.toLowerCase(), + `${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}` + ); + } +}; diff --git a/bots/telegram/index.js b/bots/telegram/index.js index 233156c..2b67747 100644 --- a/bots/telegram/index.js +++ b/bots/telegram/index.js @@ -14,7 +14,7 @@ global.config = JSON.parse(configJSON); global.helper = new HelperClient(global.config); global.helper.connect(); -global.bot = new TelegramBot(config.telegram.token, { polling: true }); +global.bot = new TelegramBot(global.config.telegram.token, { polling: true }); const commandsPath = join(__dirname, 'commands'); const commandFiles = readdirSync(commandsPath).filter((file) => @@ -31,7 +31,7 @@ for (const file of commandFiles) { `[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) => @@ -63,4 +63,4 @@ for (const file of helperEventFiles) { } else { global.helper.on(event.name, (...args) => event.execute(...args)); } -} \ No newline at end of file +}