Skip to content

Commit

Permalink
feat: Adding support for embeds and referenced messages in delete log (
Browse files Browse the repository at this point in the history
  • Loading branch information
pdellaert authored Aug 27, 2023
1 parent a0b98c5 commit d26db7d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Update <small>_ August 2023</small>

- feat: added .simbridgedebug command which explains debugging steps, if SimBridge crashes right after starting (07/08/2023)
- feat: enhancing user delete log with reference if a reply is deleted, and with embed and attachment data (27/08/2023)
- feat: added .simbridgedebug command which explains debugging steps, if SimBridge crashes right after starting (27/08/2023)

Update <small>_ July 2023</small>

Expand Down
92 changes: 41 additions & 51 deletions src/handlers/messagedelete.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Colors, AuditLogEvent, TextChannel } from 'discord.js';
import { Colors, AuditLogEvent, TextChannel, Message } from 'discord.js';
import moment from 'moment';
import { Channels, UserLogExclude } from '../constants';
import { makeEmbed } from '../lib/embed';

const FEATURE_NOT_AVAIL = '(can\'t show embeds or images)';
const CONTENT_NOT_AVAIL = 'Unable to find content or embeds.';

module.exports = {
event: 'messageDelete',
executor: async (message) => {
executor: async (message: Message) => {
if (message.guild === null) {
// DMs
return;
Expand All @@ -22,55 +22,49 @@ module.exports = {
limit: 1,
type: AuditLogEvent.MessageDelete,
});

const deletionLog = fetchedLogs.entries.first();

const currentDate = new Date();
const formattedDate: string = moment(currentDate).utcOffset(0).format('DD, MM, YYYY, HH:mm:ss');

const userLogsChannel = message.guild.channels.resolve(Channels.USER_LOGS) as TextChannel | null;

const messageDeleteEmbedNoLog = (formattedDate) => makeEmbed({
const messageEmbeds = message.embeds.length > 0 ? message.embeds : [];
const messageComponents = [];
if (message.content) {
messageComponents.push(message.content);
}
if (message.attachments) {
message.attachments.forEach((attachment) => {
if (attachment.url || attachment.proxyURL) {
messageComponents.push(attachment.url ? attachment.url : attachment.proxyURL);
}
});
}
for (const messageEmbed of messageEmbeds) {
const { image, fields } = messageEmbed;
if (image) {
messageComponents.push(`<${image.url}>`);
}
for (const field of fields) {
const { name, value } = field;
if (name && value) {
messageComponents.push(`${name}: ${value}`);
}
}
}
const messageContent = messageComponents.join('\n');
const messageReference = message.reference ? await message.fetchReference() : null;
const messageDeleteEmbed = makeEmbed({
color: Colors.Red,
thumbnail: { url: 'https://cdn.discordapp.com/attachments/770835189419999262/779946282373873694/150-1509174_deleted-message-icon-sign-hd-png-download.png' },
author: {
name: message.author.tag,
iconURL: message.author.displayAvatarURL({ dynamic: true }),
iconURL: message.author.displayAvatarURL(),
},
fields: [
{
name: 'Author',
value: `${message.author}`,
inline: true,
},
{
name: 'Channel',
value: `${message.channel}`,
name: 'Date',
value: formattedDate,
inline: true,
},
{
name: 'Deleted by',
value: 'No audit log was found, message was either deleted by author, or a bot',
inline: false,
},
{
name: 'Deleted Message',
value: message.content ? `${message.content}` : FEATURE_NOT_AVAIL,
inline: false,
},
{ name: 'Date', value: formattedDate, inline: false },
],
footer: { text: `User ID: ${message.author.id}` },
});

const messageDeleteEmbed = (executor, formattedDate) => makeEmbed({
color: Colors.Red,
thumbnail: { url: 'https://cdn.discordapp.com/attachments/770835189419999262/779946282373873694/150-1509174_deleted-message-icon-sign-hd-png-download.png' },
author: {
name: message.author.tag,
iconURL: message.author.displayAvatarURL({ dynamic: true }),
},
fields: [
{
name: 'Author',
value: `${message.author}`,
Expand All @@ -81,31 +75,27 @@ module.exports = {
value: `${message.channel}`,
inline: true,
},
{
name: 'Reply to',
value: messageReference ? `${messageReference.url}` : 'None',
inline: true,
},
{
name: 'Deleted by',
value: `${executor}`,
value: (deletionLog && deletionLog.target.id === message.author.id) ? `${deletionLog.executor}` : 'No audit log was found, message was either deleted by author, or a bot',
inline: false,
},
{
name: 'Deleted Message',
value: message.content ? `${message.content}` : FEATURE_NOT_AVAIL,
value: messageContent ? `${messageContent}` : CONTENT_NOT_AVAIL,
inline: false,
},
{ name: 'Date', value: formattedDate, inline: false },
],
footer: { text: `User ID: ${message.author.id}` },
});

if (userLogsChannel && !UserLogExclude.some((e) => e === message.author.id)) {
if (!deletionLog) await userLogsChannel.send({ embeds: [messageDeleteEmbedNoLog(formattedDate)] });

const { executor, target } = deletionLog;

if (target.id === message.author.id) {
await userLogsChannel.send({ embeds: [messageDeleteEmbed(executor, formattedDate)] });
} else {
await userLogsChannel.send({ embeds: [messageDeleteEmbedNoLog(formattedDate)] });
}
await userLogsChannel.send({ embeds: [messageDeleteEmbed] });
}
},
};

0 comments on commit d26db7d

Please sign in to comment.