Started sharding: a lot of `unknown interactions`

I'v seen a consitant increase in unknown interaction since I started shardig (was barely getting any when it wasnt sharded). My bot works mainly through Dms, which by default are all handled by shard 0 (so there is no need to communicate between shards). I do still get unknown interaction from dms buttons tho (like 1 out of 20). I dunno what I could be possibly doing wrong since the code barely changed from before sharding to now. And the first thing I try to do asap is to defer such interaction anyway ShardingManager:
const { ShardingManager, ShardClientUtil } = require('discord.js');
const manager = new ShardingManager('/home/ubuntu/discord/index.js', {
token: token,
totalShards: Number(totalShards), // Total number of shards you want to run
shardList: [0,1,2,3,4],
respawn: true, // Automatically respawn shards if they crash
});

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

manager.on('shardCreate', (shard) => {
console.log(`Shard ${shard.id} created.`);

// Listen for messages from this shard
shard.on('message', (message) => {
console.log(`Message from shard ${shard.id}:`);
const targetShard = manager.shards.get(message.shardId);
if (targetShard) {
targetShard.send(message)
.then(() => {
console.log(`Message successfully sent to shard ${message.shardId}.`);
})
.catch((error) => {
console.error(`Failed to send message to shard ${message.shardId}:`, error);
});
} else {
console.error(`Shard ${message.shardId} not available.`);
}
});
});

manager.broadcast({ action: 'log', message: 'A global event occurred.' });
manager.spawn();
const { ShardingManager, ShardClientUtil } = require('discord.js');
const manager = new ShardingManager('/home/ubuntu/discord/index.js', {
token: token,
totalShards: Number(totalShards), // Total number of shards you want to run
shardList: [0,1,2,3,4],
respawn: true, // Automatically respawn shards if they crash
});

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

manager.on('shardCreate', (shard) => {
console.log(`Shard ${shard.id} created.`);

// Listen for messages from this shard
shard.on('message', (message) => {
console.log(`Message from shard ${shard.id}:`);
const targetShard = manager.shards.get(message.shardId);
if (targetShard) {
targetShard.send(message)
.then(() => {
console.log(`Message successfully sent to shard ${message.shardId}.`);
})
.catch((error) => {
console.error(`Failed to send message to shard ${message.shardId}:`, error);
});
} else {
console.error(`Shard ${message.shardId} not available.`);
}
});
});

manager.broadcast({ action: 'log', message: 'A global event occurred.' });
manager.spawn();
18 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!
Mulo
MuloOP2w ago
on the index.js file:
async function handleButtonInteraction(interaction) {
await interaction.deferReply()
// getting error "uknown interaction 1 out of 20 times
}


client.on('interactionCreate', async interaction => {
if (interaction.isButton()) {
if (interaction.customId.startsWith("ANS")) {
await handleButtonInteraction(interaction)
return
}
//...
})
async function handleButtonInteraction(interaction) {
await interaction.deferReply()
// getting error "uknown interaction 1 out of 20 times
}


client.on('interactionCreate', async interaction => {
if (interaction.isButton()) {
if (interaction.customId.startsWith("ANS")) {
await handleButtonInteraction(interaction)
return
}
//...
})
So it's not that im doing much before trying to defer asap. Im using OVH, If it's the provider the issue, before sharding I was still with OVH in europe w/o much interaction issues, now I moved it to Canada. east coast.. which should be even closer to Discord api the bot is in 2k servers, so there isnt much traffic (I have 4 cores and 8GB Ram)
require('dotenv').config({ path: '../.env.local' });
const token = process.env.token;
const totalShards = process.env.totalShards
require('dotenv').config({ path: '../.env.local' });
const token = process.env.token;
const totalShards = process.env.totalShards
omitted to avoid verbose boilerplate totalshard is 5 shardlist is the shards that should run on this machine, while in the future other shards will operate on separate machines hence why the totalshard is in the file that I will just copy over to external vps (im not yet doing that tho) isnt totalshards accross all machines, while shardlist is what is assigned to this specific machine? indeed that's for the future when I'll add external ones So I dont have to worry for a long time well is mainlny coz of the 2.5k limit per shard, since iv 4 cores and 8gb ram I'd imagine 5 shards is not that demanding? As in, shard 0 handles dms, data should directly go to such shard w/o bouncing around, unless you saying this is what actually happen?
Mulo
MuloOP2w ago
it's not that busy
No description
Mulo
MuloOP2w ago
well im consistently getting them every 20ish interactions, but never on my end when testing. I tried lately
async function handleButtonInteraction(interaction) {
let unknownInteraction = false
try {
await interaction.deferReply()
} catch (error) {
unknownInteraction = true
}

if (unknownInteraction) {
interaction.channel.send({ content: "text"})
} else {
interaction.editReply({ content: "text" })
}
}
async function handleButtonInteraction(interaction) {
let unknownInteraction = false
try {
await interaction.deferReply()
} catch (error) {
unknownInteraction = true
}

if (unknownInteraction) {
interaction.channel.send({ content: "text"})
} else {
interaction.editReply({ content: "text" })
}
}
but now im getting
/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:727
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50035]: Invalid Form Body
message_reference[REPLIES_CANNOT_REPLY_TO_SYSTEM_MESSAGE]: Cannot reply to a system message
at handleErrors (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:727:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:1128:23)
at async SequentialHandler.queueRequest (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:959:14)
at async _REST.request (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:1272:22)
at async DMChannel.send (/home/ubuntu/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:182:15) {
requestBody: {
files: [],
json: {
content: 'Answer must contain characters',
tts: false,
nonce: undefined,
...
/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:727
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50035]: Invalid Form Body
message_reference[REPLIES_CANNOT_REPLY_TO_SYSTEM_MESSAGE]: Cannot reply to a system message
at handleErrors (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:727:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:1128:23)
at async SequentialHandler.queueRequest (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:959:14)
at async _REST.request (/home/ubuntu/node_modules/@discordjs/rest/dist/index.js:1272:22)
at async DMChannel.send (/home/ubuntu/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:182:15) {
requestBody: {
files: [],
json: {
content: 'Answer must contain characters',
tts: false,
nonce: undefined,
...
which is actually an error that reboots the shard - even tho everything is in a try catch
Because why are you running so many instances of your ShardingManager?
Sure I could change it, but shouldnt 4 cores and 8bg be more than enough to handle 5 shards? Even more so now that 2k servers are spread among 5 shards
No, it isn’t. But you‘re limiting your shard 0 to less resources when you force the ShardingManager to spawn more shards
Sure, I mean when/if I'll have 200.000 servers, this main server will probably be just for shard 0 and worker threads I'd imagine, but im far from this scenario currently Are you implying that, with this current server specs, having 5 shards is the cause of the unknown interaction? So it's not that common to get unknown interactions when switching to sharding? In that case it could also me being unlucky on having been assigned by ovh a server that is actually busy lol, since it's not dedicated. Unfortunately I never get errors when it's me checking (of course) So hard to tell
ps aux | grep node
ubuntu 3964007 0.3 0.8 21976812 69364 ? Ssl 15:50 0:02 node /home/ubuntu/discord/shardingManager.js
ubuntu 3964017 1.2 2.2 22379496 178488 ? Sl 15:50 0:08 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964048 0.9 1.8 22364612 149760 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964068 1.0 2.0 22369348 160676 ? Sl 15:50 0:07 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964089 0.9 1.8 22361976 150136 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964110 0.9 2.1 22366124 167836 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ps aux | grep node
ubuntu 3964007 0.3 0.8 21976812 69364 ? Ssl 15:50 0:02 node /home/ubuntu/discord/shardingManager.js
ubuntu 3964017 1.2 2.2 22379496 178488 ? Sl 15:50 0:08 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964048 0.9 1.8 22364612 149760 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964068 1.0 2.0 22369348 160676 ? Sl 15:50 0:07 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964089 0.9 1.8 22361976 150136 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
ubuntu 3964110 0.9 2.1 22366124 167836 ? Sl 15:50 0:06 /home/ubuntu/.nvm/versions/node/v20.10.0/bin/node /home/ubuntu/discord/index.js
looks good here
manager.on('shardCreate', (shard) => {
console.log(`Shard ${shard.id} created.`);
manager.on('shardCreate', (shard) => {
console.log(`Shard ${shard.id} created.`);
client.on('ready', async () => {
console.log("REBOOT: ", `Shard #${client.shard.ids} is ready! <<<<`);
client.on('ready', async () => {
console.log("REBOOT: ", `Shard #${client.shard.ids} is ready! <<<<`);
Shard 0 created.
REBOOT: Shard #0 is ready! <<<<
Shard 1 created.
REBOOT: Shard #1 is ready! <<<<
Shard 2 created.
REBOOT: Shard #2 is ready! <<<<
Shard 3 created.
REBOOT: Shard #3 is ready! <<<<
Shard 4 created.
REBOOT: Shard #4 is ready! <<<<
Shard 0 created.
REBOOT: Shard #0 is ready! <<<<
Shard 1 created.
REBOOT: Shard #1 is ready! <<<<
Shard 2 created.
REBOOT: Shard #2 is ready! <<<<
Shard 3 created.
REBOOT: Shard #3 is ready! <<<<
Shard 4 created.
REBOOT: Shard #4 is ready! <<<<
Mulo
MuloOP2w ago
No description
Mulo
MuloOP2w ago
sweepers: {
guildMembers: {
interval: 3_600,
lifetime: 24 * 60 * 60,
filter: () => user => user.id === user.client.user.id,
},
threads: {
interval: 86_600,
lifetime: 7 * 24 * 60 * 60,
},
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
needed
GuildMemberManager: {
maxSize: 10,
keepOverLimit: (member) => member.id === client.user.id,
},
ThreadManager: 30,
}),
sweepers: {
guildMembers: {
interval: 3_600,
lifetime: 24 * 60 * 60,
filter: () => user => user.id === user.client.user.id,
},
threads: {
interval: 86_600,
lifetime: 7 * 24 * 60 * 60,
},
},
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
needed
GuildMemberManager: {
maxSize: 10,
keepOverLimit: (member) => member.id === client.user.id,
},
ThreadManager: 30,
}),
could these sweepers be the cause? ya was worried of some under the hood things that may be causing these unknown interactions.. btw interaction.createdTimestamp is the same time which discord expects a reply within the 3 secs span?
async function handleButton(interaction) {
console.log(interaction.id, "INT ID")
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(msElapsed, "DELAY")
let unknownInteraction = false
try {
await interaction.deferReply()
} catch (error) {
unknownInteraction = true
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
async function handleButton(interaction) {
console.log(interaction.id, "INT ID")
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(msElapsed, "DELAY")
let unknownInteraction = false
try {
await interaction.deferReply()
} catch (error) {
unknownInteraction = true
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
76 DELAY
[ 0 ] CANT DEFER BTN ANS 76 DELAY
76 DELAY
[ 0 ] CANT DEFER BTN ANS 76 DELAY
im also not seeing interactions ids duplicates now that i reduced it to 2 shards im may be seeing less frequent errors for a 4 vcores 8bg ram what is the suggested amount of shards such machine can host? well im really lost my bot is not that busy atm dunno if setting the process to higher priority would fix this weird unknown interaction
ConnectTimeoutError: Connect Timeout Error
at onConnectTimeout (/home/ubuntu/node_modules/undici/lib/core/connect.js:190:24)
at /home/ubuntu/node_modules/undici/lib/core/connect.js:133:46
at Immediate._onImmediate (/home/ubuntu/node_modules/undici/lib/core/connect.js:174:9)
at process.processImmediate (node:internal/timers:478:21) {
code: 'UND_ERR_CONNECT_TIMEOUT'
}
ConnectTimeoutError: Connect Timeout Error
at onConnectTimeout (/home/ubuntu/node_modules/undici/lib/core/connect.js:190:24)
at /home/ubuntu/node_modules/undici/lib/core/connect.js:133:46
at Immediate._onImmediate (/home/ubuntu/node_modules/undici/lib/core/connect.js:174:9)
at process.processImmediate (node:internal/timers:478:21) {
code: 'UND_ERR_CONNECT_TIMEOUT'
}
well this is a new error I never seen before
manager.spawn({ delay: 5000 });
manager.spawn({ delay: 5000 });
should I add a delay when spawning shards? Does discord prefer it?
d.js docs
d.js docs2w ago
tag suggestion for @Mulo: Errors such as ECONNRESET, ConnectTimeoutError, SocketError: other side closed indicate a network issue * Free or shared hosting providers often come with significant limitations * Another indication of these issues are 'Unknown Interaction' errors.
Mulo
MuloOP2w ago
I would understand that, but how can it be that it gives unknown interaction when i log the now -.createdTime and is always way less then 3000. If it was a network issue wouldnt that be > 3000?
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(msElapsed, "DELAY")
try {
await interaction.deferReply()
} catch (error) {
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(msElapsed, "DELAY")
try {
await interaction.deferReply()
} catch (error) {
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
36 DELAY
[ 0 ] CANT DEFER BTN ANS 36 DELAY
36 DELAY
[ 0 ] CANT DEFER BTN ANS 36 DELAY
Amgelo
Amgelo2w ago
well you receive interactions via ws, but you reply to them via rest so technically they can be entirely unrelated, and there can be issues with one but not the other
Mulo
MuloOP2w ago
im usinv ovh canada would changing dns to cloudflare be of any help?
Amgelo
Amgelo2w ago
I'd say try checking this first
Mulo
MuloOP2w ago
that is indeed the case
try {
await interaction.deferReply()
} catch (error) {
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
try {
await interaction.deferReply()
} catch (error) {
const msElapsed = Date.now() - interaction.createdTimestamp;
console.log(client.shard.ids, 'CANT DEFER BTN ANS ', msElapsed, "DELAY")
}
[ 0 ] CANT DEFER BTN ANS 5156 DELAY
[ 0 ] CANT DEFER BTN ANS 5108 DELAY
[ 0 ] CANT DEFER BTN ANS 5156 DELAY
[ 0 ] CANT DEFER BTN ANS 5108 DELAY
Amgelo
Amgelo2w ago
yeah your defer is taking way too long to reach
Mulo
MuloOP2w ago
tho im getting it once every 30 other interactions that do work well so im not sure what could it be processing applications through dms, but im getting this apparent delay not only from dms butons but from any guild as well
client.rest.on('rateLimited', (info) => {
console.log(info)
})
client.rest.on('rateLimited', (info) => {
console.log(info)
})
? will try to see tools that checks I/O but for sure htop shows that all is quiet very quiet only thing I can say is that when I had this normal bot on a 1vcore 2gb ram vps in germany I was barely getting any unknown interactions, now in canada (which should be closer to discord api) with 4c and 8gb I ran into these probelms 😄 running some debugging tools and all seems to be normal Tho im wondering, if it was some sync ops clogging up the system, shouldnt I get delays when first receiving the interaction as well? - and not only when trying to defer them? right but if the cause of this were whatever internal long running sync op, I would be seeing issues on both send/ receive. Since this is not the case, im out of options beside external server provider issues
const fetch = require('node-fetch');

async function manuallyDefer(interaction) {
const url = `https://discord.com/api/v10/interactions/${interaction.id}/${interaction.token}/callback`;

const body = {
type: 5 // DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
};

const start = Date.now();
console.log(`[${interaction.id}] Sending raw deferReply at ${start}`);

try {
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bot ${process.env.BOT_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});

const end = Date.now();
console.log(`[${interaction.id}] Response from deferReply in ${end - start}ms`);

if (!res.ok) {
const errText = await res.text();
console.error(`[${interaction.id}] Defer failed: ${res.status} - ${errText}`);
}
} catch (error) {
console.error(`[${interaction.id}] Defer error:`, error);
}
}
const fetch = require('node-fetch');

async function manuallyDefer(interaction) {
const url = `https://discord.com/api/v10/interactions/${interaction.id}/${interaction.token}/callback`;

const body = {
type: 5 // DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
};

const start = Date.now();
console.log(`[${interaction.id}] Sending raw deferReply at ${start}`);

try {
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bot ${process.env.BOT_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});

const end = Date.now();
console.log(`[${interaction.id}] Response from deferReply in ${end - start}ms`);

if (!res.ok) {
const errText = await res.text();
console.error(`[${interaction.id}] Defer failed: ${res.status} - ${errText}`);
}
} catch (error) {
console.error(`[${interaction.id}] Defer error:`, error);
}
}
Should I try this to manually debug the issue
Mulo
MuloOP2w ago
No description
No description
No description
Mulo
MuloOP2w ago
im noticing im getting unknown interaction on another bot im running as well, which is not sharded im using [email protected] btw the other bot file? no, same machine i have 1Gbps, as the graph supposedly say im way below it just 2 (one sharded) the other normal (it's in 300 servers) and 2 websites
try {
await interaction.deferReply()
} catch (error) {
const memoryUsage = process.memoryUsage();
console.log(`Heap Used: ${(memoryUsage.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(`Heap Total: ${(memoryUsage.heapTotal / 1024 / 1024).toFixed(2)} MB`);
try {
await interaction.deferReply()
} catch (error) {
const memoryUsage = process.memoryUsage();
console.log(`Heap Used: ${(memoryUsage.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(`Heap Total: ${(memoryUsage.heapTotal / 1024 / 1024).toFixed(2)} MB`);
Heap Used: 188.32 MB
Heap Total: 193.33 MB
Heap Used: 188.32 MB
Heap Total: 193.33 MB
is this a concen? also:
setInterval(() => {
const currentTime = process.hrtime(lastTime);
const delay = currentTime[0] * 1e3 + currentTime[1] / 1e6;

if (delay > 1050) { // Log if the delay exceeds 100 ms (adjust threshold as needed)
console.log('Event Loop Delay:', delay, 'ms');
}

lastTime = process.hrtime();
}, 1000);
setInterval(() => {
const currentTime = process.hrtime(lastTime);
const delay = currentTime[0] * 1e3 + currentTime[1] / 1e6;

if (delay > 1050) { // Log if the delay exceeds 100 ms (adjust threshold as needed)
console.log('Event Loop Delay:', delay, 'ms');
}

lastTime = process.hrtime();
}, 1000);
im barely getting a log, if not only on reboot
Mulo
MuloOP2w ago
No description
Mulo
MuloOP7d ago
const blocked = require('blocked-at');
blocked((time, stack) => {
console.log(`Blocked for ${time}ms, operation started here:`, stack);
}, { threshold: 50 }); // Logs if event loop is blocked > 50ms
const blocked = require('blocked-at');
blocked((time, stack) => {
console.log(`Blocked for ${time}ms, operation started here:`, stack);
}, { threshold: 50 }); // Logs if event loop is blocked > 50ms
also rarely getting this, and not when getting unknwon interaction can promises be realistically the problem? as in any async process or to have these issues, it should rather be sync ops issue that's assuring 😄 i was with ovh in eu w/o much problems, now stil ovh in canada btw sidenote, i tried pinging discord with traceroute and it seems the connection is not direct as one might think as in there actually wouldnt be much difference between having a bot in north america and europe well it seems like doing the oldest trick in the book - aka rebooting - might have alleviate the issue. Tho I guess is common to have unknown interaction here and there for everybody (hopefully). But when installing couple of debugging tools they erased the node modules folder and messed up something that next.js is not able to build websites, how nice.

Did you find this page helpful?