guild based logs

This commit is contained in:
Erik 2020-06-19 21:30:30 +03:00
parent a461ea5b78
commit 2dfa6b42a1

View File

@ -1,10 +1,12 @@
const { Observer } = require('../../../interfaces/');
const { Util } = require('../../../../util');
const CONSTANTS = {
COLORS: {
RED: 16711680, // message delete
YELLOW: 15120384, // message edit
LIGHT_BLUE: 11337726 // message pin
LIGHT_BLUE: 11337726, // message pin
BLUE: 479397
}
};
@ -45,6 +47,8 @@ class GuildLogger extends Observer {
async messageDelete(message) {
if(message.author.bot) return;
const { guild } = message;
if (!guild) return;
@ -56,8 +60,11 @@ class GuildLogger extends Observer {
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;
const logChannel = await guild.resolveChannel(chatlogs.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
if (ignoredRoles && member._roles.length)
for (const role of ignoredRoles)
@ -68,7 +75,7 @@ class GuildLogger extends Observer {
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 }),
title: message.format('MSGLOG_DELETE_TITLE', { author: Util.escapeMarkdown(author.tag), channel: channel.name }),
description: message.content,
footer: {
text: message.format('MSGLOG_DELETE_FOOTER', { msgID: message.id, userID: author.id })
@ -89,6 +96,7 @@ class GuildLogger extends Observer {
// embeds loading in (ex. when a link is posted) would cause a message edit event
if(oldMessage.embeds.length !== newMessage.embeds.length) return;
if(oldMessage.author.bot) return;
const { guild } = oldMessage;
if (!guild) return;
@ -98,11 +106,14 @@ class GuildLogger extends Observer {
const settings = await guild.settings();
const chatlogs = settings.messageLog;
if (!chatlogs || !chatlogs.channel) return;
if (!chatlogs.channel) return;
const { ignoredRoles, ignoredChannels } = chatlogs;
const logChannel = guild.resolveChannel(chatlogs.channel);
if (!logChannel || !logChannel.permissionsFor(guild.me).has('SEND_MESSAGES')) return;
const logChannel = await guild.resolveChannel(chatlogs.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
if (ignoredRoles && member._roles.length)
for (const role of ignoredRoles)
@ -113,7 +124,7 @@ class GuildLogger extends Observer {
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) }),
title: oldMessage.format('MSGLOG_PINNED_TITLE', { author: Util.escapeMarkdown(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
}
@ -124,12 +135,12 @@ class GuildLogger extends Observer {
if(img.height && img.width) embed.image = { url: img.url }
}
await logChannel.send({ embed });
await logChannel.send({ embed }).catch(this.client.logger.error);
} else {
const embed = {
title: oldMessage.format('MSGLOG_EDIT_TITLE', { author: author.tag, channel: channel.name }),
title: oldMessage.format('MSGLOG_EDIT_TITLE', { author: Util.escapeMarkdown(author.tag), channel: channel.name }),
footer: {
text: oldMessage.format('MSGLOG_EDIT_FOOTER', { msgID: oldMessage.id, userID: author.id })
},
@ -137,21 +148,44 @@ class GuildLogger extends Observer {
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)
}
// {
// name: oldMessage.format('MSGLOG_EDIT_OLD'),
// value: oldMessage.content.length > 1024 ? oldMessage.content.substring(0, 1021) + '...' : oldMessage.content
// },
// {
// name: oldMessage.format('MSGLOG_EDIT_NEW'),
// value: newMessage.content.length > 1024 ? newMessage.content.substring(0, 1021) + '...' : newMessage.content
// }
]
};
const oldCon = oldMessage.content,
newCon = newMessage.content;
//Original content
embed.fields.push({
name: oldMessage.format('MSGLOG_EDIT_OLD'),
value: oldCon.length > 1024 ? oldCon.substring(0, 1021) + '...' : oldCon
});
if(oldCon.length > 1024) embed.fields.push({
name: '\u200b',
value: '...' + oldCon.substring(1021)
});
//Edited content
embed.fields.push({
name: oldMessage.format('MSGLOG_EDIT_NEW'),
value: newCon.length > 1024 ? newCon.substring(0, 1021) + '...' : newCon
});
if(newCon.length > 1024) embed.fields.push({
name: '\u200b',
value: '...' + newCon.substring(1021)
})
//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');
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 });
await logChannel.send({ embed }).catch(err => {
this.client.logger.error('Error in message edit:\n' + err.stack)
});
}
@ -159,6 +193,36 @@ class GuildLogger extends Observer {
async voiceState(oldState, newState) {
if(oldState.channel && newState.channel && oldState.channel === newState.channel) return;
const { guild, member } = oldState;
//TODO: add checks for disconnecting bot from vc when left alone in one (music player)
const settings = await guild.settings();
const setting = settings.voiceLog;
if(!setting || !setting.channel) return;
const logChannel = await guild.resolveChannel(setting.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
let index = null;
const langParams = {
nickname: member.nickname ? `\`(${member.nickname})\`` : '',
tag: Util.escapeMarkdown(member.user.tag),
id: member.id,
oldChannel: oldState.channel?.name,
newChannel: newState.channel?.name
};
if(!oldState.channel && newState.channel) index = 'VCLOG_JOIN';
else if(oldState.channel && newState.channel) index = 'VCLOG_SWITCH';
else index = 'VCLOG_LEAVE';
this.client.rateLimiter.queueSend(logChannel, guild.format(index, langParams).trim());
}
async ban(guild, user) {
@ -169,16 +233,85 @@ class GuildLogger extends Observer {
}
_replaceTags(text, member) {
const { user } = member;
return text
.replace(/\{mention\}/g, `<@${member.id}>`)
.replace(/\{tag\}/g, `${Util.escapeMarkdown(user.tag)}`)
.replace(/\{user\}/g, `${user.username}`)
.replace(/\{guildsize\}/g, `${guild.memberCount}`)
.replace(/\{accage\}/g, `${this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)}`) //.replace(/a/, '1')
.replace(/\{id\}/g, `${user.id}`)
.trim();
}
async memberJoin(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.memberLog;
if(!setting.channel) return;
const logChannel = await guild.resolveChannel(setting.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
let { joinMessage } = setting;
joinMessage = this._replaceTags(joinMessage, member);
this.client.rateLimiter.queueSend(logChannel, joinMessage)
}
async memberLeave(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.memberLog;
if(!setting.channel) return;
const logChannel = await guild.resolveChannel(setting.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
let { leaveMessage } = setting;
leaveMessage = this._replaceTags(leaveMessage, member);
this.client.rateLimiter.queueSend(logChannel, leaveMessage)
}
async memberUpdate(oldMember, newMember) {
if(oldMember.nickname === newMember.nickname) return;
const { guild, user } = oldMember;
const settings = await guild.settings();
const setting = settings.nicknameLog;
if(!setting.channel) return;
const logChannel = await guild.resolveChannel(setting.channel);
if(!logChannel) return;
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
const oldNick = oldMember.nickname || oldMember.user.username;
const newNick = newMember.nickname || newMember.user.username;
const embed = {
title: guild.format('NICKLOG_TITLE', { user: Util.escapeMarkdown(user.tag) }),
description: guild.format('NICKLOG_DESCRIPTION', { oldNick, newNick }),
footer: {
text: guild.format('NICKLOG_FOOTER', { id: user.id })
},
color: CONSTANTS.COLORS.BLUE
};
logChannel.send({embed});
}
}