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

时间:2020-06-04 19:13:21

标签: python python-requests bots discord discord.py

当字符串中包含cutstom服务器表情符号时,如何使我的机器人响应?
我的代码当前如下所示:

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

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

1 个答案:

答案 0 :(得分:1)

使用自定义表情符号时,它具有唯一的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(),这使命令不区分大小写,因此无论用户键入HelLoHELLO还是hElLo


参考: