Skip to content

Commit

Permalink
Added status change notification.
Browse files Browse the repository at this point in the history
Currently this feature doesn't properly work because of the discord.js library, it seems there's a bug that makes oldMember and newMember inside the presenceUpdate event handler have the same presence. discordjs/discord.js#3214
  • Loading branch information
moonstar-x committed Apr 19, 2019
1 parent 6a6ce44 commit 70134a3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
41 changes: 40 additions & 1 deletion src/app.js
Expand Up @@ -102,7 +102,46 @@ client.on('message', async message => {
});

client.on('presenceUpdate', (oldMember, newMember) => {
// Handle notifications.
logger.debug(oldMember.displayName, oldMember.presence.status, newMember.presence.status)

// Check if we're interested in this member's presenceUpdate.
if (oldMember.presence.status == newMember.presence.status) return;

// Check if the member updating does correspond to one stored in the realm.
const realmEntry = realm.objects('Guilds').filtered(`id = "${oldMember.guild.id}"`);

// Check if realmEntry is empty, which means it does not correspond to a stored guild.
if (realmEntry.length == 0) return;

// Check if member is stored in the realmEntry.
const id = realmEntry[0].listening.find( entry => entry == oldMember.id);
if (!id) return;

// Check if status changed.
let messageToSend = undefined;
if (oldMember.presence.status == 'online' && newMember.presence.status == 'offline') {
messageToSend = `The bot ${oldMember} has gone offline.`;
} else if (oldMember.presence.status != 'online' && newMember.presence.status == 'online') {
messageToSend = `The bot ${newMember} is now online.`;
}

// Here, we can assume that the realm entry will have a channel id stored.
const channelID = realmEntry[0].channel;

// Send the notification and handle permissions and unknown channel errors.
client.channels.fetch(channelID)
.then( channel => channel.send(messageToSend))
.catch( err => {
if (err == 'DiscordAPIError: Unknown Channel') {
oldMember.guild.owner.send(`The bot **${oldMember.displayName}** in the server **${oldMember.guild.name}** has changed its status but I couldn't send a message to the channel you set-up previously. It has probably been deleted. Please, change the broadcasting channel in **${oldMember.guild.name}** with **${config.prefix}channel** and mention the channel you want to set-up.`);
} else if (err == 'DiscordAPIError: Missing Permissions') {
oldMember.guild.owner.send(`The bot **${oldMember.displayName}** in the server **${oldMember.guild.name}** has changed its status but I couldn't send a message to the channel you set-up previously because I don't have permissions to send messages there. Please, allow me to send messages in the set channel or change the broadcasting channel in **${oldMember.guild.name}** with **${config.prefix}channel** and mention the channel you want to set-up.`);
} else {
oldMember.guild.owner.send(`The bot **${oldMember.displayName}** in the server **${oldMember.guild.name}** has changed its status but I couldn't send a message to the channel you set-up because something unexpected went wrong.`);
logger.error(`Something went wrong when handling ${oldMember.guild.name}'s ${oldMember.displayName} presence update.`);
logger.error(err);
}
});
});

client.on('guildCreate', guild => {
Expand Down
2 changes: 0 additions & 2 deletions src/commands/add.js
Expand Up @@ -15,8 +15,6 @@ module.exports = {
}

// Check if there's a channel entry in realm.
// Technically this check is not necessary, because we've designed this so that the initial guild entry
// is added when a valid text channel is specified in the argument of the channel command.
if (!realmEntry[0].channel) {
message.reply(`before adding any bots, you need to define in which channel I should send the notifications. Please define the broadcasting text channel with **${options.config.prefix}channel** and mention the text channel you want to set.`);
return;
Expand Down
2 changes: 0 additions & 2 deletions src/commands/channel.js
Expand Up @@ -14,8 +14,6 @@ module.exports = {
message.reply(`a broadcasting text channel is yet to be defined. You can define one by running **${options.config.prefix}channel** and mentioning the text channel you want to set.`);
} else {
// Check if there's a channel realm entry.
// Technically this check is not necessary, because we've designed this so that the initial guild entry
// is added when a valid text channel is specified in the argument.
if (!realmEntry[0].channel) {
message.reply(`a broadcasting text channel is yet to be defined. You can define one by running **${options.config.prefix}channel** and mentioning the text channel you want to set.`);
return;
Expand Down

0 comments on commit 70134a3

Please sign in to comment.