删除服务器中的所有角色Discord.js

时间:2020-09-30 19:06:39

标签: javascript node.js discord discord.js

我正在尝试使用discord bot删除服务器的所有角色。但是它出现了这个错误:

(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.

我的代码:

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

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

        }

此操作无效,因此我尝试使用.map()功能

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

但无济于事。我仍然遇到和以前一样的错误。我该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:0)

据我了解,.delete()返回了一个承诺,因此您需要使用async/awaitthen/catch来实现它

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

documentation here中找到