From 939b83531d776d7288f68364ab33d69ba75baff5 Mon Sep 17 00:00:00 2001 From: GramingFoxTeam Date: Fri, 18 Nov 2022 15:27:48 +0300 Subject: [PATCH] refactor(telegram-bot): don't use global variables --- bots/telegram/commands/train.js | 12 ++++++------ bots/telegram/events/helper/aiResponse.js | 6 +++--- .../telegram/events/telegram/callbackQuery.js | 12 ++++++------ bots/telegram/events/telegram/message.js | 4 ++-- bots/telegram/index.js | 19 +++++++++---------- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/bots/telegram/commands/train.js b/bots/telegram/commands/train.js index 1e2ae10..a567bcb 100644 --- a/bots/telegram/commands/train.js +++ b/bots/telegram/commands/train.js @@ -1,8 +1,8 @@ export default { command: /\/train/, - async execute(msg) { + async execute(bot, config, msg) { if (msg.reply_to_message.message_id === msg.message_thread_id) - return global.bot.sendMessage(msg.chat.id, 'Please reply to a message!', { + 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 }); @@ -10,7 +10,7 @@ export default { const options = []; let arrI = 0; let i = 0; - for (const { label } of global.config.responses) { + for (const { label } of config.responses) { if (arrI === 0 && i === 0) { options.push([ { @@ -37,15 +37,15 @@ export default { } } - const admins = await global.bot.getChatAdministrators(msg.chat.id); + const admins = await 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.', { + 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 }); - global.bot.sendMessage( + bot.sendMessage( msg.chat.id, 'Please select the corresponding label to train the bot.', { diff --git a/bots/telegram/events/helper/aiResponse.js b/bots/telegram/events/helper/aiResponse.js index ae5e69d..6779828 100644 --- a/bots/telegram/events/helper/aiResponse.js +++ b/bots/telegram/events/helper/aiResponse.js @@ -1,8 +1,8 @@ export default { name: 'aiResponse', once: false, - async execute(aiRes) { - const response = global.config.responses.find( + async execute(bot, config, aiRes) { + const response = config.responses.find( (res) => res.label === aiRes.predictions[0].label ); if (!response) return; @@ -12,7 +12,7 @@ export default { if (!response.text) return; - global.bot.sendMessage(ids[0], response.text, { + bot.sendMessage(ids[0], response.text, { message_thread_id: ids[1], reply_to_message_id: ids[2] }); diff --git a/bots/telegram/events/telegram/callbackQuery.js b/bots/telegram/events/telegram/callbackQuery.js index bc97933..9e0da9e 100644 --- a/bots/telegram/events/telegram/callbackQuery.js +++ b/bots/telegram/events/telegram/callbackQuery.js @@ -1,12 +1,12 @@ export default { name: 'callback_query', once: false, - async execute(cb) { - const admins = await global.bot.getChatAdministrators(cb.message.chat.id); + 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 global.bot.sendMessage( + return bot.sendMessage( cb.message.chat.id, 'You\'re not an admin.', { @@ -15,16 +15,16 @@ export default { } ); - global.helper.sendTrainData( + 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.', { + 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 }); - global.bot.deleteMessage(cb.message.chat.id, cb.message.message_id); + bot.deleteMessage(cb.message.chat.id, cb.message.message_id); } }; diff --git a/bots/telegram/events/telegram/message.js b/bots/telegram/events/telegram/message.js index f1c26ac..4b20d60 100644 --- a/bots/telegram/events/telegram/message.js +++ b/bots/telegram/events/telegram/message.js @@ -1,9 +1,9 @@ export default { name: 'message', once: false, - async execute(msg) { + async execute(_, helper, msg) { if (!msg.text) return; - global.helper.scanText( + 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 2b67747..f105ea3 100644 --- a/bots/telegram/index.js +++ b/bots/telegram/index.js @@ -8,13 +8,12 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); import HelperClient from '../../client/index.js'; -const configJSON = readFileSync('../config.json', 'utf-8'); -global.config = JSON.parse(configJSON); +const config = JSON.parse(readFileSync('../config.json', 'utf-8')); -global.helper = new HelperClient(global.config); -global.helper.connect(); +const helper = new HelperClient(config); +helper.connect(); -global.bot = new TelegramBot(global.config.telegram.token, { polling: true }); +const bot = new TelegramBot(config.telegram.token, { polling: true }); const commandsPath = join(__dirname, 'commands'); const commandFiles = readdirSync(commandsPath).filter((file) => @@ -25,7 +24,7 @@ for (const file of commandFiles) { const filePath = join(commandsPath, file); const command = (await import(`file://${filePath}`)).default; if ('command' in command && 'execute' in command) { - global.bot.onText(command.command, (...args) => command.execute(...args)); + 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.` @@ -42,9 +41,9 @@ for (const file of tgEventFiles) { const filePath = join(tgEventsPath, file); const event = (await import(`file://${filePath}`)).default; if (event.once) { - global.bot.once(event.name, (...args) => event.execute(...args)); + bot.once(event.name, (...args) => event.execute(bot, helper, ...args)); } else { - global.bot.on(event.name, (...args) => event.execute(...args)); + bot.on(event.name, (...args) => event.execute(bot, helper, ...args)); } } @@ -59,8 +58,8 @@ for (const file of helperEventFiles) { const filePath = join(helperEventsPath, file); const event = (await import(`file://${filePath}`)).default; if (event.once) { - global.helper.once(event.name, (...args) => event.execute(...args)); + helper.once(event.name, (...args) => event.execute(bot, config, ...args)); } else { - global.helper.on(event.name, (...args) => event.execute(...args)); + helper.on(event.name, (...args) => event.execute(bot, config, ...args)); } }