Wait for main and inbox guilds to become available before initializing the bot
parent
eac3e9e0a8
commit
fcd48d6420
91
src/main.js
91
src/main.js
|
@ -29,18 +29,54 @@ 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
|
||||||
* 2) If alwaysReply is disabled, save that message as a chat message in the thread
|
* 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 (! utils.messageIsOnInboxServer(msg)) return;
|
||||||
if (msg.author.bot) 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
|
// Otherwise just save the messages as "chat" in the logs
|
||||||
thread.saveChatMessage(msg);
|
thread.saveChatMessage(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When we get a private message...
|
* When we get a private message...
|
||||||
* 1) Find the open modmail thread for this user, or create a new one
|
* 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
|
* 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.channel instanceof Eris.PrivateChannel)) return;
|
||||||
if (msg.author.bot) return;
|
if (msg.author.bot) return;
|
||||||
if (msg.type !== 0) return; // Ignore pins etc.
|
if (msg.type !== 0) return; // Ignore pins etc.
|
||||||
|
@ -89,14 +125,14 @@ bot.on('messageCreate', async msg => {
|
||||||
|
|
||||||
if (thread) await thread.receiveUserReply(msg);
|
if (thread) await thread.receiveUserReply(msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When a message is edited...
|
* 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
|
* 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
|
* 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 || ! msg.author) return;
|
||||||
if (msg.author.bot) return;
|
if (msg.author.bot) return;
|
||||||
if (await blocked.isBlocked(msg.author.id)) return;
|
if (await blocked.isBlocked(msg.author.id)) return;
|
||||||
|
@ -124,12 +160,12 @@ bot.on('messageUpdate', async (msg, oldMessage) => {
|
||||||
|
|
||||||
thread.updateChatMessage(msg);
|
thread.updateChatMessage(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When a staff message is deleted in a modmail thread, delete it from the database as well
|
* 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) return;
|
||||||
if (msg.author.bot) return;
|
if (msg.author.bot) return;
|
||||||
if (! utils.messageIsOnInboxServer(msg)) return;
|
if (! utils.messageIsOnInboxServer(msg)) return;
|
||||||
|
@ -139,12 +175,12 @@ bot.on('messageDelete', async msg => {
|
||||||
if (! thread) return;
|
if (! thread) return;
|
||||||
|
|
||||||
thread.deleteChatMessage(msg.id);
|
thread.deleteChatMessage(msg.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When the bot is mentioned on the main server, ping staff in the log channel about it
|
* 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 (! utils.messageIsOnMainServer(msg)) return;
|
||||||
if (! msg.mentions.some(user => user.id === bot.user.id)) return;
|
if (! msg.mentions.some(user => user.id === bot.user.id)) return;
|
||||||
if (msg.author.bot) return;
|
if (msg.author.bot) return;
|
||||||
|
@ -180,10 +216,10 @@ bot.on('messageCreate', async msg => {
|
||||||
if (config.botMentionResponse) {
|
if (config.botMentionResponse) {
|
||||||
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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue