问题
This error is a weird one because the code works: it creates an admin role with the Administrator flag. And most importantly: the bot is above the ADMIN role and is Administrator.
The error appears when it's creating the role. The error says it's missing permissions but the ADMIN role is created successfully so it didn't fail. And there's another thing: when I leave out the position parameter it works just fine, no errors at all. But unfortunately I need the position parameter because I want to add more roles. What am I missing here?
bot.on('ready', () => {
const ids = bot.guilds.cache.map(guild => guild.id);
// Checks if the necessary roles are present on every guild, creates them if they are not there.
for (const id of ids) {
let guild = bot.guilds.cache.get(id);
async function assignRoles() {
let adminRole = guild.roles.cache.find(role => role.name === "ADMIN") ? console.log("Role ADMIN is present at " + '\x1b[36m%s\x1b[0m%s', guild.name, ".") : guild.roles.create({
data:{
name:"ADMIN",
color:"GREEN",
hoist: true,
position: 2,
permissions: ["ADMINISTRATOR"]
},
reason:"The administrator role.",
}).then(console.log("Created the necessary role ADMIN at " + '\x1b[35m%s\x1b[0m%s', guild.name, "."))
let allRole = await adminRole; // + await mutedRole + await friendRole
return allRole;
}
assignRoles();
}
}
来源:https://stackoverflow.com/questions/64201709/it-works-but-still-getting-this-error-unhandledpromiserejectionwarning-discord