mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
refactor(telegram-bot): don't use global variables
This commit is contained in:
@@ -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.',
|
||||
{
|
||||
|
||||
@@ -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]
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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}`
|
||||
);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user