Obelisque
Obelisque
DIAdiscord.js - Imagine an app
Created by Obelisque on 2/29/2024 in #djs-questions
Fetch deleted messages by discord upon a user ban
Currently I am fetching all deleted messages which were deleted manually. Unfortunately that does not work when I ban a user and have the messages automatically deleted that this user posted by the day.
const client = new Client({
intents: [
[...]
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessageTyping,
[...]
],
partials: [Partials.Message, Partials.Channel, Partials.Reactions]
});

/**
* Message Handler if a message is deleted
*/
client.on('messageDelete', async message => {
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))

if( message.content && message.content.length > 0 ) {
console.log( 'Message deleted...' );
let content = message.content;
if( message.attachments.size > 0 ){
message.attachments.forEach( att => { content = `${content}\n[${att.name}](${att.attachment}) `; });
}

await sleep(1000);
const logs = await message.guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete, limit: 5 });
const auditentry = logs.entries.find(a =>
a.target.id === message.author.id &&
a.extra.channel.id === message.channel.id &&
Date.now() - a.createdTimestamp < 20000
);

// here is a check for certain channels and roles which I do not want to log

try {
let helpEmbed = new EmbedBuilder();
//here is the message created and sent to the channel
}catch(error) {
console.debug( error );
}
}
});
const client = new Client({
intents: [
[...]
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessageTyping,
[...]
],
partials: [Partials.Message, Partials.Channel, Partials.Reactions]
});

/**
* Message Handler if a message is deleted
*/
client.on('messageDelete', async message => {
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))

if( message.content && message.content.length > 0 ) {
console.log( 'Message deleted...' );
let content = message.content;
if( message.attachments.size > 0 ){
message.attachments.forEach( att => { content = `${content}\n[${att.name}](${att.attachment}) `; });
}

await sleep(1000);
const logs = await message.guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete, limit: 5 });
const auditentry = logs.entries.find(a =>
a.target.id === message.author.id &&
a.extra.channel.id === message.channel.id &&
Date.now() - a.createdTimestamp < 20000
);

// here is a check for certain channels and roles which I do not want to log

try {
let helpEmbed = new EmbedBuilder();
//here is the message created and sent to the channel
}catch(error) {
console.debug( error );
}
}
});
Also I was trying to get the handler messageDeletedBulk to work which did not show any data. Any idea on how I can get these messages? node Version: v16..20.1 npm Version: 9.8.1 discord.js Version: 14.11.0
8 replies