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 toolkit2y 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
LegendaryOP2y ago
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
d42y 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
LegendaryOP2y ago
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
d42y 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
LegendaryOP2y ago
No i am not getting rate limited And a ratelimit shouldn't cause whole express server to hang
d4
d42y ago
it would cause the guilds.get request to hang iirc
Legendary
LegendaryOP2y ago
But other endpoint should work even if only this one get stuck But i receive 504 by ngnix in any endpoint
d4
d42y 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
LegendaryOP2y ago
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
d42y 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
LegendaryOP2y ago
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
Legendary
LegendaryOP2y ago
Hmm its a partial tho
d4
d42y ago
so what? it still tells you which guilds you have
Legendary
LegendaryOP2y ago
This can also get ratelimited ?? ??
d4
d42y 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
LegendaryOP2y ago
I will implement this but still there should be a solution to prevent the hang at a ratelimit by just stopping there
Legendary
LegendaryOP2y ago
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
d42y 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
LegendaryOP2y ago
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
d42y ago
ngl it's kinda confusing when you name the Client api and when you name the API client
Legendary
LegendaryOP2y ago
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
d42y ago
it doesn't look like you're handling the event right, the events in core work differently from the main djs
Legendary
LegendaryOP2y ago
Someone of the code got cut due to copying from terminal. Lemme give again
d4
d42y ago
hide your token though yes but he was about to sent the uncut code just making sure
Legendary
LegendaryOP2y ago
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
d42y ago
you use GuildUpdate
Legendary
LegendaryOP2y ago
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
LegendaryOP2y ago
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
LegendaryOP2y ago
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
LegendaryOP2y ago
These alone are gonna take up huge space in cache
Legendary
LegendaryOP2y ago
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
Want results from more Discord servers?
Add your server