You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Discord.js中使用BitField数值分配用户/角色权限?

如何通过BitField数值直接为用户/角色分配Discord频道权限?

当然可以用BitField数值直接设置权限,你代码里的问题是没有按照API要求的格式传入BitField数据。

你当前直接把PermissionsBitField对象放进配置对象里,但permissionOverwrites.edit方法的第二个参数需要通过allow(或deny)字段来接收权限位域的数值或对象,而不是直接把BitField对象作为键值对传入。

修正后的代码示例

方式1:直接传入计算好的位域数值

const channelforedit = i.options.getChannel("channel") ?? i.channel;
const userforperms = i.options.getUser("user") ?? i.user;
const permscalculate = i.options.getInteger("perms") ?? 0;

// 直接将数值转为BigInt后传入allow字段
i.guild.channels.cache.get(channelforedit.id).permissionOverwrites.edit(userforperms.id, {
  allow: BigInt(permscalculate)
});

i.reply({content: `已为用户 <@${userforperms.id}> 设置权限位域:${permscalculate}`, ephemeral: true});

方式2:使用PermissionsBitField对象

const channelforedit = i.options.getChannel("channel") ?? i.channel;
const userforperms = i.options.getUser("user") ?? i.user;
const permscalculate = i.options.getInteger("perms") ?? 0;
        
const perms = new PermissionsBitField(BigInt(permscalculate));

// 传入PermissionsBitField对象的value属性(Discord.js v14+也支持直接传对象)
i.guild.channels.cache.get(channelforedit.id).permissionOverwrites.edit(userforperms.id, {
  allow: perms.value // 或直接写 allow: perms
});

i.reply({content: `已为用户 <@${userforperms.id}> 设置权限位域:${permscalculate}`, ephemeral: true});

补充说明

  • 如果需要同时设置允许和拒绝的权限,可以同时使用allow和deny字段,比如:{ allow: 允许位域数值, deny: 拒绝位域数值 }
  • Discord.js v14及以上版本要求权限数值为BigInt类型,必须确保数值转换正确,否则会出现权限不生效的情况

内容的提问来源于stack exchange,提问作者Servantr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:55:17