【问题标题】:Role Positions: Discord.js角色职位:Discord.js
【发布时间】:2020-10-23 13:55:37
【问题描述】:

我试图通过我的机器人以特定顺序在我的服务器中创建角色,但每次我运行命令时角色位置都是错误的。

代码:

client.on("message", message => {
    if(message.content.startsWith(prefix + "createrole")){
        message.guild.roles.create({
            data: {
                name: "Owner",
                color: "BLUE",
                position: 1
            }
        })
        .then(role => console.log(red(`Role created`)))
        .catch(err => console.log(err))
        message.guild.roles.create({
            data: {
                name: "Admin",
                color: "BLUE",
                position: 2
            }
        })
        .then(role => console.log(red(`Role created`)))
        .catch(err => console.log(err))
        message.guild.roles.create({
            data: {
                name: "Mod",
                color: "BLUE",
                position: 3
            }
        })
        .then(role => console.log(red(`Role created`)))
        .catch(err => console.log(err))
    }
})

理论上这应该是有序的,但我不确定发生了什么!

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    有趣的是,考虑到角色的索引方式,它给出了该输出。请注意,角色是根据@everyone 角色索引的,如果您的所有者角色高于您的管理员角色,它必须具有更高的职位编号。令人沮丧的是,我似乎找不到任何描述这种行为的东西,但是测试确实提供了这种行为。

    将 Owner 设置为 3,Admin 设置为 2,Mod 设置为 1 应该会得到您想要的结果。

    ...它仍然真的奇怪的是,Mod 最终以您当前的位置值高于 Owner,但我猜它就是这样。

    【讨论】:

    • 所以我已经尝试了你的建议,但它仍然混淆了角色位置。见这里:[media.discordapp.net/attachments/768896301256671262/…尝试)。然后我想你所说的关于所有者位置的索引更高所以我想将所有内容设置为 2 所以 mod:1,管理员:3,所有者:5,这确实给了我想要的结果。看这里:[media.discordapp.net/attachments/768896301256671262/… Attempt)即使它起作用了,它仍然显示我和错误。 DiscordAPIError: Missing Permissions
    • DiscordAPIError: Missing Permissions at RequestHandler.execute (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\Discord Bots\Testing\My Own Projects\Riven\node_modules\discord.js\src\rest\RequestHandler.js:154:13) at async RequestHandler.push (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\Discord Bots\Testing\My Own Projects\Riven\node_modules\discord.js\src\rest\RequestHandler.js:39:14) { method: 'patch', path: '/guilds/763488896892796938/roles', code: 50013, httpStatus: 400 }
    • 但它仍然会成为角色。但是刷新控制台后不会触发错误。
    【解决方案2】:

    似乎角色创建方法及其位置的数据属性以一种奇怪的方式索引,并且它的索引为 2 以使角色高于另一个角色。

    代码:

     if(message.content.startsWith(prefix + "createrole")){
            message.guild.roles.create({
                data: {
                    name: "Owner",
                    color: "BLUE",
                    position: 5
                }
            })
            .then(role => console.log(red(`Role created`)))
            .catch(err => console.log(err))
            message.guild.roles.create({
                data: {
                    name: "Admin",
                    color: "BLUE",
                    position: 3
                }
            })
            .then(role => console.log(red(`Role created`)))
            .catch(err => console.log(err))
            message.guild.roles.create({
                data: {
                    name: "Mod",
                    color: "BLUE",
                    position: 1
                }
            })
            .then(role => console.log(red(`Role created`)))
            .catch(err => console.log(err))
            
        }
    

    结果:

    这对我来说仍然很困惑,但它就是这样。

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 2021-01-28
      • 2017-08-25
      • 2021-02-07
      • 2020-08-24
      • 2020-12-31
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多