繁体   English   中英

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

[英]Delete All Roles In A Server | Discord.js

我正在尝试使用我的不和谐机器人删除我的服务器的所有角色。 但它想出了这个错误:

(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());
        }

但无济于事。 我仍然遇到与以前相同的错误。 我该怎么做才能解决这个问题?

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

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

此处的文档中找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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