Issue with guild.roles.create()
I am trying to create a role with discord.js, using guild.roles.create(). The promise this function returns stays in the <pending> state indefinitely. Please help!!
64 Replies
- 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!
- ā
Marked as resolved by OPAre you
await
ing it?I have tried using await, I have tried using .then() (they both wait forever)
Show your code
ok one sec
basically
i do that for 10 roles
i couldnt fit it all in 1 message
and then i loop through them all and do a .then()
and it never fires
@Danial š
You could be getting rateLimited, listen to <Client>.rest.on("rateLimited")
how can I not get ratelimited tho, i need to create the 10 roles
also wdym by "listen to <Client>.rest.on("rateLimited")"
rateLimited event is emitted when you get rate limited, so listen to it like you do with ready, etc.
ok one sec
By having a decent amount of interval between role creations
ill try it
alr lemme just check if i get rastelimited
Like this??
@Danial š
^ <Client>.rest.on("rateLimited")
in angular brackets?
or like this
@Danial š
That should work, yeah
okay
if i made it console.error() instead of console.log()
would it stop my code?
so i would know if i got limited?
just listen for the event
the event might fire for other things too
ooh yeah it fired
shutting down your bot isnt a good idea
No, that's not how console.error() works
ik but just for testing
alright
it fired like once
so how would i fix this?
By having a decent amount of interval between role creations
okay soo
how do i do that
if i am creating them all in an array
setInterval() global function
The setInterval() method, offered on the Window and WorkerGlobalScope interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
how do i delay the creation?
or
setTimeout() global function
The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
they're in an array though
How would that make a difference?
^
well like idk how to do that
Maybe read the docs you were linked
yeah I know how those functions work
but how do i implement it in the array, won't the other code after the array execute before the roles are created?
or just not use an array
since your array now would just be an array of unresolved promises
just push the roles into the array after you created them
okay, but how do i stop my code after the array from executing
until all the roles are created
wrap it in a promise
and resolve it once you're done
okay
also how much delay would be neccessary?
like 2 seconds per role?
or more
ratelimits are dynamic
so there's no set delay
just start with 2s and see
okay
huhh
i put it in a settimeout and its underlined
whats wrong with that??
@arcticwolvinny ā š
i forgot a bracket or somethin
can you not ping me? I'll see this eventually. And show the error
sorry
and what is that , doing there
ohh
ty
you still have a comma after the .catch even though this is no longer in an array
yeah ik
i found that from arcticwolvinny
ty tho
side note, if you did have an array of roles you wanted to create (say some array of objects) you can loop with a delay with less boilerplate like this:
guys
does settimeout yield the whole thread?
I still get ratelimited doing this
@Danial š
this is still ratelimiting me
welp im stuck
š
I have no idea what to do now
its still ratelimiting me with the delays
if anyone knows why then tell me ty
how do i wait until they are all created?
(like once the promises finish pending)
@Qjuh
you mean if you have an array of promises
Promise.all()
The Promise.all() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason.
yeah, how do i wait for them all to finish
so i would do promise.all(array).then()?
or await
and you would get an array of roles
so
or an error
could you give a code example
if i had an array of role promises
how would i do that
const roles = await Promise.all(promises)
and this would be an array of the roles?
mhm
okay, lemme try rq thanks
one thing you may want to consider though, is that if for some reason one of them fails to create, this method will reject even though some of them were created successfully
if you wanted to be able to see which ones created successfully and which didn't, consider this method instead
Promise.allSettled()
The Promise.allSettled() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises settle (including when an empty iterable is passed), with an array of objects that describe the outcome of each promise.
allSettled will never reject (catch is not needed), instead you can check each individual element to see if it was successful or not
ill just try all() for now
and i can move onto allSettled() if all() works
thanks š
if topRolesPromises is an array of promises for role creations, would this work?
and then I could iterate through topRoles? @nick.
would that work?
yes
indeed, if topRolesPromises is a Promise<Role>[], awaiting Promise.all of it will yield Role[]
how long would this take?
i let it run for like 6 minutes and it didn't return anything
@nick. gotta go now bye
where?
where does it tell you?
could you give a code example for this?
just pass in a parameter in the callback
instead of an empty function
thanks i will try it when i get on my computer
it works!!
thanks for your help guys