Update presence @discordjs/core

Hey how can i update the presence? I have no shardID. https://discord.js.org/docs/packages/core/main/Client:Class#updatePresence
core | Client
Discord.js API Documentation
5 Replies
d.js toolkit
d.js toolkit14mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Nlmyr
Nlmyr14mo ago
const { REST } = require("@discordjs/rest");
const { WebSocketManager } = require("@discordjs/ws");
const {
GatewayDispatchEvents,
GatewayIntentBits,
InteractionType,
MessageFlags,
Client,
} = require("@discordjs/core");
require("dotenv").config();

// Create REST and WebSocket managers directly
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

const gateway = new WebSocketManager({
token: process.env.TOKEN,
intents: GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent,
rest,
});

// Create a client to emit relevant events.
const client = new Client({ rest, gateway });

// Listen for interactions
// Each event contains an `api` prop along with the event data that allows you to interface with the Discord REST API
client.on(
GatewayDispatchEvents.InteractionCreate,
async ({ data: interaction, api }) => {
if (
interaction.type !== InteractionType.ApplicationCommand ||
interaction.data.name !== "ping"
) {
return;
}

await api.interactions.reply(interaction.id, interaction.token, {
content: "Pong!",
flags: MessageFlags.Ephemeral,
});
}
);

// Listen for the ready event
client.once(GatewayDispatchEvents.Ready, () =>(
console.log(`Ready! Bot started.`);
)
);

// Start the WebSocket connection.
gateway.connect();
const { REST } = require("@discordjs/rest");
const { WebSocketManager } = require("@discordjs/ws");
const {
GatewayDispatchEvents,
GatewayIntentBits,
InteractionType,
MessageFlags,
Client,
} = require("@discordjs/core");
require("dotenv").config();

// Create REST and WebSocket managers directly
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

const gateway = new WebSocketManager({
token: process.env.TOKEN,
intents: GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent,
rest,
});

// Create a client to emit relevant events.
const client = new Client({ rest, gateway });

// Listen for interactions
// Each event contains an `api` prop along with the event data that allows you to interface with the Discord REST API
client.on(
GatewayDispatchEvents.InteractionCreate,
async ({ data: interaction, api }) => {
if (
interaction.type !== InteractionType.ApplicationCommand ||
interaction.data.name !== "ping"
) {
return;
}

await api.interactions.reply(interaction.id, interaction.token, {
content: "Pong!",
flags: MessageFlags.Ephemeral,
});
}
);

// Listen for the ready event
client.once(GatewayDispatchEvents.Ready, () =>(
console.log(`Ready! Bot started.`);
)
);

// Start the WebSocket connection.
gateway.connect();
i used this from the doc example Yes I look only in the new things there 😅 I get the following error:
throw new RangeError(`Shard ${shardId} not found`);
^

RangeError: Shard 0 not found
throw new RangeError(`Shard ${shardId} not found`);
^

RangeError: Shard 0 not found
RangeError: Shard 0 not found
PS C:\Users\xxx> node .
C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1135
throw new RangeError(`Shard ${shardId} not found`);
^

at SimpleShardingStrategy.send (C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1135:13)
at WebSocketManager.send (C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1374:26)
at Client.updatePresence (C:\Users\xxx\node_modules\@discordjs\core\dist\index.js:2391:24)
at Object.<anonymous> (C:\Users\xxx\src\main.js:48:10)
at Module._compile (node:internal/modules/cjs/loader:1267:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1321:10)
at Module.load (node:internal/modules/cjs/loader:1125:32)
at Module._load (node:internal/modules/cjs/loader:965:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47

Node.js v20.1.
RangeError: Shard 0 not found
PS C:\Users\xxx> node .
C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1135
throw new RangeError(`Shard ${shardId} not found`);
^

at SimpleShardingStrategy.send (C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1135:13)
at WebSocketManager.send (C:\Users\xxx\node_modules\@discordjs\ws\dist\index.js:1374:26)
at Client.updatePresence (C:\Users\xxx\node_modules\@discordjs\core\dist\index.js:2391:24)
at Object.<anonymous> (C:\Users\xxx\src\main.js:48:10)
at Module._compile (node:internal/modules/cjs/loader:1267:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1321:10)
at Module.load (node:internal/modules/cjs/loader:1125:32)
at Module._load (node:internal/modules/cjs/loader:965:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47

Node.js v20.1.
client.once(
GatewayDispatchEvents.Ready,
async () => console.log(`Ready! Bot started.`),
client.updatePresence(0, {
status: PresenceUpdateStatus.Online,
activities: [
{
name: "with discord.js",
type: 0,
},
],
afk: false,
})
);
client.once(
GatewayDispatchEvents.Ready,
async () => console.log(`Ready! Bot started.`),
client.updatePresence(0, {
status: PresenceUpdateStatus.Online,
activities: [
{
name: "with discord.js",
type: 0,
},
],
afk: false,
})
);
thats my code
d4
d414mo ago
you can also use the shardId passed to the event listener props
client.once(GatewayDispatchEvents.Ready, ({ shardId }) => { ... });
client.once(GatewayDispatchEvents.Ready, ({ shardId }) => { ... });
moreover you are passing the updatePresence function as a parameter, not as part of the listener function, which probably results in it being executed before the bot logs in
Nlmyr
Nlmyr14mo ago
@d4isdavid I am a Little confused about the new docs. Do you have some advice to read the docs right?
d4
d414mo ago
not really, for things like this that are not explicitly written in the docs i go off the examples and typings