Help with GuildRoleDelete Event

module.exports = {
name: Events.GuildRoleDelete,
async execute(role) {
try {
// Fetch the audit logs to identify the user who deleted the role
const auditLogs = await role.guild.fetchAuditLogs({
type: AuditLogEvent.RoleDelete,
limit: 1
});
const roleDeleteLog = auditLogs.entries.first();
const { executor, target } = roleDeleteLog;

// Fetch audit logs for member kicks to identify if the deletion was caused by a bot being kicked
const kickLogs = await role.guild.fetchAuditLogs({
type: AuditLogEvent.MemberKick,
limit: 1
});
const kickLog = kickLogs.entries.first();
const kickExecutor = kickLog ? kickLog.executor : null;
const kickTarget = kickLog ? kickLog.target : null;

// Check if the role deletion was caused by kicking a bot
if (kickTarget && kickTarget.bot && kickLog.createdTimestamp >= roleDeleteLog.createdTimestamp) {
return; // Early return if the deletion was caused by kicking a bot
}
module.exports = {
name: Events.GuildRoleDelete,
async execute(role) {
try {
// Fetch the audit logs to identify the user who deleted the role
const auditLogs = await role.guild.fetchAuditLogs({
type: AuditLogEvent.RoleDelete,
limit: 1
});
const roleDeleteLog = auditLogs.entries.first();
const { executor, target } = roleDeleteLog;

// Fetch audit logs for member kicks to identify if the deletion was caused by a bot being kicked
const kickLogs = await role.guild.fetchAuditLogs({
type: AuditLogEvent.MemberKick,
limit: 1
});
const kickLog = kickLogs.entries.first();
const kickExecutor = kickLog ? kickLog.executor : null;
const kickTarget = kickLog ? kickLog.target : null;

// Check if the role deletion was caused by kicking a bot
if (kickTarget && kickTarget.bot && kickLog.createdTimestamp >= roleDeleteLog.createdTimestamp) {
return; // Early return if the deletion was caused by kicking a bot
}
Hey guys I need help with my code so what Im trying to do is 😆 when ever user kicks or bans a bot from the server GuildRoleDelete event gets triggered and Ive been trying to stop that . Ive spent last 2-3 hours trying to solve this but couldnt come up with solution.
4 Replies
d.js toolkit
d.js toolkit•6mo 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! - ✅ Marked as resolved by OP
lexy
lexyOP•6mo ago
v20.14.0 / `-- [email protected] Im basically trying not to trigger GuildRoleDelete event when kicking a bot how can I do that ?
d.js docs
d.js docs•6mo ago
:property: Role#tags @14.15.3 The tags this role has
lexy
lexyOP•6mo ago
if (role.tags && role.tags.botId) {

return;
}
if (role.tags && role.tags.botId) {

return;
}
made it thank you
Want results from more Discord servers?
Add your server