[英]Programming a Discord bot in Python- How do I make it recognize when a message contains only one letter?
I'm working on a reaction for my bot.我正在为我的机器人做出反应。 Whenever someone says only the letter "f", I want it to send an image:每当有人只说字母“f”时,我希望它发送一张图片:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('f') or message.content.startswith('F'):
await message.channel.send(file=discord.File('f.png'))
However, this code makes it send the image whenever someone says something that starts with "f".但是,只要有人说以“f”开头的内容,此代码就会发送图像。 I want it to send the image whenever someone says something that contains only the letter "f".每当有人说仅包含字母“f”的内容时,我希望它发送图像。 Not sure how to do that, any tips?不知道怎么做,有什么提示吗?
This is basic python这是基本的 python
>>> "foo".starswith("f")
True
>>> "foo" == "f"
False
You can use the same principle in the on_message
event您可以在on_message
事件中使用相同的原理
if message.content.lower() == "f":
# Send the image
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.