Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding support for embeds and referenced messages in delete log #488

Merged
merged 5 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Update <small>_ August 2023</small>

Update <small>_ July 2023</small>

- feat: enhancing user delete log with reference if a reply is deleted, and with embed and attachment data (27/07/2023)
- fix: change the checklist image in the checklist command to the correct one (27/07/2023)
- fix: Removing auto-delete of timeout command and response, and fixing bug with untimeout (27/07/2023)
- feat: optimize simbriefdata for input error and check for FBW airframe (11/07/2023)
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] });
}
},
};
Loading