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,18 +29,54 @@ 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
* 2) If alwaysReply is disabled, save that message as a chat message in the thread
*/
bot.on('messageCreate', async msg => {
bot.on('messageCreate', async msg => {
if (! utils.messageIsOnInboxServer(msg)) return;
if (msg.author.bot) return;
@ -61,14 +97,14 @@ bot.on('messageCreate', async msg => {
// Otherwise just save the messages as "chat" in the logs
thread.saveChatMessage(msg);
}
});
});
/**
/**
* When we get a private message...
* 1) Find the open modmail thread for this user, or create a new one
* 2) Post the message as a user reply in the thread
*/
bot.on('messageCreate', async msg => {
bot.on('messageCreate', async msg => {
if (! (msg.channel instanceof Eris.PrivateChannel)) return;
if (msg.author.bot) return;
if (msg.type !== 0) return; // Ignore pins etc.
@ -89,14 +125,14 @@ bot.on('messageCreate', async msg => {
if (thread) await thread.receiveUserReply(msg);
});
});
});
/**
/**
* When a message is edited...
* 1) If that message was in DMs, and we have a thread open with that user, post the edit as a system message in the thread
* 2) If that message was moderator chatter in the thread, update the corresponding chat message in the DB
*/
bot.on('messageUpdate', async (msg, oldMessage) => {
bot.on('messageUpdate', async (msg, oldMessage) => {
if (! msg || ! msg.author) return;
if (msg.author.bot) return;
if (await blocked.isBlocked(msg.author.id)) return;
@ -124,12 +160,12 @@ bot.on('messageUpdate', async (msg, oldMessage) => {
thread.updateChatMessage(msg);
}
});
});
/**
/**
* When a staff message is deleted in a modmail thread, delete it from the database as well
*/
bot.on('messageDelete', async msg => {
bot.on('messageDelete', async msg => {
if (! msg.author) return;
if (msg.author.bot) return;
if (! utils.messageIsOnInboxServer(msg)) return;
@ -139,12 +175,12 @@ bot.on('messageDelete', async msg => {
if (! thread) return;
thread.deleteChatMessage(msg.id);
});
});
/**
/**
* When the bot is mentioned on the main server, ping staff in the log channel about it
*/
bot.on('messageCreate', async msg => {
bot.on('messageCreate', async msg => {
if (! utils.messageIsOnMainServer(msg)) return;
if (! msg.mentions.some(user => user.id === bot.user.id)) return;
if (msg.author.bot) return;
@ -180,10 +216,10 @@ bot.on('messageCreate', async msg => {
if (config.botMentionResponse) {
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();
}
};
}