【问题标题】:discord.py bot works for message event or ping command, cannot do both simultaneouslydiscord.py bot 适用于消息事件或 ping 命令,不能同时进行
【发布时间】:2021-11-18 04:33:15
【问题描述】:

当我执行命令 !ping 时,我希望机器人说 pong,当有人在消息中说 kiddo 时,机器人会回复一个你好。但是当我注释掉第二个命令时,ping 命令有效,而当我取消注释 Kiddo 事件时,ping 命令停止工作,这是怎么回事?

@client.command()
async def ping(ctx):
    message = await ctx.send("Pong!")
    await message.edit(content = f"Pong! {round(client.latency*1000,2)} ms")

@client.event
async def on_message(message):
    if message.content.startswith("kiddo"):
        await message.channel.send("hello?")

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    一个简单的解决方法是将client.process_commands(message) 添加到您的代码中,如下所示:

    @client.command()
    async def ping(ctx):
        message = await ctx.send("Pong!")
        await message.edit(content = f"Pong! {round(client.latency*1000,2)} ms")
    
    @client.event
    async def on_message(message):
        if message.content.startswith("kiddo"):
            await message.channel.send("hello?")
        else:
            await client.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 2021-04-24
      • 2021-08-07
      • 2021-04-21
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 2021-12-04
      • 2021-09-22
      相关资源
      最近更新 更多