setNickName limitations

Hi everyone! I have a bot that changes its nickname every time a websocket is updated (around 10/15 seconds) and I can't find the problem why it stops changing. As a test, I made this method that I call every 1 second and you can see that after change #20 it stops updating. As I checked on the Discord developers discord insert spiderman meme here the API has a limitation that "it is returned in the response headers", but I don't know how to handle it from discord.js. Can anyone help me?
client.guilds.cache.forEach((guild) => {
if (nickname != guild.members.me?.nickname) {
debug(
`Updating nickname of ${guild.members.me?.displayName} to ${nickname}`
);
guild.members.me
?.setNickname(nickname)
.then((m) => debug("Nickname updated %O", m.nickname))
.catch((e) => error(e));
}
});
client.guilds.cache.forEach((guild) => {
if (nickname != guild.members.me?.nickname) {
debug(
`Updating nickname of ${guild.members.me?.displayName} to ${nickname}`
);
guild.members.me
?.setNickname(nickname)
.then((m) => debug("Nickname updated %O", m.nickname))
.catch((e) => error(e));
}
});
Output
bot-btc:updateNick:debug Updating nickname of 0 to 1 +0ms
bot-btc:updateNick:debug Nickname updated '1' +301ms
bot-btc:updateNick:debug Updating nickname of 1 to 2 +0ms
bot-btc:updateNick:debug Nickname updated '2' +247ms
[...]
bot-btc:updateNick:debug Updating nickname of 18 to 19 +0ms
bot-btc:updateNick:debug Nickname updated '19' +797ms
bot-btc:updateNick:debug Updating nickname of 19 to 20 +0ms
bot-btc:updateNick:debug Nickname updated '20' +334ms
bot-btc:updateNick:debug Updating nickname of 20 to 21 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 22 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 23 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 24 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 25 +0ms
bot-btc:updateNick:debug Updating nickname of 0 to 1 +0ms
bot-btc:updateNick:debug Nickname updated '1' +301ms
bot-btc:updateNick:debug Updating nickname of 1 to 2 +0ms
bot-btc:updateNick:debug Nickname updated '2' +247ms
[...]
bot-btc:updateNick:debug Updating nickname of 18 to 19 +0ms
bot-btc:updateNick:debug Nickname updated '19' +797ms
bot-btc:updateNick:debug Updating nickname of 19 to 20 +0ms
bot-btc:updateNick:debug Nickname updated '20' +334ms
bot-btc:updateNick:debug Updating nickname of 20 to 21 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 22 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 23 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 24 +0ms
bot-btc:updateNick:debug Updating nickname of 20 to 25 +0ms
7 Replies
d.js toolkit
d.js toolkit10mo 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!
Tincho
TinchoOP10mo ago
discord.js 14.13.0
node v18.18.0
discord.js 14.13.0
node v18.18.0
Hahaha I understand that it is being filtered by Spam, and my intention is to lower the frequency... the problem is that no one tells me what the limitation is 😕
d.js docs
d.js docs10mo ago
Ratelimits are dynamically assigned by the API based on current load and may change at any point. - The scale from okay to API-spam is sliding and depends heavily on the action you are taking - Rainbow roles, clock and counter channels, and DM'ing advertisements to all members are all examples of things that are not okay
Tincho
TinchoOP9mo ago
Thank you so much! I'll look at this and come back for more help 😁 Hello, this is the solution I found so that the bot can change its nickname freely and manage the API limits:
client.guilds.cache.forEach((guild) => {
if (nickname != guild.members.me?.nickname) {
if (trackLimits.has(guild.id)) {
if (trackLimits.get(guild.id)! < new Date().getTime()) {
debug("Resetting limit");
trackLimits.delete(guild.id);
} else {
return;
}
}

guild.members.me?.setNickname(nickname).catch((err) => {
if (err instanceof RateLimitError) {
error(
"LIMIT on guild %s - Waiting for %d",
guild.id,
err.timeToReset
);
trackLimits.set(guild.id, err.timeToReset + new Date().getTime());
} else {
error("Error in setNickname: %o", err);
}
});
}
});
client.guilds.cache.forEach((guild) => {
if (nickname != guild.members.me?.nickname) {
if (trackLimits.has(guild.id)) {
if (trackLimits.get(guild.id)! < new Date().getTime()) {
debug("Resetting limit");
trackLimits.delete(guild.id);
} else {
return;
}
}

guild.members.me?.setNickname(nickname).catch((err) => {
if (err instanceof RateLimitError) {
error(
"LIMIT on guild %s - Waiting for %d",
guild.id,
err.timeToReset
);
trackLimits.set(guild.id, err.timeToReset + new Date().getTime());
} else {
error("Error in setNickname: %o", err);
}
});
}
});
Where
const trackLimits = new Collection<string, number>();
const trackLimits = new Collection<string, number>();
And
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
],
rest: {
rejectOnRateLimit: (rateLimitData) => {
// Handle rate limit on setNickName
return rateLimitData.route === "/guilds/:id/members/@me";
},
},
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
],
rest: {
rejectOnRateLimit: (rateLimitData) => {
// Handle rate limit on setNickName
return rateLimitData.route === "/guilds/:id/members/@me";
},
},
});
Thank you for your feedback! Do you have any recommendations for making a ticker style bot? Currently my bot offers the bitcoin price near in real time
Tincho
TinchoOP9mo ago
No description
d.js docs
d.js docs9mo ago
Ratelimits are dynamically assigned by the API based on current load and may change at any point. - The scale from okay to API-spam is sliding and depends heavily on the action you are taking - Rainbow roles, clock and counter channels, and DM'ing advertisements to all members are all examples of things that are not okay
Tincho
TinchoOP9mo ago
Mmmmmmmmm..... I see Maybe I'm abusing your cordiality and your time, but do you have any idea how to display information near real time on Discord? Wouldn't sending 1 message every 2 or 3 seconds also be spam? 🤔 Roger that In short: Discord is not made to display information in (near) real time
Want results from more Discord servers?
Add your server