Discord Js 不更新权限

Discord Js not updating permissions

我原以为这段代码可以正常工作,但现在似乎已经停止了。我试过给机器人更多权限(例如管理员),但仍然不走运。没有错误,我真的不知道为什么这行不通。

基本上我正在尝试读取每个人角色的权限并将它们明确分配给我的新 role_object。 (最终要去掉everyone角色的权限)。

为了测试@每个人只有踢成员的权限(0b10),新角色没有权限(0b0)。

我希望看到:

2n
Permissions { bitfield: 0n }
Permissions { bitfield: 2n }

但我看到了:

2n
Permissions { bitfield: 0n }
Permissions { bitfield: 0n }

当我运行:

const role_object = await interaction.guild.roles.fetch(verify_role);
const normal_perms = interaction.guild.roles.everyone.permissions;

console.log(normal_perms.bitfield);

console.log(role_object.permissions);
await role_object.permissions.add(normal_perms.bitfield);
console.log(role_object.permissions);

感谢您的帮助!

嗯,首先,Role#permissions 属性 根据 docs. Calling permissions.add() does not actually modify the contents of the permissions object at all, as you've seen. But in fact, it also does not query the API to modify the permissions of the role, so even if it were modifying the contents of the permissions object, it would still not set the permissions of the role. You're using the wrong method. What you're looking for is Role#setPermissions() 是只读的。这可以接受位域 BigInt 作为参数,让您轻松设置角色的权限。这是一个示例,基于您提供的相同代码:

const role_object = await interaction.guild.roles.fetch(verify_role);
const normal_perms = interaction.guild.roles.everyone.permissions;

console.log(normal_perms.bitfield);

console.log(role_object.permissions);
await role_object.setPermissions(normal_perms.bitfield);
console.log(role_object.permissions);

这样,您的期望就会变成现实。我用我自己的机器人和公会测试了这段代码,这是我的结果(请记住,我的每个人角色的权限都与你的不同,因此位域值不同):

969693056576n
Permissions { bitfield: 0n }
Permissions { bitfield: 969693056576n }