woomy-v2/bot/index.js

192 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-10-20 05:47:05 +00:00
// Copyright 2020 Emily J. / mudkipscience and contributors. Subject to the AGPLv3 license.
const Eris = require('eris-additions')(require('eris'));
const EventHandler = require('./util/handlers/eventHandler');
2020-10-20 08:30:46 +00:00
// const messageHandler = require('./util/handlers/messageHandler');
2020-10-20 05:47:05 +00:00
const Helpers = require('./util/helpers');
const Database = require('./util/database');
const Logger = require('./util/logger');
const sentry = require('@sentry/node');
2020-10-20 08:30:46 +00:00
const fs = require('fs');
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;
2020-10-20 05:47:05 +00:00
class WoomyClient extends Eris.Client {
constructor (token, options) {
super(token, options);
// Important information Woomy needs to access
2020-10-20 05:47:05 +00:00
this.config = config;
this.path = __dirname;
2020-10-20 08:30:46 +00:00
this.version = version;
// Load up our command/event modules
2020-10-20 05:47:05 +00:00
this.commandFiles = read('./commands').filter(file => file.endsWith('.js'));
this.eventFiles = read('./event_modules').filter(file => file.endsWith('.js'));
// Essential modules
2020-10-20 05:47:05 +00:00
this.logger = Logger;
this.db = new Database(this);
2020-10-20 08:30:46 +00:00
this.helpers = new Helpers(this);
this.eventHandler = new EventHandler(this);
// this.messageHandler = new messageHandler(this);
2020-10-20 05:47:05 +00:00
// Collections to store our successfully loaded commands and events in.
2020-10-20 05:47:05 +00:00
this.commands = new Eris.Collection();
this.aliases = new Eris.Collection();
this.cooldowns = new Eris.Collection();
this.eventModules = new Eris.Collection();
}
loadCommands () {
for (const file of this.commandFiles) {
try {
2020-10-21 00:23:23 +00:00
const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
const category = file.substr(0, file.indexOf('/'));
const command = new (require(this.path + '/commands/' + file))(name, category);
this.commands.set(command.name, command);
this.cooldowns.set(command.name, new Map());
command.aliases.forEach(alias => {
this.aliases.set(alias, command.name);
2020-10-20 05:47:05 +00:00
});
} catch (error) {
2020-10-20 10:22:50 +00:00
this.logger.error('COMMAND_LOADER_ERROR', `Failed to load ${file}: ${error}`);
2020-10-20 05:47:05 +00:00
}
}
2020-10-20 08:30:46 +00:00
this.logger.success('COMMAND_LOADER_SUCCESS', `Loaded ${this.commands.size}/${this.commandFiles.length} commands.`);
2020-10-20 05:47:05 +00:00
}
2020-10-20 14:03:16 +00:00
reloadCommands () {
2020-10-21 00:23:23 +00:00
for (const cmd of this.commands) {
2020-10-20 14:03:16 +00:00
try {
2020-10-21 00:23:23 +00:00
2020-10-20 14:03:16 +00:00
} catch (error) {
}
}
}
2020-10-20 05:47:05 +00:00
loadEventModules () {
for (const file of this.eventFiles) {
try {
2020-10-21 00:23:23 +00:00
const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
const category = file.substr(0, file.indexOf('/'));
const event = new (require(this.path + '/event_modules/' + file))(category);
this.eventModules.set(name, event);
2020-10-20 05:47:05 +00:00
} catch (error) {
2020-10-20 10:22:50 +00:00
this.logger.error('EVENT_LOADER_ERROR', `Failed to load ${file}: ${error}`);
2020-10-20 05:47:05 +00:00
}
}
2020-10-20 08:30:46 +00:00
this.logger.success('EVENT_LOADER_SUCCESS', `Loaded ${this.eventModules.size}/${this.eventFiles.length} event modules.`);
2020-10-20 05:47:05 +00:00
}
2020-10-20 14:03:16 +00:00
reloadEventModules () {
for (const file of this.eventFiles) {
try {
2020-10-21 00:23:23 +00:00
2020-10-20 14:03:16 +00:00
} catch (error) {
}
}
}
2020-10-20 08:30:46 +00:00
mainEventListener (wsEvent, param_1, param_2) {
try {
this.eventHandler.handle(wsEvent, param_1, param_2);
} catch (error) {
this.logger.error('MODULE_LISTENER_ERROR', error);
}
2020-10-20 05:47:05 +00:00
}
2020-10-20 08:30:46 +00:00
runReadyModules () {
2020-10-20 05:47:05 +00:00
this.mainEventListener('ready');
}
2020-10-20 08:30:46 +00:00
runErrorModules (error) {
this.mainEventListener('error', error);
}
runMessageCreateModules (message) {
this.mainEventListener('messageCreate', message);
2020-10-20 05:47:05 +00:00
}
2020-10-20 08:30:46 +00:00
runGuildCreateModules (guild) {
this.mainEventListener('guildCreate', guild);
2020-10-20 05:47:05 +00:00
}
2020-10-20 08:30:46 +00:00
runGuildDeleteModules (guild) {
this.mainEventListener('guildDelete', guild);
2020-10-20 05:47:05 +00:00
}
2020-10-20 08:30:46 +00:00
runGuildMemberAddModules (guild, member) {
this.mainEventListener('guildMemberAdd', guild, member);
}
2020-10-20 05:47:05 +00:00
2020-10-20 08:30:46 +00:00
runGuildMemberRemoveModules (guild, member) {
this.mainEventListener('guildMemberRemove', guild, member);
}
runVoiceStateUpdateModules (oldState, newState) {
this.mainEventListener('voiceStateUpdate', oldState, newState);
2020-10-20 05:47:05 +00:00
}
createEventListeners () {
2020-10-20 08:30:46 +00:00
this.on('ready', this.runReadyModules);
this.on('error', this.runErrorModules);
this.on('messageCreate', this.runMessageCreateModules);
this.on('guildCreate', this.runGuildCreateModules);
this.on('guildDelete', this.runGuildDeleteModules);
this.on('guildMemberAdd', this.runGuildMemberAddModules);
this.on('guildMemberRemove', this.runGuildMemberRemoveModules);
this.on('voiceStateUpdate', this.runVoiceStateUpdateModules);
2020-10-20 05:47:05 +00:00
}
}
const client = new WoomyClient(config.token, {
maxShards: 'auto',
defaultImageFormat: 'png',
defaultImageSize: 2048,
intents: [
'guilds',
'guildMembers',
'guildEmojis',
'guildVoiceStates',
'guildMessages',
'guildMessageReactions',
'directMessages',
'directMessageReactions'
]
});
require('./util/prototypes');
client.loadCommands();
client.loadEventModules();
client.createEventListeners();
if (client.config.devmode === true) {
try {
// sentry.init({ dsn: client.config.keys.sentry });
} catch (err) {
client.logger.error('SENTRY_INIT_ERROR', `Sentry failed to initialize: ${err}`);
2020-10-20 05:47:05 +00:00
}
} else {
client.logger.warning('DEVELOPMENT_MODE', 'Running in development mode, some features have been disabled.');
2020-10-20 05:47:05 +00:00
}
client.connect();
// Process exception/promise rejection listeners
process.on('uncaughtException', (error) => {
const errorMsg = error.stack.replace(new RegExp(`${client.path}/`, 'g'), './');
client.logger.error('UNCAUGHT_EXCEPTION_ERROR', errorMsg);
});
2020-10-20 05:47:05 +00:00
process.on('unhandledRejection', err => {
client.logger.error('UNHANDLED_PROMISE_ERROR', err);
});