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'); const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants');
// Once the bot has connected, set the status/"playing" message module.exports = {
bot.on('ready', () => { async start() {
bot.editStatus(null, {name: config.status}); console.log('Connecting to Discord...');
console.log('Connected! Now listening to DMs.');
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... * When a moderator posts in a modmail thread...
* 1) If alwaysReply is enabled, reply to the user * 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}>`)); bot.createMessage(msg.channel.id, config.botMentionResponse.replace(/{userMention}/g, `<@${msg.author.id}>`));
} }
}); });
}
module.exports = { function initPlugins() {
async start() {
// Initialize command manager // Initialize command manager
const commands = createCommandManager(bot); const commands = createCommandManager(bot);
@ -194,8 +230,8 @@ module.exports = {
} }
} }
// Load modules // Load plugins
console.log('Loading plugins...'); console.log('Loading plugins');
const builtInPlugins = [ const builtInPlugins = [
reply, reply,
close, close,
@ -231,9 +267,4 @@ module.exports = {
if (config.updateNotifications) { if (config.updateNotifications) {
updates.startVersionRefreshLoop(); updates.startVersionRefreshLoop();
} }
// Connect to Discord
console.log('Connecting to Discord...');
await bot.connect();
} }
};