Logic1
Logic1

Reputation: 1847

Why does my Discord.js bot take more than five minutes to respond on a server with less than 500 users?

I've just developed a bot for adding roles to a user who starts streaming on my server. When I tested the bot on a isolated server with only a few channels and users the bot responded instantly. But now I have set up my bot on my primary Discord server with roughly 25 channels and about ~500 users and now the response time is more than five minutes before I see it add or remove a role.

I am connecting via FIOS gigabit and running on a I9 with DDR4 4266MHz RAM.

When I first start the node server I notice the delay is much less, however still very slow responses of around a minute or two before it adds or removes the roles. If I allow the server to run for ten minutes then the response time is around five minutes before it adds a role. I do not know if it continues to get worse over time. But what could be causing this delay? I see my user status change to streaming (purple dot) in the online users list immediately, but my discord bot is not aware of this for a very long time!

This is my code, I have tried my best to optimize it but I've only been learning Discord.js for about a week and am still very unfamiliar with it:

// require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const client = new Discord.Client();

let role = '';

client.once('ready', () => {
    const myGuild = client.guilds.cache.get('123456789123456789');
    role = myGuild.roles.cache.find(role => role.name === 'streaming');

    console.log('Ready!');
});

client.on("presenceUpdate", (oldPresence, newPresence) => {
    
    if (!newPresence.activities){ return false; }
    
    newPresence.activities.forEach(activity => {
        if (activity.type == "STREAMING") {
            //console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
            newPresence.member.roles.add(role)
        } else {
            newPresence.member.roles.remove(role)
        };
    });
    
});

// login to Discord with your app's token
client.login('123456789123456789.123456789.123456789123456789');

Can someone please explain why my bot is becoming so sluggish? It is almost unusable and kind of discouraging.

Upvotes: 1

Views: 1205

Answers (1)

Manny
Manny

Reputation: 389

The default maxListeners is set to 10 and it may be worth exploring setting that value higher.

Additionally, I am curious if there is a memory leak on your application. It sounds like it from the sluggishness that is experienced over time.

Try using a profiling tool like Clinic.js to monitor the state of the application over time.

Upvotes: 2

Related Questions