Duplicate messages in multiple shards

ws.on('message', async function incoming(data) {
const object = JSON.parse(data);

let channel = await client.channels.fetch(object.message.target.id);
if (channel) {
console.log('Discord socket message:', shardid)
await channel.send(object.message.text)
}
return;

});
ws.on('message', async function incoming(data) {
const object = JSON.parse(data);

let channel = await client.channels.fetch(object.message.target.id);
if (channel) {
console.log('Discord socket message:', shardid)
await channel.send(object.message.text)
}
return;

});
2023-08-23 16:31: Discord socket message: 4
2023-08-23 16:31: Discord socket message: 8
2023-08-23 16:31: Discord socket message: 4
2023-08-23 16:31: Discord socket message: 8
Hello, I have a bot with multiple shards. I attempted to send a Discord message when the bot receives a WebSocket message. However, I'm currently facing an issue. If I use the code mentioned above, sometimes multiple shards will send the same message simultaneously. For instance, shard 4 and shard 8 both have access to the channel, resulting in duplicate messages. Is this behavior normal? How can I ensure that only one shard sends the message?
1 Reply
d.js toolkit
d.js toolkit16mo 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!
Want results from more Discord servers?
Add your server