Wait for main and inbox guilds to become available before initializing the bot

master
Dragory 2019-08-13 19:58:05 +03:00
parent eac3e9e0a8
commit fcd48d6420
1 changed files with 208 additions and 177 deletions

View File

@ -29,12 +29,48 @@ const alert = require('./modules/alert');
const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants');
// Once the bot has connected, set the status/"playing" message
bot.on('ready', () => {
bot.editStatus(null, {name: config.status});
console.log('Connected! Now listening to DMs.');
module.exports = {
async start() {
console.log('Connecting to Discord...');
bot.once('ready', async () => {
console.log('Connected, waiting for guilds to become available');
await Promise.all([
...config.mainGuildId.map(id => waitForGuild(id)),
waitForGuild(config.mailGuildId)
]);
console.log('Initializing');
initStatus();
initBaseMessageHandlers();
initPlugins();
console.log('Done! Now listening to DMs.');
});
bot.connect();
}
};
function waitForGuild(guildId) {
if (bot.guilds.has(guildId)) {
return Promise.resolve();
}
return new Promise(resolve => {
bot.on('guildAvailable', guild => {
if (guild.id === guildId) {
resolve();
}
});
});
}
function initStatus() {
bot.editStatus(null, {name: config.status});
}
function initBaseMessageHandlers() {
/**
* When a moderator posts in a modmail thread...
* 1) If alwaysReply is enabled, reply to the user
@ -181,9 +217,9 @@ bot.on('messageCreate', async msg => {
bot.createMessage(msg.channel.id, config.botMentionResponse.replace(/{userMention}/g, `<@${msg.author.id}>`));
}
});
}
module.exports = {
async start() {
function initPlugins() {
// Initialize command manager
const commands = createCommandManager(bot);
@ -194,8 +230,8 @@ module.exports = {
}
}
// Load modules
console.log('Loading plugins...');
// Load plugins
console.log('Loading plugins');
const builtInPlugins = [
reply,
close,
@ -231,9 +267,4 @@ module.exports = {
if (config.updateNotifications) {
updates.startVersionRefreshLoop();
}
// Connect to Discord
console.log('Connecting to Discord...');
await bot.connect();
}
};