【问题标题】:Delete All Roles In A Server | Discord.js删除服务器中的所有角色 |不和谐.js
【发布时间】:2021-01-16 12:23:17
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    据我了解 .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

    【讨论】:

    • 承诺不需要兑现。
    猜你喜欢
    • 2020-12-21
    • 2020-10-04
    • 2021-10-22
    • 1970-01-01
    • 2021-07-11
    • 2021-10-19
    • 2021-07-16
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多