TypeError:__init __接受1个位置参数,但给了2个discord.py

问题描述

所以我试图在我的机器人中创建一个设置命令,用户可以在其中选择他想要的东西。问题是我无法按自己的意愿运行它。

我以此为密码

# 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

感谢您的帮助

解决方法

因此您会收到一条消息,然后尝试使用该消息创建一个Role对象。这没有道理。查看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 
,

我检索了消息的内容,该内容将是<@&123456789>是我提到的角色的ID。从字符串中删除了123456789,只留下了ID。刚刚使用过<,@,& and >和繁荣时期。谢谢大家的帮助

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...