Discord JS 机器人,编辑权限

问题描述 投票:0回答:1

我正在尝试创建一个斜杠命令,但我不太明白如何根据字符串输入编辑权限,例如,如果一个人输入带有角色和 true bool 的“SEND_MESSAGES”,当前使用 Defined_permission 进行测试

脚本:

const role = interaction.options.getRole('role');
const defined_permission = interaction.options.getString('defined-permission');
const custom_permission = interaction.options.getString('custom-permission');
const enabled = interaction.options.getBoolean('enabled');

console.log(`🤔 > DEBUG | ${defined_permission}`)

if (!enabled) {
    interaction.reply("You are required to put a true or a false on if it should added or removed.");
    return
}

if (!role) {
    interaction.reply("You are required to define a role")
    return
}

if (!defined_permission && custom_permission) {

   } else if (defined_permission && !custom_permission) {
          await role.setPermissions({ [defined_permission]: enabled})
          interaction.reply(`Added the permission ${defined_permission} to the role <@&${role.id}>`)
          console.log(`✅ > ${interaction.user.username} added the permission ${defined_permission} to the role ${role.name}`)
            
   } else if (defined_permission && custom_permission) {

   } else {
          interaction.reply("Please input what permission you would like to edit")
   }

错误:

node:events:492
      throw er; // Unhandled 'error' event
      ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: [object Object].
    at PermissionsBitField.resolve (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\util\BitField.js:172:11)
    at new BitField (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\util\BitField.js:33:38)
    at new PermissionsBitField (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\util\PermissionsBitField.js:12:1)
    at RoleManager.edit (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\managers\RoleManager.js:203:68)
    at Role.edit (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\structures\Role.js:254:29)
    at Role.setPermissions (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\structures\Role.js:333:17)
    at Client.<anonymous> (C:\Development\Visual Studio Code Projects\GuildTweaker\src\index.js:277:24)
    at Client.emit (node:events:514:28)
    at InteractionCreateAction.handle (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Development\Visual Studio Code Projects\GuildTweaker\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:395:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'BitFieldInvalid'
}

期望: 如果我要使用以下命令: “/编辑@definerole4 true SEND_MESSAGES” “/编辑 [角色] [布尔] [权限]”

javascript discord permissions discord.js roles
1个回答
0
投票

Role.setPermissions()
只接受 PermissionResolvable 的数组。

await role.setPermissions(['AddReactions', 'SendMessages']);

并删除权限:

await role.setPermissions(0n);
© www.soinside.com 2019 - 2024. All rights reserved.