简体   繁体   English

在 Python 中编程 Discord 机器人 - 当消息仅包含一个字母时,如何让它识别?

[英]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.

相关问题 用 Python 编写 Discord 机器人 - 如何让机器人对自己的消息做出反应? - Programming a Discord bot in Python- How do I make the bot react to its own message? 用 Python 编写 Discord 机器人 - 如何让它每天在特定时间发送消息? - Programming a Discord bot in Python- How do I make it send a message every day at a certain time? 用 Python 编写 Discord 机器人 - 如何让机器人 ping @everyone? - Programming a Discord bot in Python- How do I make the bot ping @everyone? 在 Python 中编程 Discord 机器人 - 如何让机器人重复图像? - Programming a Discord bot in Python- How do I make the bot repeat an image? 在 Python 中编程 Discord 机器人 - 如何让机器人从 subreddit 发送随机文本组? - Programming a Discord bot in Python- How do I make the bot send a random group of text from a subreddit? 在 Python 中编程 Discord 机器人 - 如何使机器人将命令限制到某个服务器? - Programming a Discord bot in Python- How do I make the bot restrict commands to a certain server? 在 Python 中编程 Discord 机器人 - 在 on_raw_reaction_add 中,我如何判断消息是否有图像? - Programming a Discord bot in Python- In on_raw_reaction_add, how do I tell if the message has an image? 用 Python 编写 Discord 机器人 - 如何在嵌入中缩进? - Programming a Discord bot in Python- How do I indent in embeds? 在 Python 中编程 Discord 机器人 - 如何发出踢命令? - Programming a Discord bot in Python- How do I make a kick command? 在 Python 中编程 Discord 机器人 - 如何发出静音命令? - Programming a Discord bot in Python- How do I make a mute command?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM