简体   繁体   中英

Delete All Roles In A Server | Discord.js

I am trying to delete all roles of my server with my discord bot. But it came up with this error:

(node:10096) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Role
    at RequestHandler.execute (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\amy nuker\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:10096) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10096) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My code:

        if(message.content.startsWith(prefix + 'roled')){

        message.guild.roles.cache.forEach(roles => roles.delete());

        }

This didn't work so I tried using the .map() fucntion

        if(message.content.startsWith(prefix + 'roled')){
        //  message.guild.roles.cache.map(roles => roles.delete());
        }

But to no avail. I still got the same error as before. What do I do to fix this?

From what I understand .delete() returns a promise so you need to fulfill it either using async/await or then/catch

message.guild.roles.cache.forEach(roles => {
    roles.delete()
    .then(deleted => console.log(`Deleted role ${deleted.name}`))
    .catch(console.error);
});

Found in documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM