Fetch user from different shard (without being in the same guild)

I'm working on a notification function to send DM user . But I read some information online that using .broadcastEval() may result in multiple messages will be send if that user is currently in cache of multiple shards. So in my current implementation, I'm using cron job running on shard manager to check when to send user a DM and picking random shard to send it like this: Code in shard manager
// Pick random shard to dm user to prevent dulicate message
const totalShard = manager.totalShards;
const rngShard = getRandomInt(totalShard);
manager.broadcast({ name: 'dmUser', id: id, msg: msg, shard: rngShard });
// Pick random shard to dm user to prevent dulicate message
const totalShard = manager.totalShards;
const rngShard = getRandomInt(totalShard);
manager.broadcast({ name: 'dmUser', id: id, msg: msg, shard: rngShard });
Code in client
process.on('message', async function(m) {
//...
if (m.name === 'dmUser') {
// Check if this shard is the one assigned to dm user
if (client.shard.ids[0] === Number(m.shard)) {
const user = await client.users.fetch(m.id);
if (!m.msg || m.msg === '') {
return;
}
await user.send(m.msg);
}
}
}
process.on('message', async function(m) {
//...
if (m.name === 'dmUser') {
// Check if this shard is the one assigned to dm user
if (client.shard.ids[0] === Number(m.shard)) {
const user = await client.users.fetch(m.id);
if (!m.msg || m.msg === '') {
return;
}
await user.send(m.msg);
}
}
}
but my bot is still in development and don't have enough user to spawn multiple shards yet, so I have no way to test if my shard can fetch and DM that user without being in the same guild as them
4 Replies
d.js toolkit
d.js toolkit13mo 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
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
HaruAjsuru
HaruAjsuruOP13mo ago
thanks for your answer, I also want to know that if I can DM user is not in the same guild with the shard (something like the guild that user currently in belong to shard 1 but I want to use shard 4 to DM that user) fixed my question to clarify my intent
monbrey
monbrey13mo ago
Yeah you dont need to DM a user from a specific shard In fact, all received DMs go to shard 0 anyway I think
Want results from more Discord servers?
Add your server