event loading + ready event

v2
Kit Kasune 2 years ago
parent fed56baec9
commit dfedd09384
  1. 3
      bot.js
  2. 1
      src/db/connect.js
  3. 5
      src/handle/runtime/events/ready.js
  4. 46
      src/handle/startup/collect/events.js
  5. 2
      src/handle/startup/run/login.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 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/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/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 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: // feels like there isn't a function name to do this justice :joy:

@ -4,6 +4,7 @@ const mongoose = require('mongoose');
const ora = require('../util/log/ora'); const ora = require('../util/log/ora');
module.exports = async client => { module.exports = async client => {
if (!client.misc) {client.misc = {};}
const auth = client.auth; const auth = client.auth;
const t = Date.now(); const t = Date.now();
client.misc.dbconnected = true; client.misc.dbconnected = true;

@ -0,0 +1,5 @@
const Discord = require('discord.js');
module.exports = async client => {
require('../../startup/run/hello')(client); // startup info
};

@ -1 +1,45 @@
module.exports = async client => {}; 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);
};

@ -17,5 +17,5 @@ module.exports = async (client) => {
client.warn("Discord not connected, considering runtime to be unusable and exiting.", 0, true, true); client.warn("Discord not connected, considering runtime to be unusable and exiting.", 0, true, true);
throw new Error(); 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);
}; };

Loading…
Cancel
Save