2020-10-20 05:47:05 +00:00
|
|
|
// Copyright 2020 Emily J. / mudkipscience and contributors. Subject to the AGPLv3 license.
|
|
|
|
|
2021-07-15 02:26:02 +00:00
|
|
|
const Discord = require('discord.js');
|
2020-10-21 03:43:05 +00:00
|
|
|
const CommandLoader = require('./util/commandLoader');
|
|
|
|
const EventLoader = require('./util/eventLoader');
|
2022-12-09 12:41:29 +00:00
|
|
|
const EventHandler = require('./event_modules/eventHandler');
|
2020-10-25 02:10:05 +00:00
|
|
|
const Functions = require('./util/functions');
|
2022-12-09 12:41:29 +00:00
|
|
|
const Database = require('./util/database');
|
2020-10-20 05:47:05 +00:00
|
|
|
const Logger = require('./util/logger');
|
|
|
|
const sentry = require('@sentry/node');
|
2021-03-02 23:42:45 +00:00
|
|
|
const config = require('../botconfig.json');
|
2020-10-20 08:30:46 +00:00
|
|
|
const version = require('../package.json').version;
|
2021-07-15 02:26:02 +00:00
|
|
|
class WoomyClient extends Discord.Client {
|
|
|
|
constructor (options) {
|
|
|
|
super(options);
|
2020-10-20 05:47:05 +00:00
|
|
|
|
2020-10-20 13:44:52 +00:00
|
|
|
// 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;
|
2020-10-20 13:44:52 +00:00
|
|
|
|
|
|
|
// Essential modules
|
2020-10-20 05:47:05 +00:00
|
|
|
this.logger = Logger;
|
2021-07-16 03:21:09 +00:00
|
|
|
this.MessageEmbed = Discord.MessageEmbed;
|
2022-12-09 12:41:29 +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-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.
|
2021-07-15 02:26:02 +00:00
|
|
|
this.commands = new Discord.Collection();
|
|
|
|
this.aliases = new Discord.Collection();
|
|
|
|
this.eventModules = new Discord.Collection();
|
|
|
|
this.cooldowns = new Discord.Collection();
|
2020-10-20 05:47:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 02:26:02 +00:00
|
|
|
// Listen for Discord events and pass needed information to the event handler so we can respond to them.
|
2020-10-21 03:43:05 +00:00
|
|
|
createEventListeners () {
|
|
|
|
this.on('ready', this.runReadyModules);
|
|
|
|
this.on('error', this.runErrorModules);
|
2022-12-09 13:21:45 +00:00
|
|
|
this.on('interactionCreate', this.runInteractionCreateModules);
|
2020-10-21 03:43:05 +00:00
|
|
|
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
|
|
|
|
2022-12-05 22:44:25 +00:00
|
|
|
// Receives 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);
|
|
|
|
}
|
|
|
|
|
2022-12-09 12:41:29 +00:00
|
|
|
runInteractionCreateModules (interaction) {
|
|
|
|
this.mainEventListener('interactionCreate', interaction);
|
|
|
|
}
|
|
|
|
|
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
|
2021-07-15 02:26:02 +00:00
|
|
|
const client = new WoomyClient({
|
|
|
|
shards: 'auto',
|
2020-10-20 13:44:52 +00:00
|
|
|
intents: [
|
2022-12-05 22:44:25 +00:00
|
|
|
Discord.GatewayIntentBits.Guilds,
|
|
|
|
Discord.GatewayIntentBits.GuildMembers,
|
|
|
|
Discord.GatewayIntentBits.GuildEmojisAndStickers,
|
|
|
|
Discord.GatewayIntentBits.GuildVoiceStates,
|
2022-12-09 13:14:24 +00:00
|
|
|
],
|
|
|
|
allowedMentions: {
|
|
|
|
parse: [
|
|
|
|
'users',
|
|
|
|
'roles'
|
|
|
|
]
|
|
|
|
}
|
2020-10-20 13:44:52 +00:00
|
|
|
});
|
2022-12-05 22:44:25 +00:00
|
|
|
|
2020-10-21 03:43:05 +00:00
|
|
|
// Extensions of native javascript types, *not good practice* but they're useful
|
2020-10-20 13:44:52 +00:00
|
|
|
require('./util/prototypes');
|
|
|
|
|
2021-07-15 02:26:02 +00:00
|
|
|
// Load commands, event modules and create listeners for Discord events (ready, errors, messages, etc)
|
2020-10-21 03:43:05 +00:00
|
|
|
client.commandLoader.loadCommands();
|
|
|
|
client.eventLoader.loadEventModules();
|
2020-10-20 13:44:52 +00:00
|
|
|
client.createEventListeners();
|
|
|
|
|
2022-12-05 22:44:25 +00:00
|
|
|
// Development mode is set in botconfig.json
|
2021-03-13 03:32:17 +00:00
|
|
|
if (client.config.developmentMode === false) {
|
2020-10-20 13:44:52 +00:00
|
|
|
try {
|
2020-10-21 07:06:36 +00:00
|
|
|
sentry.init({ dsn: client.config.keys.sentry });
|
2020-10-20 13:44:52 +00:00
|
|
|
} catch (err) {
|
2021-03-13 03:32:17 +00:00
|
|
|
client.logger.error('SENTRY_INIT_ERROR', `Sentry failed to initialize: ${err}`);
|
2020-10-20 05:47:05 +00:00
|
|
|
}
|
2020-10-20 13:44:52 +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
|
2021-07-15 02:26:02 +00:00
|
|
|
client.login(config.token);
|
2020-10-20 13:44:52 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-10-20 13:44:52 +00:00
|
|
|
process.on('unhandledRejection', err => {
|
2020-10-21 07:06:36 +00:00
|
|
|
client.logger.error('UNHANDLED_PROMISE_ERROR', err.stack);
|
2021-03-01 22:18:00 +00:00
|
|
|
});
|
|
|
|
|
2022-12-09 12:41:29 +00:00
|
|
|
// Shut down gracefully when SIGINT is received
|
2021-03-01 22:18:00 +00:00
|
|
|
process.on('SIGINT', () => {
|
|
|
|
client.functions.shutdown();
|
2020-10-20 13:44:52 +00:00
|
|
|
});
|