Search code examples
node.jsdiscorddiscord.js

client.user.setActivity found as null? (Node.js)


I am trying to set the activity of my Discord bot as a "game" however the examples I've found online haven't helped at all.

client.user.setActivity("what the bot is playing");

Does not work at all, it gives me this error stating that I am trying to find something that is null (non-existent... in non programming terms)

TypeError: Cannot read properties of null (reading 'setActivity')
at Object.<anonymous> (/home/runner/DiscordBot/index.js:46:13)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

Solution

  • You're probably trying to change the bot's status before it's online. Try putting it inside of the ready event instead.

    client.on("ready", () => {
        client.user.setActivity("what the bot is playing");
    });