是否可以更改与 discord.py 不一致的特定用户的权限

Is it possible to change the permissions of a specific user in discord with discord.py

我正在 python 上为 Discord 制作 Mafia 游戏机器人。我是编程初学者,我 运行 遇到了一个问题。当我创建游戏时,我的机器人 makes a category with three channels: a text channel for everyone, a voice channel for everybody, and a text channel specifically for mafia players. 然后机器人会为每个玩家分配一个带有游戏编号的角色。问题是我想让黑手党聊天只对黑手党可见。而且我不想为它创建一个单独的角色,因为如果有人检查玩家的角色,它将是可见的。

有没有办法以某种方式编辑频道的权限并使其只有特定的一群人才能看到该频道。 You can do that by hand,但我没能找到用机器人来做的方法。

以下示例展示了您如何能够为特定用户编辑权限:

@bot.command()
async def togglechat(ctx, member: discord.Member):
    perms = ctx.channel.overwrites_for(member)
    await ctx.channel.set_permissions(member, read_messages=not perms.read_messages)
    await ctx.send(f"Successfully toggled {member.name}'s view of this channel!")

默认情况下,成员的值可能设置为 None,并且切换成员对频道的视图几次在逻辑上将如下所示:

>>> view = None
>>> view = not view
>>> view
True
>>> view = not view
>>> view
False
>>> view = not view
>>> view
True

您也可以根据他们收到的角色将其设置为特定值 (True/False);

perms.read_messages = True # or False

您还可以使用 ctx.guild.default_role 将频道的 @everyone's view of the channel. This means that you can set their view of the channel to False and then edit the mafia members' 视图编辑为 True.


参考文献: