discord.ext.commands.errors.MissingRequiredArgument: guild 是缺少的必需参数

discord.ext.commands.errors.MissingRequiredArgument: guild is a required argument that is missing

我想创建一个命令来设置所有文本频道的权限,但是我在创建这个命令时遇到了一些困难

我试过很多次,但我不记得我必须做什么 请帮助我,我需要一个代码

我的代码:

@bot.command()
async def close_all(ctx, *, guild: discord.Guild):
    for chan in guild.channels:
      await guild.channels.set_permissions(ctx.guild.default_role, send_messages=False)

错误: guild is a required argument that is missing.

尝试

@bot.command()
async def close_all(ctx):
    for chan in ctx.guild.channels:
      await chan.set_permissions(ctx.guild.default_role, send_messages=False)

查看您遇到的错误,在我看来您没有传递公会对象。此外,您的 for 循环实际上并没有使用它的索引,除非确保它运行一定次数。

只要您尝试更改的公会权限是您发送此消息的公会,这应该有效。

你现在正在做的是,要求公会作为你命令的参数,所以机器人实际上正在寻找的是一条消息

close_all <guild>

guild: discord.Guild 是公会的 Converter ,所以它应该以某种方式将字符串转换为公会对象。由于这实际上不可能,因此您的命令无法正常工作。

简单的解决方法:只要一直使用消息已发送的公会

@bot.command()
async def close_all(ctx):
    for chan in ctx.guild.channels:
        await guild.channels.set_permissions(ctx.guild.default_role, send_messages=False)

更灵活的解决方案:向函数传递一个额外的参数,即执行命令的公会id

@bot.command()
async def close_all(ctx, guild_id: int):
    # finding the guild according to the id passed
    guild = discord.utils.find(lambda g: g.id == guild_id, ctx.bot.guilds)
    for chan in guild.channels:
        await guild.channels.set_permissions(ctx.guild.default_role, send_messages=False)