flashy
flashy

Reputation: 564

Discord.JS: Listening for kicks

I am building a server protection feature. I want to listen for kicks on guilds. I know listening for ban but I could not find the event for kick. Does kick have an event? If not, is there a way to listen for kicks?

Upvotes: 0

Views: 3042

Answers (2)

user13429955
user13429955

Reputation:

Your comment on renato's was correct, a quick fix for this would be to check the times for the audit log and the join:

if (kickLog.createdAt < member.joinedAt) { 
    return console.log(`${member.user.tag} left the guild, most likely of their own will.`);
}

Full code:

client.on('guildMemberRemove', async member => {
    const fetchedLogs = await member.guild.fetchAuditLogs({
        limit: 1,
        type: 'MEMBER_KICK',
    });
    // Since we only have 1 audit log entry in this collection, we can simply grab the first one
    const kickLog = fetchedLogs.entries.first();

    // Let's perform a sanity check here and make sure we got *something*
    if (!kickLog) return console.log(`${member.user.tag} left the guild, most likely of their own will.`);

    // We now grab the user object of the person who kicked our member
    // Let us also grab the target of this action to double check things
    const { executor, target } = kickLog;

    if (kickLog.createdAt < member.joinedAt) { 
        return console.log(`${member.user.tag} left the guild, most likely of their own will.`);
    }

    // And now we can update our output with a bit more information
    // We will also run a check to make sure the log we got was for the same kicked member
    if (target.id === member.id) {
        console.log(`${member.user.tag} left the guild; kicked by ${executor.tag}?`);
    } else {
        console.log(`${member.user.tag} left the guild, audit log fetch was inconclusive.`);
    }
});

Upvotes: 4

Renato
Renato

Reputation: 29

You can find help about kicks event in discord.js official wiki. Link : Link

The event is: guildMemberRemove

This is an example taken from https://discordjs.guide (you can find this clicking the Link) :

client.on('guildMemberRemove', async member => {
    const fetchedLogs = await member.guild.fetchAuditLogs({
        limit: 1,
        type: 'MEMBER_KICK',
    });
    // Since we only have 1 audit log entry in this collection, we can simply grab the first one
    const kickLog = fetchedLogs.entries.first();

    // Let's perform a sanity check here and make sure we got *something*
    if (!kickLog) return console.log(`${member.user.tag} left the guild, most likely of their own will.`);

    // We now grab the user object of the person who kicked our member
    // Let us also grab the target of this action to double check things
    const { executor, target } = kickLog;

    // And now we can update our output with a bit more information
    // We will also run a check to make sure the log we got was for the same kicked member
    if (target.id === member.id) {
        console.log(`${member.user.tag} left the guild; kicked by ${executor.tag}?`);
    } else {
        console.log(`${member.user.tag} left the guild, audit log fetch was inconclusive.`);
    }
});

This isn't my code. I copy-pasted this code from discordjs.guide. Do you need a list of events?

Here you can find the list. (Source: https://discord.js.org/ and https://discordjs.guide)

Upvotes: 1

Related Questions