galactic-bot/structure/client/components/observers/GuildLogging.js
2020-06-19 00:00:32 +03:00

186 lines
6.3 KiB
JavaScript

const { Observer } = require('../../../interfaces/');
const CONSTANTS = {
COLORS: {
RED: 16711680, // message delete
YELLOW: 15120384, // message edit
LIGHT_BLUE: 11337726 // message pin
}
};
class GuildLogger extends Observer {
constructor(client) {
super(client, {
name: 'guildLogger',
priority: 3
});
this.hooks = [
['message', this.storeAttachment.bind(this)], //Attachment logging
['messageDelete', this.messageDelete.bind(this)],
['messageDeleteBulk', this.messageDeleteBulk.bind(this)],
['messageUpdate', this.messageEdit.bind(this)],
['voiceStateUpdate', this.voiceState.bind(this)],
['guildBanAdd', this.ban.bind(this)],
['guildBanRemove', this.unban.bind(this)],
['guildMemberAdd', this.memberJoin.bind(this)],
['guildMemberRemove', this.memberLeave.bind(this)],
['guildMemberUpdate', this.memberUpdate.bind(this)]
];
}
async storeAttachment(message) {
}
//TODO: Figure this thing out, this should be called from messageDelete and rawMessageDelete if any attachments are present
//data should be an object containing the necessary information to query for the attachment from the db, and the relevant information to log it
//Will figure this out once I get to that point
async logAttachment(data) {
}
async messageDelete(message) {
const { guild } = message;
if (!guild) return;
if (!message.member) message.member = await guild.members.fetch(message.author);
const { member, channel, author } = message;
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 (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 });
}
async messageDeleteBulk(messages) {
}
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 ban(guild, user) {
}
async unban(guild, user) {
}
async memberJoin(member) {
}
async memberLeave(member) {
}
async memberUpdate(oldMember, newMember) {
}
}
module.exports = GuildLogger;