feat: run bots in one process

This commit is contained in:
GramingFoxTeam
2022-12-25 15:45:40 +03:00
parent 54ec6232ee
commit d26d533174
5 changed files with 140 additions and 115 deletions

View File

@@ -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
View 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
View 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"
}

View File

@@ -9,7 +9,8 @@ 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);
@@ -99,3 +100,4 @@ for (const file of helperEventFiles) {
helper.on(event.name, (...args) => event.execute(client, config, ...args)); helper.on(event.name, (...args) => event.execute(client, config, ...args));
} }
} }
}

View File

@@ -8,7 +8,8 @@ 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();
@@ -65,3 +66,4 @@ for (const file of helperEventFiles) {
helper.on(event.name, (...args) => event.execute(bot, config, ...args)); helper.on(event.name, (...args) => event.execute(bot, config, ...args));
} }
} }
}