【问题标题】:event() missing 1 required positional argument: 'coro' error discord.pyevent() 缺少 1 个必需的位置参数:“coro”错误 discord.py
【发布时间】:2021-03-10 06:49:57
【问题描述】:

我正在为新服务器制作一个机器人,但我不断收到此错误,我对 python 和一般编码相当陌生,因此感谢任何帮助。

event() 缺少 1 个必需的位置参数:'coro' 错误 discord.py

当我发帖时,我的意思是说我的帖子主要是代码,所以我只是随机输入内容,直到它允许我发布为止。我有一只很酷的狗,他很可爱,而且他是有史以来最优秀的男孩:)

下面是我收到错误的代码

@client.event
 

    
async def on_ready():
    print('We have logged in as {0.user}'
    .format(client))

下面是我的完整代码

    import discord


client = discord.Client

@client.event
async def on_ready():
    print('We have logged in as {0.user}'
    .format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>Hello Sylas'):
        await message.channel.send('Hello there!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>Is Dobbie cool?'):
        await message.channel.send('Yes, he is the goodest boy!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    if message.content.startswith('>Hey Sylas'):
        if str(message.author) in ["Trax#8949"]:
            await message.channel.send('Hey Trax!')



@client.event
async def on_message(message):  
    if message.author == client.user:
        return        
async def on_message(message): 
    if message.content.startswith('>'):
     async def ban(ctx, member : discord.Member, *, reason = None):
      await member.ban(reason = reason)





 













client.run('my token :)') 

【问题讨论】:

  • 欢迎来到 Stack Overflow!为了尽快获得高质量的回复,请不要为了通过问题内容要求而输入乱码。虽然你的狗看起来不错,但它根本没有帮助,详细描述你的问题会更有用。这样,也没有必要发布整个代码,因为 Stack Overflow 需要最少的代码来重现问题。
  • 你有很多 on_message 函数..????也许您应该首先将它们全部放在功能上,然后看看它是否有效
  • 尝试客户端 = discord.Client()
  • 参考@Dominik 帖子末尾链接的帖子,我强烈建议使用 discord.ext.commands 机器人而不是 discord.client()。我现在将它用于我的机器人,它使事情变得非常简单。很明显,您需要一堆不同的 on_messages 来拆分您的代码。您可以使用链接帖子中提到的听众来做到这一点。如果您希望命令仍然有效(如果有的话),请确保将 await bot.process_commands(message) 放在原始 on_message 的末尾。

标签: python discord.py


【解决方案1】:

多个on_message 事件将不起作用

您一次只能有一个事件,因此您必须合并这些事件。这看起来像这样:

client = discord.Client() # Added brackets

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>Hey Sylas'):
        if str(message.author) in ["Trax#8949"]:
            await message.channel.send('Hey Trax!') # First event

    [Shortened]

    if message.content.startswith('>ban'):
      await member.ban(reason = reason) # Last event

您可以拥有多个client.command()“函数”,但这不计入事件。

一篇解释得很好的帖子:Why multiple on_message events will not work

【讨论】:

    【解决方案2】:

    试试这个:

    import discord
    
    
    client = discord.Client
    
    @client.event
    async def on_ready():
        print('We have logged in as {0.user}'
        .format(client))
    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
    
        elif message.content.startswith('>Hello Sylas'):
            await message.channel.send('Hello there!')
    
        elif message.content.startswith('>Is Dobbie cool?'):
            await message.channel.send('Yes, he is the goodest boy!')
    
        elif message.content.startswith('>Hey Sylas'):
            if str(message.author) == "Trax#8949":
                await message.channel.send('Hey Trax!')
    
        elif message.content.startswith('>ban'): # You should restrict that to specific roles
            await member.ban(reason = reason)
    
    
    client.run('now its my token :)')
    

    我做了什么:

    • 将所有on_message 放入一个 on_message
    • 更改为">ban",而不是在其中使用未调用的函数
    • "in ['Trax']" 更改为"== 'Trax'"
    • 已添加elifs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2022-06-11
      • 2020-10-12
      • 2020-08-13
      • 1970-01-01
      • 2014-09-13
      相关资源
      最近更新 更多