woomy-v2/bot/index.js

152 lines
5.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.
2020-10-22 07:36:13 +00:00
const Eris = (require('eris'));
2020-10-21 03:43:05 +00:00
const CommandLoader = require('./util/commandLoader');
const EventLoader = require('./util/eventLoader');
2020-10-20 05:47:05 +00:00
const EventHandler = require('./util/handlers/eventHandler');
2020-10-21 07:06:36 +00:00
const MessageHandler = require('./util/handlers/messageHandler');
2020-10-25 02:10:05 +00:00
const Functions = require('./util/functions');
2020-10-20 05:47:05 +00:00
const Database = require('./util/database');
const Logger = require('./util/logger');
const RichEmbed = require('./util/embed');
2020-10-20 05:47:05 +00:00
const sentry = require('@sentry/node');
const emojis = require('./assets/emojis.json');
const config = require('../botconfig.json');
2020-10-20 08:30:46 +00:00
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;
// Essential modules
this.emojis = emojis;
2020-10-20 05:47:05 +00:00
this.logger = Logger;
this.RichEmbed = RichEmbed;
2020-10-20 05:47:05 +00:00
this.db = new Database(this);
2020-10-25 02:10:05 +00:00
this.functions = new Functions(this);
2020-10-21 03:43:05 +00:00
this.commandLoader = new CommandLoader(this);
this.eventLoader = new EventLoader(this);
2020-10-20 08:30:46 +00:00
this.eventHandler = new EventHandler(this);
2020-10-21 07:06:36 +00:00
this.messageHandler = new MessageHandler(this);
2020-10-20 05:47:05 +00:00
2020-10-21 07:06:36 +00:00
// Collections to store our successfully loaded events and commands in, as well as cooldowns.
2020-10-20 05:47:05 +00:00
this.commands = new Eris.Collection();
this.aliases = new Eris.Collection();
this.eventModules = new Eris.Collection();
2020-10-21 07:06:36 +00:00
this.cooldowns = new Eris.Collection();
2020-10-20 05:47:05 +00:00
}
2020-10-21 03:43:05 +00:00
// Listen for Eris events and pass needed information to the event handler so we can respond to them.
createEventListeners () {
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 14:03:16 +00:00
}
2020-10-21 03:43:05 +00:00
// Recieves information from the per-event listeners, and passes on needed information to the handler
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-21 03:43:05 +00:00
// All the repeated code below just tells the main event listener what information needs to be passed to the event handler
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) {
2020-10-21 03:43:05 +00:00
this.messageHandler.handle(message);
2020-10-20 08:30:46 +00:00
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
}
}
2020-10-21 03:43:05 +00:00
// Initialize our client
const client = new WoomyClient(config.token, {
maxShards: 'auto',
2020-10-24 04:02:14 +00:00
restMode: true,
2020-10-25 02:10:05 +00:00
//getAllUsers: true,
defaultImageSize: 2048,
intents: [
'guilds',
'guildMembers',
'guildEmojis',
'guildVoiceStates',
'guildMessages',
'guildMessageReactions',
2020-10-28 03:06:12 +00:00
'guildMessageTyping'
]
});
2020-10-21 03:43:05 +00:00
// Extensions of native javascript types, *not good practice* but they're useful
require('./util/prototypes');
2020-10-21 03:43:05 +00:00
// Load commands, event modules and create listeners for Eris events (ready, errors, messages, etc)
client.commandLoader.loadCommands();
client.eventLoader.loadEventModules();
client.createEventListeners();
2020-10-21 03:43:05 +00:00
// Development mode is set in botconfig.yml, and disables some stuff if enabled. Imagine how messy Sentry would be without this!
if (client.config.devmode === true) {
try {
2020-10-21 07:06:36 +00:00
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
}
2020-10-21 03:43:05 +00:00
// Login to Discord
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 => {
2020-10-21 07:06:36 +00:00
client.logger.error('UNHANDLED_PROMISE_ERROR', err.stack);
});
// Shut down gracefully when SIGINT is recieved
process.on('SIGINT', () => {
client.functions.shutdown();
});