【问题标题】:When certain keyword is entered bot will not return message when using @client.event discord当使用@client.event discord 时输入某些关键字时,bot 不会返回消息
【发布时间】:2021-08-10 05:56:46
【问题描述】:

所以,我正在编写一个不和谐的机器人。我试图让它响应一个关键词,例如“Pong!”由于某种原因,这不起作用。我已经尝试了一个小时的解决方案,我累了。如果有人可以提供帮助,那就太好了。我只想让机器人在有人说 ping 作为关键字时说 pong。 这是代码。

# if anyone says keywords
@client.event
async def on_message(message):
    if "Ping" in message.content:
        await ctx.send('Pong!')

谢谢
-杰克

【问题讨论】:

  • 您不能在message 事件中使用ctx。将其更改为await message.channel.send("Pong!")

标签: python discord discord.py


【解决方案1】:

错误非常清楚。

到底哪里出错了?

问题出在这句话中:

await ctx.send('Pong!')

ctx实际上是你使用discord库的Command API时传递的上下文参数。但是当您使用简单的Client 时,您真的可以使用它。

在这一行:

async def on_message(message):

参数是message,我们将使用它来发送消息。

所以很简单。

只要把错误语句改成上面提到的:

await message.channel.send("Pong!")

这会解决你的问题!

谢谢! :D

【讨论】:

    猜你喜欢
    • 2020-12-24
    • 2019-01-16
    • 2021-08-12
    • 2020-07-31
    • 2021-12-07
    • 2021-08-29
    • 2021-05-15
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多