From dd0017c24e21d0fe9e7d18055e0aa11caec18377 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 19 Jun 2020 00:00:32 +0300 Subject: [PATCH] Message logging stuff --- .../languages/en_us/observers/en_us_logs.lang | 39 ++++++++ .../components/observers/GuildLogging.js | 94 ++++++++++++++++++- .../settings/moderation/MessageLog.js | 36 ++++--- 3 files changed, 145 insertions(+), 24 deletions(-) create mode 100644 language/languages/en_us/observers/en_us_logs.lang diff --git a/language/languages/en_us/observers/en_us_logs.lang b/language/languages/en_us/observers/en_us_logs.lang new file mode 100644 index 0000000..0db0568 --- /dev/null +++ b/language/languages/en_us/observers/en_us_logs.lang @@ -0,0 +1,39 @@ +[MSGLOG_DELETE_TITLE] +🗑️ {author}'s message was deleted in #{channel} + +[MSGLOG_DELETE_FOOTER] +Message ID: {msgID} | User ID: {userID} + +[MSGLOG_EDIT_TITLE] +📝 {author} edited their message in #{channel} + +[MSGLOG_EDIT_FOOTER] +Message ID: {msgID} | User ID: {userID} + +[MSGLOG_EDIT_OLD] +Old message + +[MSGLOG_EDIT_NEW] +New message + +[MSGLOG_EDIT_OLD_CUTOFF] +The original message was cut off at 1024 characters. + +[MSGLOG_EDIT_NEW_CUTOFF] +The new message is cut off at 1024 characters. + +[MSGLOG_EDIT_JUMP] +[Jump to message](https://discordapp.com/channels/{guild}/{channel}/{message}) + +[MSGLOG_PINNED_TITLE] +📌 {author}'s message was {pinned} in #{channel} + +[PIN_TOGGLE] +switch({toggle}) { + case true: + 'pinned'; + break; + default: + 'unpinned'; + break; +} \ No newline at end of file diff --git a/structure/client/components/observers/GuildLogging.js b/structure/client/components/observers/GuildLogging.js index cb81aa3..ff28621 100644 --- a/structure/client/components/observers/GuildLogging.js +++ b/structure/client/components/observers/GuildLogging.js @@ -1,6 +1,12 @@ const { Observer } = require('../../../interfaces/'); -const CONSTANTS = {}; +const CONSTANTS = { + COLORS: { + RED: 16711680, // message delete + YELLOW: 15120384, // message edit + LIGHT_BLUE: 11337726 // message pin + } +}; class GuildLogger extends Observer { @@ -43,7 +49,7 @@ class GuildLogger extends Observer { if (!guild) return; if (!message.member) message.member = await guild.members.fetch(message.author); - const { member, channel } = message; + const { member, channel, author } = message; const settings = await guild.settings(); const chatlogs = settings.messageLog; @@ -51,7 +57,7 @@ class GuildLogger extends Observer { const { ignoredRoles, ignoredChannels } = chatlogs; const logChannel = guild.resolveChannel(chatlogs.channel); - if (!logChannel) return; + if (!logChannel || !logChannel.permissionsFor(guild.me).has('SEND_MESSAGES')) return; if (ignoredRoles && member._roles.length) for (const role of ignoredRoles) @@ -59,9 +65,19 @@ class GuildLogger extends Observer { if (ignoredChannels && ignoredChannels.includes(channel.id)) return; - if (message.attachments.size) return this.logAttachment(); + if (message.attachments.size) return this.logAttachment({ msgID: message.id, guildID: guild.id, channelID: channel.id, logChannel }); - + const embed = { + title: message.format('MSGLOG_DELETE_TITLE', { author: author.tag, channel: channel.name }), + description: message.content, + footer: { + text: message.format('MSGLOG_DELETE_FOOTER', { msgID: message.id, userID: author.id }) + }, + color: CONSTANTS.COLORS.RED, + timestamp: message.createdAt + }; + + await logChannel.send({ embed }); } @@ -71,6 +87,74 @@ class GuildLogger extends Observer { async messageEdit(oldMessage, newMessage) { + // embeds loading in (ex. when a link is posted) would cause a message edit event + if(oldMessage.embeds.length !== newMessage.embeds.length) return; + + const { guild } = oldMessage; + if (!guild) return; + + if (!oldMessage.member) oldMessage.member = await guild.members.fetch(oldMessage.author); + const { member, channel, author } = oldMessage; + + const settings = await guild.settings(); + const chatlogs = settings.messageLog; + if (!chatlogs || !chatlogs.channel) return; + + const { ignoredRoles, ignoredChannels } = chatlogs; + const logChannel = guild.resolveChannel(chatlogs.channel); + if (!logChannel || !logChannel.permissionsFor(guild.me).has('SEND_MESSAGES')) return; + + if (ignoredRoles && member._roles.length) + for (const role of ignoredRoles) + if (member._roles.includes(role)) return; + + if (ignoredChannels && ignoredChannels.includes(channel.id)) return; + + if(oldMessage.content === newMessage.content && oldMessage.pinned !== newMessage.pinned) { + + const embed = { + title: oldMessage.format('MSGLOG_PINNED_TITLE', { author: author.tag, channel: channel.name, pinned: oldMessage.format('PIN_TOGGLE', { toggle: newMessage.pinned }, true) }), + description: oldMessage.format('MSGLOG_EDIT_JUMP', { guild: guild.id, channel: channel.id, message: oldMessage.id }), + color: CONSTANTS.COLORS.LIGHT_BLUE + } + + if(oldMessage.content.length) embed.description += '\n' + oldMessage.content.substring(0, 1900); + if(oldMessage.attachments.size) { + const img = oldMessage.attachments.first(); + if(img.height && img.width) embed.image = { url: img.url } + } + + await logChannel.send({ embed }); + + } else { + + const embed = { + title: oldMessage.format('MSGLOG_EDIT_TITLE', { author: author.tag, channel: channel.name }), + footer: { + text: oldMessage.format('MSGLOG_EDIT_FOOTER', { msgID: oldMessage.id, userID: author.id }) + }, + description: oldMessage.format('MSGLOG_EDIT_JUMP', { guild: guild.id, channel: channel.id, message: oldMessage.id }), + color: CONSTANTS.COLORS.YELLOW, + timestamp: oldMessage.createdAt, + fields: [ + { + name: oldMessage.format('MSGLOG_EDIT_OLD'), + value: oldMessage.content.substring(0, 1024) + }, + { + name: oldMessage.format('MSGLOG_EDIT_NEW'), + value: newMessage.content.substring(0, 1024) + } + ] + }; + + if(oldMessage.content.length > 1024) embed.description += '\n' + oldMessage.format('MSGLOG_EDIT_OLD_CUTOFF'); + if(newMessage.content.length > 1024) embed.description += '\n' + oldMessage.format('MSGLOG_EDIT_NEW_CUTOFF'); + + await logChannel.send({ embed }); + + } + } async voiceState(oldState, newState) { diff --git a/structure/client/components/settings/moderation/MessageLog.js b/structure/client/components/settings/moderation/MessageLog.js index c0f509d..f0c39b4 100644 --- a/structure/client/components/settings/moderation/MessageLog.js +++ b/structure/client/components/settings/moderation/MessageLog.js @@ -179,15 +179,11 @@ class MessageLogsSetting extends Setting { if (channel) { //Channel might have been deleted const hooks = channel.fetchWebhooks(); const hook = hooks.filter(hook => hook.id === setting.webhook).first(); + guild.webhooks.delete(setting.webhook); + setting.webhook = null; + if (hook && !channel.permissionsFor(guild.me).has('MANAGE_WEBHOOKS')) index = 'S_MESSAGELOG_TOGGLE_PERM' //missing perms - else try { //If necessary perms and a hook exists, remove it and uncache it from the main collection - if (hook) await hook.delete('Removing message logging hook, as message logs were disabled.'); - guild.webhooks.delete(setting.webhook); - setting.webhook = null; - } catch (err) { - this.client.logger.error(err); - if (err.code === 10015) setting.webhook = null; // webhook is somehow missing prior to the bot deleting it? - } + else if (hook) await hook.delete('Removing message logging hook, as message logs were disabled.').catch(this.client.logger.error); } } @@ -201,17 +197,19 @@ class MessageLogsSetting extends Setting { error: true }; - //Handle old webhook if one exists - const oldChannel = guild.resolveChannel(setting.channel); - if(oldChannel ) - if (oldChannel && chatlogs.webhook && oldChannel.id !== channel.id) { - const hooks = await oldChannel.fetchWebhooks(); - const hook = hooks.filter(hook => hook.id === setting.webhook).first(); - if (hook) { - guild.webhooks.delete(hook.id); - hook.delete('Removing old webhook').catch(this.client.logger.error); - } - } + // //Handle old webhook if one exists + // const oldChannel = guild.resolveChannel(setting.channel); + // if (oldChannel && chatlogs.webhook && oldChannel.id !== channel.id) { + // const hooks = await oldChannel.fetchWebhooks(); + // const hook = hooks.filter(hook => hook.id === setting.webhook).first(); + + // guild.webhooks.delete(hook.id); + // if (hook) hook.delete('Removing old webhook').catch(this.client.logger.error); + // } + + // if(oldChannel && oldChannel.id !== channel.id) { + + // } index = 'S_MESSAGELOG_CHANNEL'; changes = channel.name;