Search code examples
javascriptnode.jsnodesdiscorddiscord.js

How do I make my Discord bot change status auto


How do I make my Discord bot change status every second

This code does not work

TypeError: client.user.setActivity is not a function

const activities_list = [
"with the &help command.", 
"with the developers console",
"with some code", 
"with JavaScript"
]; // creates an arraylist containing phrases you want your bot to switch through.

client.on('ready', () => {
    setInterval(() => {
        const index = Math.floor(Math.random() * (activities_list.length - 1) + 1); // generates a random number between 1 and the length of the activities array list (in this case 5).
        client.user.setActivity(activities_list[index]); // sets bot's activities to one of the phrases in the arraylist.
    }, 10000); // Runs this every 10 seconds.
});

Solution

  • I pasted your code and it totally worked as expected, a little thing: The setInterval function waits before executing, means in your case: it waits 10 seconds and only after that it sets the status the first time, so you might want to change your code a little bit so that it already sets a status when starting and not after the first interval is over:

    const actvs = [
        "with the &help command.",
        "with the developers console",
        "with some code",
        "with JavaScript"
    ];
    
    client.on('ready', () => {
        client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
        setInterval(() => {
            client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
        }, 10000);
    });
    

    Edit: My Discord.js version is v11.5.1, it is different in v12. Here is a link to the docs for v12