Ban/Kick event reason

I'm doing a bot in Discord.JS and i'm in the process of adding the welcome/leave/ban messages. Is possible to fetch the reason (specially when a moderator bans using the GUI) of why the member was kicked/banned?
8 Replies
d.js toolkit
d.js toolkit4mo ago
beetle
beetle4mo ago
yes through audit logs
Vico
VicoOP4mo ago
can you give me an example?
Maxi130
Maxi1304mo ago
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
Maxi130
Maxi1304mo ago
const { AuditLogEvent, Events } = require('discord.js');

client.on(Events.GuildAuditLogEntryCreate, async auditLog => {
// Define your variables.
const { action, executorId, targetId, reason } = auditLog;

// Check only for kicked users.
if (action !== AuditLogEvent.MemberKick) return;

// Ensure the executor is cached.
const executor = await client.users.fetch(executorId);

// Ensure the kicked guild member is cached.
const kickedUser = await client.users.fetch(targetId);

// Now you can log the output!
console.log(`${kickedUser.tag} was kicked by ${executor.tag} for ${reason}`);
});
const { AuditLogEvent, Events } = require('discord.js');

client.on(Events.GuildAuditLogEntryCreate, async auditLog => {
// Define your variables.
const { action, executorId, targetId, reason } = auditLog;

// Check only for kicked users.
if (action !== AuditLogEvent.MemberKick) return;

// Ensure the executor is cached.
const executor = await client.users.fetch(executorId);

// Ensure the kicked guild member is cached.
const kickedUser = await client.users.fetch(targetId);

// Now you can log the output!
console.log(`${kickedUser.tag} was kicked by ${executor.tag} for ${reason}`);
});
Vico
VicoOP4mo ago
thank you so much
Maxi130
Maxi1304mo ago
Could you mark the thread as solved?
d.js toolkit
d.js toolkit4mo ago
The thread owner has marked this issue as solved.

Did you find this page helpful?