From d26d53317440c64fb775cea609a87d29be6c8b40 Mon Sep 17 00:00:00 2001 From: GramingFoxTeam Date: Sun, 25 Dec 2022 15:45:40 +0300 Subject: [PATCH] feat: run bots in one process --- bots/discord/index.js | 4 +- bots/index.js | 10 +++ bots/package.json | 9 +++ bots/reddit/index.js | 140 +++++++++++++++++++++-------------------- bots/telegram/index.js | 92 ++++++++++++++------------- 5 files changed, 140 insertions(+), 115 deletions(-) create mode 100644 bots/index.js create mode 100644 bots/package.json diff --git a/bots/discord/index.js b/bots/discord/index.js index 1ca3d4f..069272c 100644 --- a/bots/discord/index.js +++ b/bots/discord/index.js @@ -3,11 +3,11 @@ 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); @@ -78,3 +78,5 @@ for (const file of helperEventFiles) { } client.login(config.discord.token); + +} \ No newline at end of file diff --git a/bots/index.js b/bots/index.js new file mode 100644 index 0000000..cc33417 --- /dev/null +++ b/bots/index.js @@ -0,0 +1,10 @@ +import { readdirSync } from 'node:fs'; + +const botFolders = readdirSync('./', { withFileTypes: true }) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name); + +for (const botFolder of botFolders) { + const botIndex = await import(`./${botFolder}/index.js`); + botIndex.default(); +} \ No newline at end of file diff --git a/bots/package.json b/bots/package.json new file mode 100644 index 0000000..aa7ca10 --- /dev/null +++ b/bots/package.json @@ -0,0 +1,9 @@ +{ + "name": "bots", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "author": "Reis Can", + "license": "GPL-3.0-or-later" +} diff --git a/bots/reddit/index.js b/bots/reddit/index.js index fc52bae..d41968e 100644 --- a/bots/reddit/index.js +++ b/bots/reddit/index.js @@ -9,93 +9,95 @@ import HelperClient from '../../client/index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); -const config = JSON.parse(readFileSync('../config.json', 'utf-8')); +export default async () => { + const config = JSON.parse(readFileSync('./config.json', 'utf-8')); -const client = new Snoowrap(config.reddit); -const helper = new HelperClient(config); + const client = new Snoowrap(config.reddit); + const helper = new HelperClient(config); -helper.connect(); + helper.connect(); -client.commands = new Map(); + client.commands = new Map(); -const commandsPath = join(__dirname, 'commands'); -const commandFiles = readdirSync(commandsPath).filter((file) => - file.endsWith('.js') -); + 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.` - ); + 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 checkedItems = []; -const args = { - subreddit: 'revancedapp', - limit: 10, - pollTime: 5000 -}; + const args = { + subreddit: 'revancedapp', + limit: 10, + pollTime: 5000 + }; -const comments = new CommentStream(client, args); + const comments = new CommentStream(client, args); -const posts = new SubmissionStream(client, args); + const posts = new SubmissionStream(client, args); -comments.on('item', async (item) => { - await handleItem(item, false); -}); + comments.on('item', async (item) => { + await handleItem(item, false); + }); -posts.on('item', async (item) => { - await handleItem(item, true); -}); + 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; + 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 (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; + 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}`); + await client.commands.get(command).execute(client, helper, item, args); + } else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`); + } } -} -// The ReVanced Helper events. + // The ReVanced Helper events. -const helperEventsPath = join(__dirname, 'events/helper'); -const helperEventFiles = readdirSync(helperEventsPath).filter((file) => - file.endsWith('.js') -); + 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)); + 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)); + } } -} +} \ No newline at end of file diff --git a/bots/telegram/index.js b/bots/telegram/index.js index 67c7ef3..08064c9 100644 --- a/bots/telegram/index.js +++ b/bots/telegram/index.js @@ -8,60 +8,62 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); import HelperClient from '../../client/index.js'; -const config = JSON.parse(readFileSync('../config.json', 'utf-8')); +export default async () => { + const config = JSON.parse(readFileSync('./config.json', 'utf-8')); -const helper = new HelperClient(config); -helper.connect(); + const helper = new HelperClient(config); + helper.connect(); -const bot = new TelegramBot(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) => - file.endsWith('.js') -); + 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.` - ); + 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') -); + 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)); + 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. + // The ReVanced Helper events. -const helperEventsPath = join(__dirname, 'events/helper'); -const helperEventFiles = readdirSync(helperEventsPath).filter((file) => - file.endsWith('.js') -); + 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)); + 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)); + } } -} +} \ No newline at end of file