Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “func”位置参数-Discord.py Bot_Python_Bots_Discord - Fatal编程技术网

Python “func”位置参数-Discord.py Bot

Python “func”位置参数-Discord.py Bot,python,bots,discord,Python,Bots,Discord,因此,我正在为Discord制作一个Python机器人,我收到一个奇怪的错误,在此之后我还有另一个问题: TypeError: decorator() missing 1 required positional argument: 'func' 我不确定是什么原因造成的。我为项目设置文件树的方式如下: ├───.idea ├───handlers │ └───userHandler.py └───src └───main.py 它将我指向main.py中的on_message方法

因此,我正在为Discord制作一个Python机器人,我收到一个奇怪的错误,在此之后我还有另一个问题:

TypeError: decorator() missing 1 required positional argument: 'func'
我不确定是什么原因造成的。我为项目设置文件树的方式如下:

├───.idea
├───handlers
│   └───userHandler.py
└───src
    └───main.py
它将我指向main.py中的on_message方法的第23行,这里是整个方法:

@client.event
async def on_message(message):
    if message.content.startswith(client.command_prefix + 'tstring'):
        client.send_message(message.channel, 'Hello, ' + str(userHandler.getuser()))
第23行是这里的一行:

client.send_messagemessage.channel,“你好,”+struserHandler.getuser

userHandler方法位于userHandler.py中,如上面的树所示:

@client.command
async def getuser(user, member: discord.Member = None):
    if member is None:
        member = user.message.author
        client.say('{0}'.format(member))
用户处理程序文件与主文件是分开的,因为我希望在它们自己的系统中处理所有基于用户的命令。我也将在它自己的处理程序中对命令使用相同的逻辑

所以我有两个问题:

是否可以/理想地将命令写在远离main.py的不同文件中,并且仍然在main中调用它们以供使用?如果是这样,我是否需要在每次从commandHandler.py调用main.py中的命令时编写@client.command,即使它已经在commandHandler.py文件中?还是有更好的方法?不确定这是否有足够的意义

我是不是在上面的错误中遗漏了一个论点?目前我对Discord.py API不太了解。如果没有,问题是什么?我真的希望我是个笨蛋


要回答第一个问题,可以在单独的文件中编写命令,并让它们仍然注册到main。在discord.py中,这称为使用齿轮。这是一个例子

假设您的代码就是这样的,那么您的代码有几个问题。首先,您必须始终等待协程函数才能使用它们->等待客户端。发送\u messageHello。。。其次,我不确定创建一个userhandler是否是一个好主意。特别是您试图考虑的方式,有更简单更直接的方法来获取用户对象


最后,请参考我在上面发布的链接,了解@client.command decorator是如何使用的

即使我使用的是wait,它仍然无法工作,如果我将wait放在client.send_message的前面,就会出现另一个错误。我猜只是因为糟糕的代码。不过我注意到,@client decorator我更了解。