discord js core issue
Sometime halfway through guilds.map(async (guild) =>{..} ) cpu usage goes abnormally high ,ram usage increases by like 50mb and server gets stuck , it doesn't continue . And remains stuck till it is killed
Full code: https://pastecode.io/s/mk7w8hu2
the code which could be responsable: https://pastecode.io/s/mk7w8hu2
Note: In this code guilds is a array of guilds fetched by api
And i am using discord js core
I asked from help from other servers first they told me nothing seems of in the code and it could be a discordjs core issue
34 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.Using latest discordjs core
Facing similar issue in discord.js I moved from discordjs to discord js core thinking it was cache issue but still facing it
In discordjs core
in the attached code you aren't even using djs core, also you are not using discord.js for fetching the guilds, and it seems like you have a very outdated version of discord.js too
What why are assuming this. I have imported client from my index.ts and in index.ts its a discord js core client
index.js:
const rest = new REST({ version: "10" }).setToken(token);
const gateway = new WebSocketManager({
token,
intents: GatewayIntentBits.Guilds,
rest,
});
const api = new Client({ rest, gateway });
export const client = api.api;
ah apologies then, i assumed that because you named it
client
when typically it's named api
i would assume here that you are most likely being rate limited by discord, fetching every single guild like this would be api spamNo i am not getting rate limited
And a ratelimit shouldn't cause whole express server to hang
it would cause the
guilds.get
request to hang iircBut other endpoint should work even if only this one get stuck
But i receive 504 by ngnix in any endpoint
also looks like you're fetching guilds using a
Bearer
token? that's probably against TOS
also since users can be in 100 guilds or more and your bot has to fetch every single one, theoretically your bot could have over 100 hanging promises running in the background each one dealing with discord's rate limitSee two ratelimit can occur
1. Fetching user guilds by oauth token
2. Fetching guild by bot client
I think 1 shows the error when it happens so should 2?
If not whats the solution
Like stop going through every server at a rate limit
djs handles rate limits for you thus the fetching with bot client doesn't show anything
but since it handles them for you your promises hang
why would you need to fetch with bot client anyway if you already fetch the user guilds?
So adding try {} catch (e) {
if (// rate limit error code) {
// Exit
}
} Won't work?
To know if bot is in which guilds and show something like a invite button if its not present
you should use
UsersAPI#getGuilds
then https://discord.js.org/docs/packages/core/0.6.0/UsersAPI:Class#getGuildsHmm its a partial tho
so what? it still tells you which guilds you have
This can also get ratelimited
??
??
this won't work because iirc djs won't throw an error if the rate limit is expected, the
rateLimited
event does exist for the REST
class though
sure, theoretically anything can, but at least you won't have 100+ requests running at the same time
i also recommend setting up caching here so you won't have to request guilds every single timeI will implement this but still there should be a solution to prevent the hang at a ratelimit by just stopping there
again, the
rateLimited
event exists for REST
, and RESTOptions
also has something for ratelimits iirc
https://discord.js.org/docs/packages/rest/main/RestEvents:Interface#rateLimited
https://discord.js.org/docs/packages/rest/main/RESTOptions:Interface#rejectOnRateLimitHmm
Thanks
I also wanted to tell this only happens sometimes. Once i kept reloading page every 1s but still didn't receive this issue. Happens at random instance , could be due to another main bot client which handles commands and all ( it runs on seperate server though )
This won't work because documention says:
Returns a list of partial guild objects the current user is a member of
And going through 6000 servers bot is in would be a nightmarealso since you said you'd implement caching you could do so using a Map or Collection in which case checking if a bot is in a guild would be easy and fast
Hmm thanks guys
I would have been clueless without both of you
As qjuh told be to use ws to add new guilds to cache by using guildcreate event
But old guild should be in the cache so i should fetch all of the guilds at once at startup and add them to cache
Uh i thought it would be similar to discord.js (main). Sorry
Yeah
I thought guildcreate would only fire and send the guild which bot jois in future
Oh ok
Using discordjs core makes me realise how much discord.js does for us
Cache was 1 and client.users.getGuilds() length was is there
ngl it's kinda confusing when you name the Client
api
and when you name the API client
Yeah. You are most active,helpful staff rn
Lol guild, channels etc are method of API thats why for my convience i did rhat
So why only one guild is coming through guildcreate event?
it doesn't look like you're handling the event right, the events in core work differently from the main djs
Someone of the code got cut due to copying from terminal. Lemme give again
hide your token though
yes but he was about to sent the uncut code
just making sure
Deleted that message
Oh my bad
Not much gives about these events in website
Working as intended now
I have ran it for like 2 min
Test Bot is in 60 server (most of them dead)
The guild create event fired 120 times
Why??
I don't think so. By 240 times fired
Wait lemme chrck
Its running on my machine so it could be reconnecting
Does guildcreate fire again if some properties changed or you have to do that manually?
you use GuildUpdate
Resumed event didn't fire or does other fire on reconnect
I changed api.once to api.on and noticed that Ready event fired again( because of unstable internet of my local machine ) i will consider that as a reconnect.
Is there a setting related to limit what data we recieve in guildcreate event?
Ok i guess. How can i wait till all guilds are added to cache ?
Not possible to disable all of the cache
I think just guildmanager cache cannot be stopped but guilds have huge amount of data
And i just need to store their ids
I think only solution for know is implement the way discordjs guilds have been received
GitHub
discord.js/packages/discord.js/src/client/websocket/WebSocketManage...
A powerful JavaScript library for interacting with the Discord API - discord.js/packages/discord.js/src/client/websocket/WebSocketManager.js at main · discordjs/discord.js
It still loads many servers and their data to cache .
On Startup
I just want guild ids
Not their data
It goes upto 1gb
6000 guild
I did most of them not all to be honest
Still got 1gb usage
These alone are gonna take up huge space in cache
One solution for using discord.js core is get /users/@me/guilds/ once at am stuck between the uncustomiziblty of discordjs and features deficiency of core will . I have to rewrite the code that once
Even if we do this in actual bot process getting all guilds would still be issue because discord.js cache can't have all server and if i use fetch there are chances that i would again face the hang issue
Umm what. Ok