From dfedd09384ddd51f3907d86cf33bc363978a4efc Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Fri, 18 Nov 2022 23:45:31 -0700 Subject: [PATCH] event loading + ready event --- bot.js | 3 +- src/db/connect.js | 1 + src/handle/runtime/events/ready.js | 5 +++ src/handle/startup/collect/events.js | 46 +++++++++++++++++++++++++++- src/handle/startup/run/login.js | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/handle/runtime/events/ready.js diff --git a/bot.js b/bot.js index da32fda..8630c81 100644 --- a/bot.js +++ b/bot.js @@ -25,10 +25,9 @@ const startBot = async () => { client.log(client.utils.gr(client.config.randResp.clistart), {color: "#78d9f8", source: "NATS"}, true, true); //natsuki's wakeup log - await require('./src/handle/startup/run/login')(client); //log in to discord await require('./src/db/connect')(client); //connect to database - await require('./src/handle/startup/run/collect')(client); //load in commands and events + await require('./src/handle/startup/run/login')(client); //log in to discord }; startBot().catch(e => errorhandler(e)); // TODO add a .catch() and flag to recover the process // feels like there isn't a function name to do this justice :joy: diff --git a/src/db/connect.js b/src/db/connect.js index 0b184fd..7e794e1 100644 --- a/src/db/connect.js +++ b/src/db/connect.js @@ -4,6 +4,7 @@ const mongoose = require('mongoose'); const ora = require('../util/log/ora'); module.exports = async client => { + if (!client.misc) {client.misc = {};} const auth = client.auth; const t = Date.now(); client.misc.dbconnected = true; diff --git a/src/handle/runtime/events/ready.js b/src/handle/runtime/events/ready.js new file mode 100644 index 0000000..2a86e19 --- /dev/null +++ b/src/handle/runtime/events/ready.js @@ -0,0 +1,5 @@ +const Discord = require('discord.js'); + +module.exports = async client => { + require('../../startup/run/hello')(client); // startup info +}; \ No newline at end of file diff --git a/src/handle/startup/collect/events.js b/src/handle/startup/collect/events.js index 39963e0..c3d338e 100644 --- a/src/handle/startup/collect/events.js +++ b/src/handle/startup/collect/events.js @@ -1 +1,45 @@ -module.exports = async client => {}; \ No newline at end of file +const fs = require('fs'); +const chalk = require('chalk'); +const {Collection} = require('discord.js'); + +const eventsDirName = './src/handle/runtime/events'; + +module.exports = async client => { + client.aliases = new Collection(); + + let dirErrors = []; let fileErrors = []; //collect error objects to output them all at the end + let readDirs = []; //list of dirs to print at the end + let totalEvents = 0; + client.log('Loading events...', {source: 'boot', color: 'blue'}, 0, 1); + const readDir = dir => { + let dirRead; + try {dirRead = fs.readdirSync(dir);} + catch (e) { + client.error(`Failed to read directory ${chalk.white(dir)}`); + return dirErrors.push([`Unable to read directory ${chalk.white(dir)}. Error:`, e]); + } + let files = dirRead.filter(item => item.endsWith('.js')); + let folders = dirRead.filter(item => fs.lstatSync(`${dir}/${item}`).isDirectory()); + files.forEach(file => { + try { + const event = require(`../../../../${dir.slice(2)}/${file}`); + const eventName = file.split('.')[0]; + client.removeAllListeners(eventName); + client.on(eventName, event.bind(null, client)); + client.log(`Loaded ${chalk.white(eventName)} event`, {color: 'blueBright', source: 'boot', sourceColor: 'blue'}); + } + catch (e) { + client.error(`Failed to read file ${chalk.white(file)}`); + return fileErrors.push([`Unable to read file ${chalk.white(file)}. Error:`, e]); + } + }); + readDirs.push(`${dir.split('/').slice(4).join('/')}/`); // "events/..." + return folders.forEach(folder => readDir(`${dir}/${folder}`)); //recurse infinitely + }; + readDir(eventsDirName); + console.log(""); + dirErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); + fileErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); + readDirs.forEach(dir => client.log(`Read from directory ${chalk.green(dir)}`, {source: 'proc'})); + client.log(`Loaded ${chalk.white(totalEvents)} event${client.utils.s(totalEvents)}!`, {color: 'blue', source: 'boot'}, 1, 1); +}; \ No newline at end of file diff --git a/src/handle/startup/run/login.js b/src/handle/startup/run/login.js index 89c91a9..eaa4c5e 100644 --- a/src/handle/startup/run/login.js +++ b/src/handle/startup/run/login.js @@ -17,5 +17,5 @@ module.exports = async (client) => { client.warn("Discord not connected, considering runtime to be unusable and exiting.", 0, true, true); throw new Error(); } - return client.success(`Connected to Discord in ${chalk.white(`${Date.now() - t}ms`)}.`); + return client.success(`Connected to Discord in ${chalk.white(`${Date.now() - t}ms`)}.`, 0, 0, 1); };