Search code examples
javascriptnode.jsdiscorddiscord.jschatbot

how to code a custom rich presence i tried doing one but gives error


const client = require("../index");

client.on("ready", () =>
    console.log(`${client.user.tag} is up and ready to go!`)
);
client.user.setPresence({
    status: "online",  // You can show online, idle... Do not disturb is dnd
    game: {
        name: "p!help • poketwo.net",  // The message shown
        type: "PLAYING" // PLAYING, WATCHING, LISTENING, STREAMING,
    }
});

that is the code for my rich presence but but i get a error as below

F:\akash\code\p22222\events\ready.js:6
client.user.setPresence({
            ^

TypeError: Cannot read properties of null (reading 'setPresence')
    at Object.<anonymous> (F:\akash\code\p22222\events\ready.js:6: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 Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at F:\akash\code\p22222\handler\index.js:27:31
    at Array.map (<anonymous>)
    at module.exports (F:\akash\code\p22222\handler\index.js:27:16)

i dont know how to fix pls let me know what to do


Solution

  • Since I do not know where does the client come from, I can't give you a proper solution. I'll show you how I made mine work, though.

    Client code:

    const client = new Discord.Client({
      intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS", "DIRECT_MESSAGES", "GUILD_BANS", "GUILD_MEMBERS", "GUILD_VOICE_STATES"], //must use intents
      allowedMentions: ["users"] //stops people from using +say @everyone
    });
    

    On code:

    //state: on
    client.on("ready", () => {
      console.log(`Logged in as ${client.user.tag}!`);
      console.log("Connected to server(s):");
      client.guilds.cache.forEach((guild) => {
        console.log(" - " + guild.name + "\n    ID: " + guild.id +"\n");
        guild_number += 1;
      });
          console.log(`The number of guilds connected: ${guild_number}`);
          client.user.setActivity('World of Tanks', {type: "PLAYING"});
          client.user.setStatus('dnd');
        });
    

    You should put all the client initalizers (defining, on, etc.) in your index.js.