Jonah
Jonah
DIAdiscord.js - Imagine an app
Created by Jonah on 2/6/2025 in #djs-questions
Issue with @discordjs/ws sharding
I am trying to use worker threads for sharding just like shown in the docs just in common js instead ts https://discordjs.dev/docs/packages/ws/main This is the code:
require("dotenv").config();

const { WebSocketManager, WebSocketShardEvents, WorkerShardingStrategy } = require('@discordjs/ws');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');

const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
// This example will spawn Discord's recommended shard count, all under the current process.
const manager = new WebSocketManager({
token: process.env.TOKEN,
intents: 0, // for no intents
fetchGatewayInformation() {
return rest.get(Routes.gatewayBot());
},
buildStrategy: (manager) =>
new WorkerShardingStrategy(manager, {
shardsPerWorker: 2,
workerPath: './worker.js',
// Optionally, if you have custom messaging, like for analytic collection, you can use this:
async unknownPayloadHandler(data) {
// handle data here :3
},
}),
// uncomment if you have zlib-sync installed and want to use compression
// compression: CompressionMethod.ZlibSync,

// alternatively, we support compression using node's native `node:zlib` module:
// compression: CompressionMethod.ZlibNative,
});

manager.on(WebSocketShardEvents.Dispatch, (event) => {
// Process gateway events here.
});

manager.connect().catch();
require("dotenv").config();

const { WebSocketManager, WebSocketShardEvents, WorkerShardingStrategy } = require('@discordjs/ws');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');

const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
// This example will spawn Discord's recommended shard count, all under the current process.
const manager = new WebSocketManager({
token: process.env.TOKEN,
intents: 0, // for no intents
fetchGatewayInformation() {
return rest.get(Routes.gatewayBot());
},
buildStrategy: (manager) =>
new WorkerShardingStrategy(manager, {
shardsPerWorker: 2,
workerPath: './worker.js',
// Optionally, if you have custom messaging, like for analytic collection, you can use this:
async unknownPayloadHandler(data) {
// handle data here :3
},
}),
// uncomment if you have zlib-sync installed and want to use compression
// compression: CompressionMethod.ZlibSync,

// alternatively, we support compression using node's native `node:zlib` module:
// compression: CompressionMethod.ZlibNative,
});

manager.on(WebSocketShardEvents.Dispatch, (event) => {
// Process gateway events here.
});

manager.connect().catch();
Why do I get this error?
const data = await this.options.rest.get(import_v103.Routes.gatewayBot());
^

TypeError: Cannot read properties of undefined (reading 'get')
at WebSocketManager.fetchGatewayInformation (C:\Users\Jonah\Github\CoreVoice\node_modules\@discordjs\ws\dist\index.js:1502:42)
at WebSocketManager.getShardIds

...
const data = await this.options.rest.get(import_v103.Routes.gatewayBot());
^

TypeError: Cannot read properties of undefined (reading 'get')
at WebSocketManager.fetchGatewayInformation (C:\Users\Jonah\Github\CoreVoice\node_modules\@discordjs\ws\dist\index.js:1502:42)
at WebSocketManager.getShardIds

...
91 replies
DIAdiscord.js - Imagine an app
Created by Jonah on 2/20/2023 in #djs-questions
Bot Disconnects From Discord
Hey Guys 🙋‍♂️ I am currently experiencing that my bot randomly disconnects from Discord (Offline and not responding) but there is no crash or error in console and still running. I just want to know if that’s somehow a known issue or what can be the reason for that :)
19 replies