sync changes

This commit is contained in:
Emily 2020-10-20 19:30:46 +11:00
parent ee5de0ccaa
commit 4769669d12
6 changed files with 123 additions and 64 deletions

View file

@ -0,0 +1,12 @@
class Ready {
constructor () {
this.wsEvent;
}
async run (client) {
client.editStatus('online', { name: `Goddess of Discord! | v${client.version}`});
client.logger.event(`Woomy v${client.version} initialized and ready to accept commands!`);
}
}
module.exports = new Ready();

View file

@ -2,14 +2,16 @@
const Eris = require('eris-additions')(require('eris')); const Eris = require('eris-additions')(require('eris'));
const EventHandler = require('./util/handlers/eventHandler'); const EventHandler = require('./util/handlers/eventHandler');
const messageHandler = require('./util/handlers/messageHandler'); // const messageHandler = require('./util/handlers/messageHandler');
const Helpers = require('./util/helpers'); const Helpers = require('./util/helpers');
const Database = require('./util/database'); const Database = require('./util/database');
const Logger = require('./util/logger'); const Logger = require('./util/logger');
const read = require('fs-readdir-recursive');
const sentry = require('@sentry/node'); const sentry = require('@sentry/node');
const config = require('../botconfig.yml'); const fs = require('fs');
const pkg = require('../package.json'); const read = require('fs-readdir-recursive');
const yaml = require('js-yaml');
const config = yaml.safeLoad(fs.readFileSync('../botconfig.yml', 'utf8'));
const version = require('../package.json').version;
class WoomyClient extends Eris.Client { class WoomyClient extends Eris.Client {
constructor (token, options) { constructor (token, options) {
@ -17,13 +19,15 @@ class WoomyClient extends Eris.Client {
this.config = config; this.config = config;
this.path = __dirname; this.path = __dirname;
this.version = pkg.version; this.version = version;
this.commandFiles = read('./commands').filter(file => file.endsWith('.js')); this.commandFiles = read('./commands').filter(file => file.endsWith('.js'));
this.eventFiles = read('./event_modules').filter(file => file.endsWith('.js')); this.eventFiles = read('./event_modules').filter(file => file.endsWith('.js'));
this.logger = Logger; this.logger = Logger;
//this.helpers = new Helpers(this);
this.db = new Database(this); this.db = new Database(this);
this.helpers = new Helpers(this);
this.eventHandler = new EventHandler(this);
// this.messageHandler = new messageHandler(this);
this.commands = new Eris.Collection(); this.commands = new Eris.Collection();
this.aliases = new Eris.Collection(); this.aliases = new Eris.Collection();
@ -47,61 +51,79 @@ class WoomyClient extends Eris.Client {
this.aliases.set(alias, props.help.name); this.aliases.set(alias, props.help.name);
}); });
} catch (error) { } catch (error) {
this.logger.error('COMMAND_LOADER', error); this.logger.error('COMMAND_LOADER_ERROR', error);
} }
} }
this.logger.success('COMMAND_LOADER', `Successfully loaded ${this.commands.size}/${this.commandFiles.length} commands.`); this.logger.success('COMMAND_LOADER_SUCCESS', `Loaded ${this.commands.size}/${this.commandFiles.length} commands.`);
} }
loadEventModules () { loadEventModules () {
const nameRegexp = /[^/]*$/; const nameRegexp = /[^/]*$/;
const catRegexp = /.+?(?=\/)/;
for (const file of this.eventFiles) { for (const file of this.eventFiles) {
try { try {
const event = require(this.path + '/event_modules/' + file)(this); const event = require(this.path + '/event_modules/' + file);
event.wsEvent = catRegexp.exec(file);
this.eventModules.set(nameRegexp.exec(file), event); this.eventModules.set(nameRegexp.exec(file), event);
} catch (error) { } catch (error) {
this.logger.error('EVENT_LOADER', error); this.logger.error('EVENT_LOADER_ERROR', error);
} }
} }
this.logger.success('EVENT_LOADER', `Successfully loaded ${this.eventModules.size}/${this.eventFiles.length} event modules.`); this.logger.success('EVENT_LOADER_SUCCESS', `Loaded ${this.eventModules.size}/${this.eventFiles.length} event modules.`);
} }
mainEventListener (wsEvent, message, other) { mainEventListener (wsEvent, param_1, param_2) {
try {
this.eventHandler.handle(wsEvent, param_1, param_2);
} catch (error) {
this.logger.error('MODULE_LISTENER_ERROR', error);
}
} }
runReadyEvents () { runReadyModules () {
this.mainEventListener('ready'); this.mainEventListener('ready');
} }
runErrorEvents (error) { runErrorModules (error) {
this.mainEventListener('error', null, error); this.mainEventListener('error', error);
} }
runGuildCreateEvents (guild) { runMessageCreateModules (message) {
this.mainEventListener('guildCreate', null, guild); this.mainEventListener('messageCreate', message);
} }
runGuildDeleteEvents (guild) { runGuildCreateModules (guild) {
this.mainEventListener('guildDelete', null, guild); this.mainEventListener('guildCreate', guild);
} }
runGuildMemberAddEvents () { runGuildDeleteModules (guild) {
this.mainEventListener('guildDelete', guild);
}
runGuildMemberAddModules (guild, member) {
this.mainEventListener('guildMemberAdd', guild, member);
}
runGuildMemberRemoveModules (guild, member) {
this.mainEventListener('guildMemberRemove', guild, member);
}
runVoiceStateUpdateModules (oldState, newState) {
this.mainEventListener('voiceStateUpdate', oldState, newState);
} }
createEventListeners () { createEventListeners () {
this.on('ready', ); this.on('ready', this.runReadyModules);
this.on('error') this.on('error', this.runErrorModules);
this.on('messageCreate', this.mainEventLIstener('message', message)); this.on('messageCreate', this.runMessageCreateModules);
this.on('guildCreate', ); this.on('guildCreate', this.runGuildCreateModules);
this.on('guildDelete', ); this.on('guildDelete', this.runGuildDeleteModules);
this.on('guildMemberAdd', ); this.on('guildMemberAdd', this.runGuildMemberAddModules);
this.on('guildMemberRemove', ); this.on('guildMemberRemove', this.runGuildMemberRemoveModules);
this.on('voiceStateUpdate', ); this.on('voiceStateUpdate', this.runVoiceStateUpdateModules);
} }
} }
@ -131,11 +153,13 @@ async function init () {
try { try {
// sentry.init({ dsn: client.config.keys.sentry }); // sentry.init({ dsn: client.config.keys.sentry });
} catch (err) { } catch (err) {
client.logger.error('SENTRY', `Sentry failed to start: ${err}`); client.logger.error('SENTRY_INIT_ERROR', `Sentry failed to initialize: ${err}`);
} }
} else { } else {
client.logger.warning('DEVMODE', 'Running in development mode, some features have been disabled.'); client.logger.warning('DEVELOPMENT_MODE', 'Running in development mode, some features have been disabled.');
} }
client.connect();
} }
init (); init ();

View file

@ -1,46 +1,67 @@
/* eslint-disable indent */
class EventHandler { class EventHandler {
constructor (client) { constructor (client) {
this.client = client; this.client = client;
} }
ready () { handle (wsEvent, param_1, param_2) {
const readyModules = this.client.eventModules.filter(module => module.wsEvent = 'ready'); switch (wsEvent) {
readyModules.forEach(module => module.execute(this.client)); case 'ready': {
const readyModules = this.client.eventModules.filter(module => module.wsEvent === 'ready');
readyModules.forEach(module => module.run(this.client));
break;
} }
error () { // param_1 - error message
const errorModules = this.client.eventModules.filter(module => module.wsEvent = 'error'); case 'error': {
errorModules.forEach(module => module.execute(this.client)); const errorModules = this.client.eventModules.filter(module => module.wsEvent === 'error');
errorModules.forEach(module => module.run(this.client, param_1));
break;
} }
messageCreate () { // param_1 - message object
const mCreateModules = this.client.eventModules.filter(module => module.wsEvent = 'messageCreate'); case 'messageCreate': {
mCreateModules.forEach(module => module.execute(this.client)); const mCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'messageCreate');
mCreateModules.forEach(module => module.run(this.client, param_1));
break;
} }
guildCreate () { // param_1 - guild object
const gCreateModules = this.client.eventModules.filter(module => module.wsEvent = 'guildCreate'); case 'guildCreate': {
gCreateModules.forEach(module => module.execute(this.client)); const gCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'guildCreate');
gCreateModules.forEach(module => module.run(this.client, param_1));
break;
} }
guildDelete () { // param_1 - guild object
const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent = 'guildDelete'); case 'guildDelete': {
gDeleteModules.forEach(module => module.execute(this.client)); const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent === 'guildDelete');
gDeleteModules.forEach(module => module.run(this.client, param_1));
break;
} }
guildMemberAdd () { // param_1 - guild object | param_2 - member object
const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent = 'guildMemberAdd'); case 'guildMemberAdd': {
gMemberAddModules.forEach(module => module.execute(this.client)); const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberAdd');
gMemberAddModules.forEach(module => module.run(this.client, param_1, param_2));
break;
} }
guildMemberRemove () { // param_1 - guild object | param_2 - member object
const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent = 'guildMemberRemove'); case 'guildMemberRemove': {
gMemberRemoveModules.forEach(module => module.execute(this.client)); const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberRemove');
gMemberRemoveModules.forEach(module => module.run(this.client, param_1, param_2));
break;
} }
voiceStateUpdate () { // param_1 - old voice state | param_2 - new voice state
const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent = 'voiceStateUpdate'); case 'voiceStateUpdate': {
vStateUpdateModules.forEach(module => module.execute(this.client)); const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent === 'voiceStateUpdate');
vStateUpdateModules.forEach(module => module.run(this.client));
break;
}
}
} }
} }

View file

@ -1,4 +1,3 @@
const { MessageEmbed } = require('discord.js');
const { inspect, promisify } = require('util'); const { inspect, promisify } = require('util');
class Helpers { class Helpers {
@ -6,6 +5,7 @@ class Helpers {
this.client = client; this.client = client;
} }
/* Rewrite for Eris
userError (channel, cmd, error) { userError (channel, cmd, error) {
const embed = new MessageEmbed() const embed = new MessageEmbed()
.setColor('#EF5350') .setColor('#EF5350')
@ -16,6 +16,7 @@ class Helpers {
channel.send(embed); channel.send(embed);
} }
*/
async getLastMessage (channel) { async getLastMessage (channel) {
const messages = await channel.messages.fetch({ limit: 2 }); const messages = await channel.messages.fetch({ limit: 2 });

View file

@ -77,7 +77,7 @@ class Logger {
* @returns {void} * @returns {void}
*/ */
static event (body) { static event (body) {
console.log(chalk.bold.yellow(`[ ${this._getCurrentTime()} ] [ EVENT ] `) + body); console.log(chalk.bold.blue(`[ ${this._getCurrentTime()} ] [ EVENT ] `) + body);
} }
/** /**

View file

@ -10,6 +10,7 @@
"eris-additions": "^1.4.1", "eris-additions": "^1.4.1",
"eslint": "^7.11.0", "eslint": "^7.11.0",
"fs-readdir-recursive": "^1.1.0", "fs-readdir-recursive": "^1.1.0",
"js-yaml": "^3.14.0",
"pg": "^8.4.1", "pg": "^8.4.1",
"pg-format": "^1.0.4" "pg-format": "^1.0.4"
}, },