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
5 Replies
d.js toolkit
d.js toolkit•10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Toast
Toast•10mo ago
Are you sure that you're not making a typo in messageDeletedBulk? The actual event name is messageDeleteBulk
Obelisque
ObelisqueOP•10mo ago
The typo was in my question. I am indeed using messageDeleteBulk
/**
* Message Handler for deleted messages in bulk
*/
client.on('messageDeleteBulk', async message => {
if( message.content && message.content.length > 0 ) {
console.log( message );
}
});
/**
* Message Handler for deleted messages in bulk
*/
client.on('messageDeleteBulk', async message => {
if( message.content && message.content.length > 0 ) {
console.log( message );
}
});
Unknown User
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Obelisque
ObelisqueOP•10mo ago
Many thanks - that was the issue I had here 🙂
Want results from more Discord servers?
Add your server