Skip to content

Commit

Permalink
add some safety checks
Browse files Browse the repository at this point in the history
- on the leave ratio checks
fix webhooks
- add info in the server join/leave control embeds
  • Loading branch information
TobiTenno committed Sep 18, 2018
1 parent b300d97 commit 478c40b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 0 additions & 2 deletions main.js
Expand Up @@ -9,11 +9,9 @@ let controlHook;
if (process.env.CONTROL_WH_ID) {
// eslint-disable-next-line global-require
const { WebhookClient } = require('discord.js');

controlHook = new WebhookClient(process.env.CONTROL_WH_ID, process.env.CONTROL_WH_TOKEN);
}


/**
* Raven client instance for logging errors and debugging events
*/
Expand Down
3 changes: 3 additions & 0 deletions src/eventHandlers/JoinNotify.js
Expand Up @@ -37,6 +37,9 @@ class JoinNotify extends Handler {
thumbnail: {
url: guild.iconURL,
},
footer: {
text: guild.id,
},
timestamp: new Date(),
}],
});
Expand Down
12 changes: 10 additions & 2 deletions src/eventHandlers/LeaveNotify.js
Expand Up @@ -19,16 +19,24 @@ class LeaveNotify extends Handler {
*/
async execute(...[guild]) {
this.logger.debug(`Running ${this.id} for ${this.event}. Params: ${guild}`);

const tokens = [
`**Left:** ${guild.name}`,
`**ID:** ${guild.id}`,
`**Owner:** ${guild.owner.user.username}#${guild.owner.user.discriminator} (${guild.ownerID})`,
`**Members:** ${guild.memberCount}`,
];
try {
this.bot.controlHook.send({
embeds: [{
color: 0x00d62e,
title: 'Left Server',
description: `Left: ${guild.name}\nID: ${guild.id}`,
description: tokens.join('\n'),
thumbnail: {
url: guild.iconURL,
},
footer: {
text: guild.id,
},
timestamp: new Date(),
}],
});
Expand Down
2 changes: 1 addition & 1 deletion src/eventHandlers/Ready.js
Expand Up @@ -85,7 +85,7 @@ class OnReadyHandle extends Handler {
*/
async execute() {
this.logger.debug(`Running ${this.id} for ${this.event}`);
if (this.bot.controlHook) {
if (this.bot.controlHook && ((process.env.LOG_LEVEL || 'ERROR').toLowerCase() === 'debug')) {
await this.bot.controlHook.edit(
this.bot.client.user.username,
this.bot.client.user.displayAvatarURL,
Expand Down
30 changes: 19 additions & 11 deletions src/eventHandlers/ReadyRatioCheck.js
Expand Up @@ -14,20 +14,28 @@ async function guildLeave(self) {
const owners = {};
try {
guilds.forEach((row) => {
if (owners[row.owner_id]) {
// owners[row.owner_id].message += `, **${self.client.guilds.get(row.guild_id).name}**`;
owners[row.owner_id].guilds.push(row.guild_id);
} else {
owners[row.owner_id] = {
// message: `**${self.client.guilds.get(row.guild_id).name}**`,
guilds: [row.guild_id],
};
if (self.client.row.guilds.has(row.guild_id)) {
if (owners[row.owner_id]) {
owners[row.owner_id].message += `, **${self.client.guilds.get(row.guild_id).name}**`;
owners[row.owner_id].guilds.push(row.guild_id);
} else {
owners[row.owner_id] = {
message: `**${self.client.guilds.get(row.guild_id).name}**`,
guilds: [row.guild_id],
};
}
}
});
Object.keys(owners).forEach(async (id) => {
// const user = await self.client.fetchUser(id, true);
// const DM = await user.createDM();
// await DM.send(`Your guild${owners[id].guilds.length > 1 ? 's' : ''} ${owners[id].message} are over the bot-to-user ratio. ${self.client.user.username} will now leave. If you want to keep using Genesis please invite more people or kick some bots.`);
const user = self.client.users.has(id)
? self.client.users.get(id)
: await self.client.fetchUser(id, true);
if (user) {
await user.send(`Your guild${owners[id].guilds.length > 1 ? 's' : ''}
${owners[id].message} are over the bot-to-user ratio.
${self.client.user.username} will now leave.
If you want to keep using Genesis please invite more people or kick some bots.`);
}
owners[id].guilds.forEach((guild) => {
if (guild && self.client.guilds.has(guild)) {
self.client.guilds.get(guild).leave();
Expand Down

0 comments on commit 478c40b

Please sign in to comment.