diff --git a/src/app.js b/src/app.js index a3fc983..1ad353d 100644 --- a/src/app.js +++ b/src/app.js @@ -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 => { diff --git a/src/commands/add.js b/src/commands/add.js index 69485fc..b9c9c7c 100644 --- a/src/commands/add.js +++ b/src/commands/add.js @@ -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; diff --git a/src/commands/channel.js b/src/commands/channel.js index 6043c29..343fcc3 100644 --- a/src/commands/channel.js +++ b/src/commands/channel.js @@ -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;