【问题标题】:How do I make a Discord bot respond to a message with a custom emoji in on_message如何让 Discord 机器人在 on_message 中使用自定义表情符号响应消息
【发布时间】:2020-09-23 21:33:46
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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


    最后一部分是消息内容将包含的内容。所以在我的例子中,正确的陈述应该是这样的:

    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,它都可以工作


    参考资料:

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2021-03-21
      • 2018-08-05
      • 2019-01-29
      • 2021-07-19
      • 2022-08-22
      • 2020-06-29
      • 2021-05-24
      • 2019-11-23
      相关资源
      最近更新 更多