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
d.js toolkit
d.js toolkitā€¢12mo 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! - āœ… Marked as resolved by OP
Danial
Danialā€¢12mo ago
Are you awaiting it?
Mortai
MortaiOPā€¢12mo ago
I have tried using await, I have tried using .then() (they both wait forever)
Danial
Danialā€¢12mo ago
Show your code
Mortai
MortaiOPā€¢12mo ago
ok one sec basically
const topRoles = [
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),

guild.roles
.create({
name: "#2 Magic",
color: "#C0C0C0",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),
]
const topRoles = [
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),

guild.roles
.create({
name: "#2 Magic",
color: "#C0C0C0",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),
]
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 šŸ
Danial
Danialā€¢12mo ago
You could be getting rateLimited, listen to <Client>.rest.on("rateLimited")
Mortai
MortaiOPā€¢12mo ago
how can I not get ratelimited tho, i need to create the 10 roles also wdym by "listen to <Client>.rest.on("rateLimited")"
Danial
Danialā€¢12mo ago
rateLimited event is emitted when you get rate limited, so listen to it like you do with ready, etc.
Mortai
MortaiOPā€¢12mo ago
ok one sec
Danial
Danialā€¢12mo ago
By having a decent amount of interval between role creations
Mortai
MortaiOPā€¢12mo ago
ill try it alr lemme just check if i get rastelimited
client.on('rateLimited', function() {
console.log('Rate limited');
})
client.on('rateLimited', function() {
console.log('Rate limited');
})
Like this?? @Danial šŸ
Danial
Danialā€¢12mo ago
^ <Client>.rest.on("rateLimited")
Mortai
MortaiOPā€¢12mo ago
in angular brackets? or like this
client.rest.on("rateLimited", function() {
console.log('RATE LIMITED')
})
client.rest.on("rateLimited", function() {
console.log('RATE LIMITED')
})
@Danial šŸ
Danial
Danialā€¢12mo ago
That should work, yeah
Mortai
MortaiOPā€¢12mo ago
okay if i made it console.error() instead of console.log() would it stop my code? so i would know if i got limited?
treble/luna
treble/lunaā€¢12mo ago
just listen for the event the event might fire for other things too
Mortai
MortaiOPā€¢12mo ago
ooh yeah it fired
treble/luna
treble/lunaā€¢12mo ago
shutting down your bot isnt a good idea
Danial
Danialā€¢12mo ago
No, that's not how console.error() works
Mortai
MortaiOPā€¢12mo ago
ik but just for testing alright it fired like once so how would i fix this?
treble/luna
treble/lunaā€¢12mo ago
By having a decent amount of interval between role creations
Mortai
MortaiOPā€¢12mo ago
okay soo how do i do that if i am creating them all in an array
d.js docs
d.js docsā€¢12mo ago
mdn 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.
Mortai
MortaiOPā€¢12mo ago
how do i delay the creation?
treble/luna
treble/lunaā€¢12mo ago
or
d.js docs
d.js docsā€¢12mo ago
mdn setTimeout() global function The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
Mortai
MortaiOPā€¢12mo ago
they're in an array though
Danial
Danialā€¢12mo ago
How would that make a difference?
Mortai
MortaiOPā€¢12mo ago
^ well like idk how to do that
Danial
Danialā€¢12mo ago
Maybe read the docs you were linked
Mortai
MortaiOPā€¢12mo ago
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?
treble/luna
treble/lunaā€¢12mo ago
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
Mortai
MortaiOPā€¢12mo ago
okay, but how do i stop my code after the array from executing until all the roles are created
treble/luna
treble/lunaā€¢12mo ago
wrap it in a promise and resolve it once you're done
Mortai
MortaiOPā€¢12mo ago
okay also how much delay would be neccessary? like 2 seconds per role? or more
treble/luna
treble/lunaā€¢12mo ago
ratelimits are dynamic so there's no set delay just start with 2s and see
Mortai
MortaiOPā€¢12mo ago
okay huhh i put it in a settimeout and its underlined
setTimeout(function() {
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),
}, 3000);
setTimeout(function() {
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
}),
}, 3000);
whats wrong with that?? @arcticwolvinny ā„ šŸŒˆ i forgot a bracket or somethin
treble/luna
treble/lunaā€¢12mo ago
can you not ping me? I'll see this eventually. And show the error
Mortai
MortaiOPā€¢12mo ago
sorry
treble/luna
treble/lunaā€¢12mo ago
and what is that , doing there
Mortai
MortaiOPā€¢12mo ago
ohh ty
nick.
nick.ā€¢12mo ago
you still have a comma after the .catch even though this is no longer in an array
Mortai
MortaiOPā€¢12mo ago
yeah ik i found that from arcticwolvinny ty tho
nick.
nick.ā€¢12mo ago
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:
// Use the Promise version of setTimeout provided by node
const { setTimeout } = require("node:timers/promises");

// Iterate over the array, create a role and then wait
for (const role of roles) {
try {
createdRoles.push(await guild.roles.create({ /* your code here */ }));
} catch (error) {
/* your code here */
} finally {
await setTimeout(2000); // wait 2 seconds
}
}
// Use the Promise version of setTimeout provided by node
const { setTimeout } = require("node:timers/promises");

// Iterate over the array, create a role and then wait
for (const role of roles) {
try {
createdRoles.push(await guild.roles.create({ /* your code here */ }));
} catch (error) {
/* your code here */
} finally {
await setTimeout(2000); // wait 2 seconds
}
}
Mortai
MortaiOPā€¢12mo ago
guys does settimeout yield the whole thread?
setTimeout(() => {
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 3000);

setTimeout(() => {
guild.roles
.create({
name: "#2 Magic",
color: "#C0C0C0",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 6000);

setTimeout(() => {
guild.roles
.create({
name: "#3 Magic",
color: "#CD7F32",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 9000);
setTimeout(() => {
guild.roles
.create({
name: "#1 Magic",
color: "#FFD700",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 3000);

setTimeout(() => {
guild.roles
.create({
name: "#2 Magic",
color: "#C0C0C0",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 6000);

setTimeout(() => {
guild.roles
.create({
name: "#3 Magic",
color: "#CD7F32",
}).then((role) => {
topRoles.push(role);
})
.catch((error) => {
console.log("There was an error creating one of the top 10 roles.");
})
}, 9000);
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
nick.
nick.ā€¢12mo ago
you mean if you have an array of promises
d.js docs
d.js docsā€¢12mo ago
mdn 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.
Mortai
MortaiOPā€¢12mo ago
yeah, how do i wait for them all to finish so i would do promise.all(array).then()?
nick.
nick.ā€¢12mo ago
or await and you would get an array of roles
Mortai
MortaiOPā€¢12mo ago
so
nick.
nick.ā€¢12mo ago
or an error
Mortai
MortaiOPā€¢12mo ago
could you give a code example if i had an array of role promises how would i do that
nick.
nick.ā€¢12mo ago
const roles = await Promise.all(promises)
Mortai
MortaiOPā€¢12mo ago
and this would be an array of the roles?
nick.
nick.ā€¢12mo ago
mhm
Mortai
MortaiOPā€¢12mo ago
okay, lemme try rq thanks
nick.
nick.ā€¢12mo ago
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
d.js docs
d.js docsā€¢12mo ago
mdn 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.
nick.
nick.ā€¢12mo ago
allSettled will never reject (catch is not needed), instead you can check each individual element to see if it was successful or not
Mortai
MortaiOPā€¢12mo ago
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?
const topRoles = await Promise.all(topRolesPromises);
const topRoles = await Promise.all(topRolesPromises);
and then I could iterate through topRoles? @nick. would that work?
nick.
nick.ā€¢12mo ago
yes indeed, if topRolesPromises is a Promise<Role>[], awaiting Promise.all of it will yield Role[]
Mortai
MortaiOPā€¢12mo ago
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?
treble/luna
treble/lunaā€¢12mo ago
just pass in a parameter in the callback instead of an empty function
Mortai
MortaiOPā€¢12mo ago
thanks i will try it when i get on my computer it works!! thanks for your help guys
Want results from more Discord servers?
Add your server