首页 > 解决方案 > 用 Python 编写 Discord 机器人——当消息只包含一个字母时,如何让它识别?

问题描述

我正在为我的机器人做出反应。每当有人只说字母“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'))

但是,只要有人说以“f”开头的内容,此代码就会发送图像。每当有人说仅包含字母“f”的内容时,我希望它发送图像。不知道怎么做,有什么提示吗?

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


这是基本的python

>>> "foo".starswith("f")
True
>>> "foo" == "f"
False

on_message您可以在事件中使用相同的原则

if message.content.lower() == "f":
    # Send the image

推荐阅读