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
d.js toolkit
d.js toolkit11mo ago
• 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.
legendary_aka_jagrit
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
d4
d411mo ago
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
legendary_aka_jagrit
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;
d4
d411mo ago
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 spam
legendary_aka_jagrit
No i am not getting rate limited And a ratelimit shouldn't cause whole express server to hang
d4
d411mo ago
it would cause the guilds.get request to hang iirc
legendary_aka_jagrit
But other endpoint should work even if only this one get stuck But i receive 504 by ngnix in any endpoint
d4
d411mo ago
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 limit
legendary_aka_jagrit
See 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
d4
d411mo ago
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?
legendary_aka_jagrit
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
d4
d411mo ago
legendary_aka_jagrit
Hmm its a partial tho
d4
d411mo ago
so what? it still tells you which guilds you have
legendary_aka_jagrit
This can also get ratelimited ?? ??
d4
d411mo ago
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 time
legendary_aka_jagrit
I will implement this but still there should be a solution to prevent the hang at a ratelimit by just stopping there
d4
d411mo ago
legendary_aka_jagrit
Hmm 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 nightmare
d4
d411mo ago
also 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
legendary_aka_jagrit
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 pikaOh is there
d4
d411mo ago
ngl it's kinda confusing when you name the Client api and when you name the API client
legendary_aka_jagrit
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?
d4
d411mo ago
it doesn't look like you're handling the event right, the events in core work differently from the main djs
legendary_aka_jagrit
Someone of the code got cut due to copying from terminal. Lemme give again
d4
d411mo ago
hide your token though yes but he was about to sent the uncut code just making sure
legendary_aka_jagrit
const { REST } = require ("@discordjs/rest");
const { WebSocketManager } = require("@discordjs/ws");
const {
GatewayDispatchEvents,
GatewayIntentBits,
Client,
} = require( "@discordjs/core")
const { Collection } = require('discord.js')

const guildCache = new Collection();
const token = "token"
const rest = new REST({ version: "10" }).setToken(token);
const gateway = new WebSocketManager({
token,
intents: GatewayIntentBits.Guilds,
rest,
});
const api = new Client({ rest, gateway });

const client = api.api;

api.on(GatewayDispatchEvents.GuildCreate , guild => {
guildCache.set(guild.id, guild);


})
api.once(GatewayDispatchEvents.Ready, () => {
console.log("Bot Ready!");

});
setInterval ( function(){
console.log(guildCache.size,//guildCache
);}
,5000)
gateway.connect()
setTimeout( function(){
client.users.getGuilds().then(a => console.log(a.size || a.length))
} , 6000)
const { REST } = require ("@discordjs/rest");
const { WebSocketManager } = require("@discordjs/ws");
const {
GatewayDispatchEvents,
GatewayIntentBits,
Client,
} = require( "@discordjs/core")
const { Collection } = require('discord.js')

const guildCache = new Collection();
const token = "token"
const rest = new REST({ version: "10" }).setToken(token);
const gateway = new WebSocketManager({
token,
intents: GatewayIntentBits.Guilds,
rest,
});
const api = new Client({ rest, gateway });

const client = api.api;

api.on(GatewayDispatchEvents.GuildCreate , guild => {
guildCache.set(guild.id, guild);


})
api.once(GatewayDispatchEvents.Ready, () => {
console.log("Bot Ready!");

});
setInterval ( function(){
console.log(guildCache.size,//guildCache
);}
,5000)
gateway.connect()
setTimeout( function(){
client.users.getGuilds().then(a => console.log(a.size || a.length))
} , 6000)
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?
d4
d411mo ago
you use GuildUpdate
legendary_aka_jagrit
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
legendary_aka_jagrit
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
legendary_aka_jagrit
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
legendary_aka_jagrit
These alone are gonna take up huge space in cache
legendary_aka_jagrit
One solution for using discord.js core is get /users/@me/guilds/ once at FeelsBadMan am stuck between the uncustomiziblty of discordjs and features deficiency of core FeelsBadMan 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