如何使用discord.py更改公会所有频道的权限?

时间:2020-09-05 14:26:48

标签: python discord discord.py

我想将服务器所有通道的权限从“读取消息=真”更改为“假”。基本上,我不希望具有default-role的用户能够看到任何频道。 我写了什么

@bot.command()
async def maintainance(ctx):
    channel = ctx.guild.channels
    perms = channel.overwrites_for(ctx.guild.default_role)
    perms.read_messages=False
    for channel in ctx.guild.channels:
        await channel.set_permissions(ctx.guild.default_role, overwrite=perms)
        await ctx.message.add_reaction(emoji="<a:tick:748476262640779276>")

我得到的错误:

Ignoring exception in command maintainance:
Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\Rohit\Desktop\discord bots\tutorial bot\bot.py", line 23, in maintainance
    perms = channel.overwrites_for(ctx.guild.default_role)
AttributeError: 'list' object has no attribute 'overwrites_for'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'list' object has no attribute 'overwrites_for'

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

覆盖是按通道进行的,因此您需要在for循环中进行get_overwrites ,有些伪代码看起来像

for every channel:
    overwrites = get_overwrites()
    overwrites.send_message = False
    channel.set_permissions(default_role, overwrites=overwrites)

另一件事是,您应该在循环外执行ctx.message.add_reaction(),以避免尝试多次添加相同的反应