Search code examples
discord.jsbots

Discord.js : detect when user gets disconnected, and by whom


I'm trying to make a discord bot who 'avenges me'. With discord.js, is it possible to detect when a user (me) gets disconnected from voice channel by another user, and know who the latter is, so that i can be avenged.

thanks


Solution

  • So, here's what I found that gets the closest to what I wanted to do ;

    async function discoAvenger() {
        // disco(nnection) avenger, not 80's IronMan dancing eh
    
        const guild = bot.guilds.cache.get(config.guildId);
           // targeted discord server
        const fetchedLogs = await guild.fetchAuditLogs(({ limit: 1 }));
           // fetch the logs of that server (returns an array)
        const auditLogEntry = fetchedLogs.entries.first();
           // select the first logs of that array
    
        if (auditLogEntry.action == 27) {
          // if the logs is a "player got disconnected from vocal" (unintentionally)
            console.log("27 it is");
            const now = new Date();
            const diff = now.getTime() - auditLogEntry.createdTimestamp;
               // get the time diff between now and the log27
            const member = guild.members.cache.get(auditLogEntry.executor.id);
               // fetch who disconnected someone
            if (diff < 5000) {
               // use diff to know if the log27 was created less than 5s ago
               // else the member would be disconnected even if the log27 was created 1h ago
                console.log("log27 > 5s")
                console.log(member.id)
                if (member.id !== config.clientId) {
                  // if the member who disconnected someone is not the bot
                    console.log("I dit not do it. -T. Lannister");
                    member.voice.setChannel(null);
                       // disconnect the member for revenge
                    console.log(`disconnected ${member.displayName}`);
    
               // here i just tried to create and delete a dummy channel, i'll explain down there
                    const channel = guild.channels.create({
                        name: "Dummy",
                        type: Discord.ChannelType.GuildVoice,
                        parent: "911348246327210039",
                    });
                    channel.delete();
                }
            }
        }
    }
    setInterval(discoAvenger, 5000);  // repeat every 5s to check if there is a new log27
    
    

    Here's the thing; (my assumption)

    Discord has it's ways of generating the server's logs. Disconnect someone, and a log is created.

    "Ding disconnected 1 user"

    Disconnect another someone between the first log creation and a certain amount of time and a second log is not created, but the first is updated

    "Ding disconnect 2 users"

    And this is where it ends, it's not possible (to the best of my knowledge) to detect that log update.

    This is why i tried to create and delete dummy channel, thus creating 2 logs after the deconnection, to see if the log27 would still be updated or a new one created, but even with 2 different newer logs, it gets updated.

    The code works, but only once every x minutes. (the time it waits before created a new log, not updating an older one).

    Hope this can help.