mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-19 17:23:59 +00:00
feat: run bots in one process
This commit is contained in:
@@ -3,11 +3,11 @@ import { readFileSync, readdirSync } from 'node:fs';
|
|||||||
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname, join } from 'node:path';
|
import { dirname, join } from 'node:path';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
import HelperClient from '../../client/index.js';
|
import HelperClient from '../../client/index.js';
|
||||||
|
|
||||||
|
export default async () => {
|
||||||
const config = JSON.parse(readFileSync('../config.json', 'utf-8'));
|
const config = JSON.parse(readFileSync('../config.json', 'utf-8'));
|
||||||
|
|
||||||
const helper = new HelperClient(config);
|
const helper = new HelperClient(config);
|
||||||
@@ -78,3 +78,5 @@ for (const file of helperEventFiles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.login(config.discord.token);
|
client.login(config.discord.token);
|
||||||
|
|
||||||
|
}
|
||||||
10
bots/index.js
Normal file
10
bots/index.js
Normal file
@@ -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();
|
||||||
|
}
|
||||||
9
bots/package.json
Normal file
9
bots/package.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
@@ -9,21 +9,22 @@ import HelperClient from '../../client/index.js';
|
|||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
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 client = new Snoowrap(config.reddit);
|
||||||
const helper = new HelperClient(config);
|
const helper = new HelperClient(config);
|
||||||
|
|
||||||
helper.connect();
|
helper.connect();
|
||||||
|
|
||||||
client.commands = new Map();
|
client.commands = new Map();
|
||||||
|
|
||||||
const commandsPath = join(__dirname, 'commands');
|
const commandsPath = join(__dirname, 'commands');
|
||||||
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||||
file.endsWith('.js')
|
file.endsWith('.js')
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
const filePath = join(commandsPath, file);
|
const filePath = join(commandsPath, file);
|
||||||
const command = (await import(`file://${filePath}`)).default;
|
const command = (await import(`file://${filePath}`)).default;
|
||||||
if ('command' in command && 'execute' in command) {
|
if ('command' in command && 'execute' in command) {
|
||||||
@@ -33,29 +34,29 @@ for (const file of commandFiles) {
|
|||||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkedItems = [];
|
const checkedItems = [];
|
||||||
|
|
||||||
const args = {
|
const args = {
|
||||||
subreddit: 'revancedapp',
|
subreddit: 'revancedapp',
|
||||||
limit: 10,
|
limit: 10,
|
||||||
pollTime: 5000
|
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) => {
|
comments.on('item', async (item) => {
|
||||||
await handleItem(item, false);
|
await handleItem(item, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
posts.on('item', async (item) => {
|
posts.on('item', async (item) => {
|
||||||
await handleItem(item, true);
|
await handleItem(item, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleItem(item, isPost) {
|
async function handleItem(item, isPost) {
|
||||||
// The "skill issue (refresh)" incident.
|
// The "skill issue (refresh)" incident.
|
||||||
if (item.author.name === config.reddit.username) return;
|
if (item.author.name === config.reddit.username) return;
|
||||||
|
|
||||||
@@ -79,16 +80,16 @@ async function handleItem(item, isPost) {
|
|||||||
await client.commands.get(command).execute(client, helper, item, args);
|
await client.commands.get(command).execute(client, helper, item, args);
|
||||||
} else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`);
|
} else helper.scanText(item.body.toLowerCase(), `comment/${item.id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The ReVanced Helper events.
|
// The ReVanced Helper events.
|
||||||
|
|
||||||
const helperEventsPath = join(__dirname, 'events/helper');
|
const helperEventsPath = join(__dirname, 'events/helper');
|
||||||
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
||||||
file.endsWith('.js')
|
file.endsWith('.js')
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of helperEventFiles) {
|
for (const file of helperEventFiles) {
|
||||||
const filePath = join(helperEventsPath, file);
|
const filePath = join(helperEventsPath, file);
|
||||||
const event = (await import(`file://${filePath}`)).default;
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
if (event.once) {
|
if (event.once) {
|
||||||
@@ -98,4 +99,5 @@ for (const file of helperEventFiles) {
|
|||||||
} else {
|
} else {
|
||||||
helper.on(event.name, (...args) => event.execute(client, config, ...args));
|
helper.on(event.name, (...args) => event.execute(client, config, ...args));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,19 +8,20 @@ const __filename = fileURLToPath(import.meta.url);
|
|||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
import HelperClient from '../../client/index.js';
|
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);
|
const helper = new HelperClient(config);
|
||||||
helper.connect();
|
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 commandsPath = join(__dirname, 'commands');
|
||||||
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||||
file.endsWith('.js')
|
file.endsWith('.js')
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
const filePath = join(commandsPath, file);
|
const filePath = join(commandsPath, file);
|
||||||
const command = (await import(`file://${filePath}`)).default;
|
const command = (await import(`file://${filePath}`)).default;
|
||||||
if ('command' in command && 'execute' in command) {
|
if ('command' in command && 'execute' in command) {
|
||||||
@@ -32,14 +33,14 @@ for (const file of commandFiles) {
|
|||||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const tgEventsPath = join(__dirname, 'events/telegram');
|
const tgEventsPath = join(__dirname, 'events/telegram');
|
||||||
const tgEventFiles = readdirSync(tgEventsPath).filter((file) =>
|
const tgEventFiles = readdirSync(tgEventsPath).filter((file) =>
|
||||||
file.endsWith('.js')
|
file.endsWith('.js')
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of tgEventFiles) {
|
for (const file of tgEventFiles) {
|
||||||
const filePath = join(tgEventsPath, file);
|
const filePath = join(tgEventsPath, file);
|
||||||
const event = (await import(`file://${filePath}`)).default;
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
if (event.once) {
|
if (event.once) {
|
||||||
@@ -47,16 +48,16 @@ for (const file of tgEventFiles) {
|
|||||||
} else {
|
} else {
|
||||||
bot.on(event.name, (...args) => event.execute(bot, helper, ...args));
|
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 helperEventsPath = join(__dirname, 'events/helper');
|
||||||
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
||||||
file.endsWith('.js')
|
file.endsWith('.js')
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of helperEventFiles) {
|
for (const file of helperEventFiles) {
|
||||||
const filePath = join(helperEventsPath, file);
|
const filePath = join(helperEventsPath, file);
|
||||||
const event = (await import(`file://${filePath}`)).default;
|
const event = (await import(`file://${filePath}`)).default;
|
||||||
if (event.once) {
|
if (event.once) {
|
||||||
@@ -64,4 +65,5 @@ for (const file of helperEventFiles) {
|
|||||||
} else {
|
} else {
|
||||||
helper.on(event.name, (...args) => event.execute(bot, config, ...args));
|
helper.on(event.name, (...args) => event.execute(bot, config, ...args));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user