【问题标题】:TypeError: __init__() takes 1 positional argument but 2 were given discord.pyTypeError: __init__() 接受 1 个位置参数,但给定了 2 个 discord.py
【发布时间】:2020-08-13 07:28:02
【问题描述】:

所以我试图在我的机器人中创建一个设置命令,用户可以在其中选择他想要的内容。问题是我无法让它按我的意愿工作。

我有这个作为我的代码

# Function to write changes to file
def set_adminrole(guild: Guild, *, role: Role):
    with open("admins.json") as f:
        roles = json.load(f)

    roles[str(guild.id)] = role.id

    with open("admins.json", 'w') as f:
        json.dump(roles, f, indent=4)

# Actual command

-- Not important code --

await ctx.send(f"Now, mention the role you want it to be the admin role")
    role: Message = await bot.wait_for("message", check=check)
    actualrole: Role = Role(role)
    set_adminrole(ctx.message.guild, role=actualrole)
    await ctx.send(f"Admin role changed to {Role(role.content).mention}... Let's keep going")

当我提到将其设置为管理员角色的角色时,它会引发此错误:

Ignoring exception in command setup:
Traceback (most recent call last):
  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "D:/Ficheiros/Pastas/Tudo/Coding/Python/Projects/Server-Utils/bot.py", line 262, in start_setup
    actualrole: Role = Role(role)
TypeError: __init__() takes 1 positional argument but 2 were given

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

Traceback (most recent call last):
  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: __init__() takes 1 positional argument but 2 were given

感谢您的帮助

【问题讨论】:

  • 您的问题没有足够的信息。查看日志,您会发现一些内容告诉您导致错误的原因,可能是:...Server-Utils/bot.py", line 262, in start_setup
  • 请提供预期的minimal, reproducible example。由于您未能为 Role 类提供适当的支持,并且您发布的代码在很多方面都无法运行,因此我们无法为您做太多事情。

标签: python python-3.x discord discord.py


【解决方案1】:

因此您收到一条消息,然后尝试使用该消息创建角色对象。这没有任何意义。查看 Role 对象的构造函数,并正确调用它。

await ctx.send(f"Now, mention the role you want it to be the admin role")
    role: Message = await bot.wait_for("message", check=check)
    actualrole: Role = Role(role) #<- what are you doing here????
    set_adminrole(ctx.message.guild, role=actualrole)
    await ctx.send(f"Admin role changed to 

【讨论】:

  • 我不知道如何从消息中获取提到的角色...你能告诉我该怎么做吗?
  • 有多种方式。您可以查看消息内容并将其解析出来。您可以更改命令,使其成为命令的参数并使用角色转换器。尝试查看您首先收到的消息中包含的所有内容。使用调试器,或使用“dir”命令
  • 是的,想通了。 Tnx
  • 太棒了!随意投票或接受。祝你未来好运。
【解决方案2】:

我检索到消息的内容,即&lt;@&amp;123456789&gt;123456789 我提到的角色的ID。从字符串中删除了&lt;, @, &amp; and &gt;,只剩下了id。刚刚使用discord.utils.get(ctx.guild.roles, id=123456789) 和繁荣。谢谢大家的帮助

【讨论】:

  • 您应该查看角色转换器...。它旨在用于命令定义,但我在函数内部使用它们。还是有用的。
猜你喜欢
  • 1970-01-01
  • 2017-04-22
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2023-03-03
  • 2018-11-15
相关资源
最近更新 更多