From 8e6891efabca2745746de667d824bfd6ee323036 Mon Sep 17 00:00:00 2001 From: CaptV0rt3x Date: Fri, 9 Aug 2019 23:56:55 +0530 Subject: [PATCH] Add logging for message edits/deletions --- env.json | 3 +++ src/server.js | 33 +++++++++++++++++++++++++++++++++ src/state.js | 1 + 3 files changed, 37 insertions(+) diff --git a/env.json b/env.json index bd4763a..0d499a3 100644 --- a/env.json +++ b/env.json @@ -17,6 +17,9 @@ "DISCORD_LOG_CHANNEL": { "description": "The unique ID for the channel the bot will output logs to." }, + "DISCORD_MSGLOG_CHANNEL": { + "description": "The unique ID for the channel the bot will output message logs to." + }, "DISCORD_MEDIA_CHANNEL": { "description": "The unique ID for the channel the bot will moderate images in." }, diff --git a/src/server.js b/src/server.js index dc2deb9..83b7167 100644 --- a/src/server.js +++ b/src/server.js @@ -38,6 +38,7 @@ function findArray (haystack, arr) { client.on('ready', () => { // Initialize app channels. state.logChannel = client.channels.get(process.env.DISCORD_LOG_CHANNEL); + state.msglogChannel = client.channels.get(process.env.DISCORD_MSGLOG_CHANNEL); state.guild = state.logChannel.guild; logger.info('Bot is now online and connected to server.'); @@ -65,6 +66,38 @@ client.on('guildMemberAdd', (member) => { member.addRole(process.env.DISCORD_RULES_ROLE); }); +client.on('messageDelete', message => { + if (Boolean(message.content) && !message.content.startsWith('.')) { + const deletionEmbed = new discord.RichEmbed() + .setAuthor(message.author.tag, message.author.displayAvatarURL) + .setDescription(`Message deleted in ${message.channel}`) + .addField('Content', message.cleanContent, false) + .setTimestamp() + .setColor('RED'); + + state.msglogChannel.send(deletionEmbed); + logger.info(`${message.author.username} ${message.author} deleted message: ${message.cleanContent}.`); + } +}); + +client.on('messageUpdate', (oldMessage, newMessage) => { + const oldM = oldMessage.cleanContent; + const newM = newMessage.cleanContent; + + if (Boolean(oldM) && Boolean(newM)) { + const editedEmbed = new discord.RichEmbed() + .setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL) + .setDescription(`Message edited in ${oldMessage.channel} [Jump To Message](${newMessage.url})`) + .addField('Before', oldM, false) + .addField('After', newM, false) + .setTimestamp() + .setColor('GREEN'); + + state.msglogChannel.send(editedEmbed); + logger.info(`${oldMessage.author.username} ${oldMessage.author} edited message from: ${oldM} to: ${newM}.`); + } +}); + client.on('message', message => { if (message.author.bot && message.content.startsWith('.ban') === false) { return; } diff --git a/src/state.js b/src/state.js index 4a337a2..704ad84 100644 --- a/src/state.js +++ b/src/state.js @@ -2,6 +2,7 @@ const State = function () { this.guild = null; this.logChannel = null; + this.msglogChannel = null; this.warnings = []; this.responses = null; this.bans = [];