Skip to content

Commit

Permalink
fix: handle errors if message doesn't exist for delete logs (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
benw202 authored Jan 28, 2024
1 parent c4fc87f commit ed43f3d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 82 deletions.
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Update <small>_ January 2024</small>

- fix: handle errors if message doesn't exist for delete logs (28/01/2024)
- fix: clearer logging for the role assignment handler (28/01/2024)
- fix: interaction already replied in faq print-all (28/01/2024)
- fix: ScamLog - Ignore messages entirely unless it has everyone (28/01/2024)
Expand Down
168 changes: 86 additions & 82 deletions src/events/logging/messageDelete.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,103 @@
import moment from 'moment';
import { AuditLogEvent, Colors, TextChannel } from 'discord.js';
import { constantsConfig, event, Events, imageBaseUrl, makeEmbed } from '../../lib';
import { constantsConfig, event, Events, imageBaseUrl, Logger, makeEmbed } from '../../lib';

const CONTENT_NOT_AVAIL = 'Unable to find content or embeds.';

export default event(Events.MessageDelete, async (_, msg) => {
if (msg.guild === null || msg.author === null) {
try {
if (msg.guild === null || msg.author === null) {
// DMs
return;
}
return;
}

if (msg.content === null) {
// Old Message
return;
}
if (msg.content === null || msg.content.trim() === '') {
// Old Message or empty content
return;
}

const fetchedLogs = await msg.guild.fetchAuditLogs({
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 = msg.guild.channels.resolve(constantsConfig.channels.USER_LOGS) as TextChannel | null;
const messageEmbeds = msg.embeds.length > 0 ? msg.embeds : [];
const messageComponents = [];
if (msg.content) {
messageComponents.push(msg.content);
}
if (msg.attachments) {
msg.attachments.forEach((attachment) => {
if (attachment.url || attachment.proxyURL) {
messageComponents.push(attachment.url ? attachment.url : attachment.proxyURL);
}
const fetchedLogs = await msg.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MessageDelete,
});
}
for (const messageEmbed of messageEmbeds) {
const { image, fields } = messageEmbed;
if (image) {
messageComponents.push(`<${image.url}>`);
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 = msg.guild.channels.resolve(constantsConfig.channels.USER_LOGS) as TextChannel | null;
const messageEmbeds = msg.embeds.length > 0 ? msg.embeds : [];
const messageComponents = [];
if (msg.content) {
messageComponents.push(msg.content);
}
for (const field of fields) {
const { name, value } = field;
if (name && value) {
messageComponents.push(`${name}: ${value}`);
if (msg.attachments) {
msg.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 = msg.reference ? await msg.fetchReference() : null;
const messageDeleteEmbed = makeEmbed({
color: Colors.Red,
thumbnail: { url: `${imageBaseUrl}/moderation/message_deleted.png` },
author: {
name: msg.author.tag,
iconURL: msg.author.displayAvatarURL(),
},
fields: [
{
name: 'Date',
value: formattedDate,
inline: true,
},
{
name: 'Author',
value: `${msg.author}`,
inline: true,
},
{
name: 'Channel',
value: `${msg.channel}`,
inline: true,
},
{
name: 'Reply to',
value: messageReference ? `${messageReference.url}` : 'None',
inline: true,
const messageContent = messageComponents.join('\n');
const messageReference = msg.reference ? await msg.fetchReference() : null;
const messageDeleteEmbed = makeEmbed({
color: Colors.Red,
thumbnail: { url: `${imageBaseUrl}/moderation/message_deleted.png` },
author: {
name: msg.author.tag,
iconURL: msg.author.displayAvatarURL(),
},
{
name: 'Deleted by',
value: (deletionLog && deletionLog.target.id === msg.author.id) ? `${deletionLog.executor}` : 'No audit log was found, message was either deleted by author, or a bot',
inline: false,
},
{
name: 'Deleted Message',
value: messageContent ? `${messageContent}` : CONTENT_NOT_AVAIL,
inline: false,
},
],
footer: { text: `User ID: ${msg.author.id}` },
});
fields: [
{
name: 'Date',
value: formattedDate,
inline: true,
},
{
name: 'Author',
value: `${msg.author}`,
inline: true,
},
{
name: 'Channel',
value: `${msg.channel}`,
inline: true,
},
{
name: 'Reply to',
value: messageReference ? `${messageReference.url}` : 'None',
inline: true,
},
{
name: 'Deleted by',
value: (deletionLog && deletionLog.target.id === msg.author.id) ? `${deletionLog.executor}` : 'No audit log was found, message was either deleted by author, or a bot',
inline: false,
},
{
name: 'Deleted Message',
value: messageContent ? `${messageContent}` : CONTENT_NOT_AVAIL,
inline: false,
},
],
footer: { text: `User ID: ${msg.author.id}` },
});

if (userLogsChannel && !constantsConfig.userLogExclude.includes(msg.author!.id)) {
await userLogsChannel.send({ embeds: [messageDeleteEmbed] });
if (userLogsChannel && !constantsConfig.userLogExclude.includes(msg.author!.id)) {
await userLogsChannel.send({ embeds: [messageDeleteEmbed] });
}
} catch (error) {
Logger.error(error);
}
});

0 comments on commit ed43f3d

Please sign in to comment.