【问题标题】:How to change permissions of all channels of a guild with discord.py?如何使用 discord.py 更改公会所有频道的权限?
【发布时间】:2023-03-21 10:01:02
【问题描述】:

我想将服务器所有频道的权限从 Read messages = True 更改为 False。基本上我不希望具有默认角色的用户能够看到任何频道。 我写的:

@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'

我不知道我做错了什么。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    覆盖是每个频道的,所以你需要在 for 循环中执行 get_overwrites inside,一些伪代码看起来像

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 2019-10-11
      • 2021-05-02
      • 2021-12-10
      • 2020-11-14
      • 2021-04-18
      相关资源
      最近更新 更多