python - 如何让 Discord 机器人在 on_message 中使用自定义表情符号响应消息

标签 python python-requests bots discord discord.py

当字符串中包含剪切服务器表情符号时,我如何才能让我的机器人做出响应?
我的代码目前如下所示:

if message.content.startswith(":emoji: Hello"):
      await client.get_channel(123456789).send("Hello to you, too!")

如果我在 Discord 中发布表情符号和句子,该命令将不会触发。我需要改变什么?

最佳答案

使用自定义表情符号时,它有一个唯一的 ID,这是消息内容中显示的内容,而不仅仅是表情符号的名称。您可以通过几种不同的方式访问它,但最简单的可能是 \:emoji: ,您可以在这里看到它的工作原理:

enter image description here

enter image description here

enter image description here


最后一部分是消息的内容将包含的内容。因此,就我而言,正确的陈述如下:

if message.content.lower().startswith("<:statue:709925980029845565> hello"):
    await message.channel.send("Hello to you too!") # sends to the same channel the message came from

您还可以在不使用 ID 的情况下检索表情符号:

emoji = discord.utils.get(message.guild.emojis, name="statue")
if message.content.lower().startswith(f"{emoji} hello"):
    await message.channel.send("Hello to you too!")

请注意,我还使用了 .lower()这使得命令不区分大小写,因此无论用户键入 HelLo 都会起作用。 , HELLOhElLo


引用文献:

关于python - 如何让 Discord 机器人在 on_message 中使用自定义表情符号响应消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62202435/

相关文章:

python - 文本 Python 中的重复短语

python - cgi.FieldStorage 不从 requests.post 读取 json 数据

python - 使用 namespace 和 Python (lxml) 从 XML 中获取 XML 属性

python - 属性错误 : read in Python

Python 仅 append 唯一名称而不是重复名称

javascript - Telegraf:无法使用 Markdown 样式回复 inlineButtons

c# - 如何将按钮添加到 HeroCard 并在单击时选择值

python - 尝试使用 Python Paramiko 建立 SSH 连接时出现 "getaddrinfo"错误

python - 使用inspect_container在python上检查停止的docker容器

python - 为什么重写 __iter__ 会抛出 RecursionError?