Message logging stuff

This commit is contained in:
Erik 2020-06-19 00:00:32 +03:00
parent 8bf33bbff3
commit dd0017c24e
3 changed files with 145 additions and 24 deletions

View File

@ -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;
}

View File

@ -1,6 +1,12 @@
const { Observer } = require('../../../interfaces/'); 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 { class GuildLogger extends Observer {
@ -43,7 +49,7 @@ class GuildLogger extends Observer {
if (!guild) return; if (!guild) return;
if (!message.member) message.member = await guild.members.fetch(message.author); 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 settings = await guild.settings();
const chatlogs = settings.messageLog; const chatlogs = settings.messageLog;
@ -51,7 +57,7 @@ class GuildLogger extends Observer {
const { ignoredRoles, ignoredChannels } = chatlogs; const { ignoredRoles, ignoredChannels } = chatlogs;
const logChannel = guild.resolveChannel(chatlogs.channel); const logChannel = guild.resolveChannel(chatlogs.channel);
if (!logChannel) return; if (!logChannel || !logChannel.permissionsFor(guild.me).has('SEND_MESSAGES')) return;
if (ignoredRoles && member._roles.length) if (ignoredRoles && member._roles.length)
for (const role of ignoredRoles) for (const role of ignoredRoles)
@ -59,9 +65,19 @@ class GuildLogger extends Observer {
if (ignoredChannels && ignoredChannels.includes(channel.id)) return; 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) { 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) { async voiceState(oldState, newState) {

View File

@ -179,15 +179,11 @@ class MessageLogsSetting extends Setting {
if (channel) { //Channel might have been deleted if (channel) { //Channel might have been deleted
const hooks = channel.fetchWebhooks(); const hooks = channel.fetchWebhooks();
const hook = hooks.filter(hook => hook.id === setting.webhook).first(); 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 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 else if (hook) await hook.delete('Removing message logging hook, as message logs were disabled.').catch(this.client.logger.error);
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?
}
} }
} }
@ -201,17 +197,19 @@ class MessageLogsSetting extends Setting {
error: true error: true
}; };
//Handle old webhook if one exists // //Handle old webhook if one exists
const oldChannel = guild.resolveChannel(setting.channel); // const oldChannel = guild.resolveChannel(setting.channel);
if(oldChannel ) // if (oldChannel && chatlogs.webhook && oldChannel.id !== channel.id) {
if (oldChannel && chatlogs.webhook && oldChannel.id !== channel.id) { // const hooks = await oldChannel.fetchWebhooks();
const hooks = await oldChannel.fetchWebhooks(); // const hook = hooks.filter(hook => hook.id === setting.webhook).first();
const hook = hooks.filter(hook => hook.id === setting.webhook).first();
if (hook) { // guild.webhooks.delete(hook.id);
guild.webhooks.delete(hook.id); // if (hook) hook.delete('Removing old webhook').catch(this.client.logger.error);
hook.delete('Removing old webhook').catch(this.client.logger.error); // }
}
} // if(oldChannel && oldChannel.id !== channel.id) {
// }
index = 'S_MESSAGELOG_CHANNEL'; index = 'S_MESSAGELOG_CHANNEL';
changes = channel.name; changes = channel.name;