【问题标题】:Keep getting "missing 1 required positional argument" but the argument is given不断收到“缺少 1 个必需的位置参数”,但给出了参数
【发布时间】:2021-05-24 07:59:43
【问题描述】:

我的 discord 机器人有问题。 (discord.py)

这是我的代码:

@bot.command()
async def mute(ctx):
    muteRole = discord.Guild.get_role(845612659940524032)
    if(message.author.kick_members == True):
        return

    else:
        await ctx.send('You don\'t have the permission for that')

在我看来一切都是正确的。但是当我测试命令时,我总是得到这个错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: get_role() missing 1 required positional argument: 'role_id'

有人可以帮我吗?

【问题讨论】:

  • 我的猜测是您正在尝试使用 Guild 类的非静态方法,并且您传递给 get_role 的数字被解释为 self 参数,这将解释您的异常。你不应该从类 Guild 实例化吗?

标签: python discord discord.py


【解决方案1】:

我认为您还没有完全理解它是如何工作的。 discord.Guild 是一个类,不是一个实例,您正在调用它的函数 get_role()。那就是你的错误来自哪里。要解决此问题,请改用 ctx.guild 实例。

muteRole = ctx.guild.get_role(845612659940524032)

message.author.kick_members 也不能在这里工作。首先,因为message 没有在任何地方定义,请改用ctx.message。此外,ctx.message.author 没有属性kick_members,可以尝试使用装饰器来代替

@bot.command()
@has_permissions(kick_members=True)
async def mute(ctx):
    muteRole = ctx.guild.get_role(845612659940524032)

【讨论】:

  • 感谢您的快速回答!但是当我尝试你的代码时,我只是得到另一个错误代码: NameError: name 'has_permissions' is not defined 对不起我的问题我不习惯这个重写的东西....
  • 然后您需要导入from discord.ext.commands import has_permissions, MissingPermissions
猜你喜欢
  • 2014-09-13
  • 1970-01-01
  • 2019-10-10
  • 2017-03-27
  • 2019-11-12
  • 2019-12-23
  • 2021-02-01
  • 2020-11-15
相关资源
最近更新 更多