Shard Broadcast

main.js
let imgchannel, vidchannel, shardId;

bot.once(Events.ShardReady, async () => {
imgchannel = bot.channels.cache.get(process.env.IMAGE_CHANNEL);
vidchannel = bot.channels.cache.get(process.env.VIDEO_CHANNEL);
module.exports.imgchannel = imgchannel;
module.exports.vidchannel = vidchannel;
shardId = bot.shard.ids[0];

bot.user.setActivity({
name: `/help | Shard ${bot.shard.ids[0]}`,
type: ActivityType.Watching,
shardId: bot.shard.ids[0],
});
if (shardId === 1) {
console.log("Started Source ON shard 1");
client.login(process.env.SOURCE_TOKEN);
}
});
let imgchannel, vidchannel, shardId;

bot.once(Events.ShardReady, async () => {
imgchannel = bot.channels.cache.get(process.env.IMAGE_CHANNEL);
vidchannel = bot.channels.cache.get(process.env.VIDEO_CHANNEL);
module.exports.imgchannel = imgchannel;
module.exports.vidchannel = vidchannel;
shardId = bot.shard.ids[0];

bot.user.setActivity({
name: `/help | Shard ${bot.shard.ids[0]}`,
type: ActivityType.Watching,
shardId: bot.shard.ids[0],
});
if (shardId === 1) {
console.log("Started Source ON shard 1");
client.login(process.env.SOURCE_TOKEN);
}
});
run.js
require("dotenv").config();
const { ShardingManager } = require("discord.js");

const manager = new ShardingManager("./xmain.js", {
token: process.env.BOT_TOKEN,
});

manager.on("shardCreate", (shard) => {
console.log(`Launched shard ${shard.id}`);
});

manager.spawn();
require("dotenv").config();
const { ShardingManager } = require("discord.js");

const manager = new ShardingManager("./xmain.js", {
token: process.env.BOT_TOKEN,
});

manager.on("shardCreate", (shard) => {
console.log(`Launched shard ${shard.id}`);
});

manager.spawn();
Use case of imported channels setup.js
const { vidchannel } = require("../xmain");
await vidchannel
.addFollower(channel, "Video memes feed")
.then(
async () =>
await interaction.reply({ embeds: [setup], ephemeral: false })
)
const { vidchannel } = require("../xmain");
await vidchannel
.addFollower(channel, "Video memes feed")
.then(
async () =>
await interaction.reply({ embeds: [setup], ephemeral: false })
)
Well as you know the server is getting cached on shard 1 idk why but it seems to always be the case. I fetch (cache.get) channels from that server for my bot to function but any other shard that doesn't share the same shard as the server with these channels can't fetch these channels on that other shard. I tried to use broadcast eval and other ways like saving channels in a seperate function in another file and initializing it from shard 1 but it always returned undefined on the other shards. I'm not sure either if im using broadcast eval correctly. I want to fix that but first i also want to know how is the main server we get the channels from allocated to shards? Can i get this server on which shard too and can i start shards before the others? I really need to fix this asap.
6 Replies
d.js toolkit
d.js toolkit2w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Lawa
LawaOP2w ago
i provided code that handles only 1 shard being online. If you want me to provide my failed attempts i could try as well. But either way i need to find a solution.
Mark
Mark2w ago
Pretty sure that exporting from within an event listener isn't gonna work correctly. That being said, if you need to find a guild/channel on a shard to do something with like adding a follower, you need to use bEval and do the API request inside that function
Lawa
LawaOP2w ago
i'll try that i still want to know based on what servers get allocated to which shards and if we can control this
d.js docs
d.js docs2w ago
:method: (static) ShardClientUtil#shardIdForGuildId() [email protected] Get the shard id for a given guild id.
Lawa
LawaOP2w ago
yeah im not going anywhere. i'm so confused. The slashcommand sends a request to it's specific shard so what i need this shard to request info from the main shard? I'm getting no where 💔 okay I've got it thank you @Mark for your input i would have not figured it out. It's finally working.

Did you find this page helpful?