【问题标题】:In which way can I make an Auto-React feature for a discord bot that I'm writing in the discord.py API?我可以通过哪种方式为我在 discord.py API 中编写的不和谐机器人制作自动反应功能?
【发布时间】:2021-05-30 00:13:05
【问题描述】:

基本上,我希望我的 discord 机器人对包含关键字的消息做出反应。我尝试了使用 if 语句的教程,但这两种方法都不起作用。

   async def on_message(message):
      if "react to me" in message.content.lower():
        await client.react(message)```

【问题讨论】:

  • 这里是添加reactions到消息的文档。尝试关注文档,最近更新的教程,您似乎指的是过时的教程。

标签: python discord discord.py


【解决方案1】:

这是一个简单的功能,如果你想让它使用服务器上的随机表情符号,你必须获取表情符号 id,但你可以这样做。

@client.event
async def on_message(message):
    emoji = client.get_emoji(#insert custom emoji id)
    if re.match("react to me", message.content.lower):
        await message.add_reaction(emoji)
    await client.process_commands(message)

这将匹配任何只说“对我做出反应”的消息

编辑:

此方法不能使用默认表情符号(例如竖起大拇指),它使用服务器中的自定义表情符号,您可以使用\:put the name of the emoji in between these signs:获取类似<:name:id>的内容

示例:

所以表情符号的 id(您将在 add_reaction() 中放入的只是 672251623422296117

如果您想使用通用表情符号(默认),您必须找到它的 unicode 并将其作为字符串放入(在“”内)。 示例:? 的 unicode 是 \U0001F3D3,因此您将其放入函数中。

您可以在此处找到默认表情符号的 unicode:https://emojipedia.org/unicode-9.0/

看起来像这样

这意味着该表情符号的 unicode 是 \U0001F923(将 U+ 替换为 \U000,使其成为 U+1F923 = \U0001F923

在函数末尾添加await client.process_commands(message),以允许其他函数工作

【讨论】:

  • 我收到一条错误消息File "C:\Users\anyuser\AppData\Local\Programs\Python\Python37\lib\re.py", line 173, in match return _compile(pattern, flags).match(string) TypeError: expected string or bytes-like object
  • 是因为没找到emoji id,把id改成服务器中存在的emoji。这是我的错误,我应该知道,但默认表情符号没有 ID
  • 由于某种原因,我仍然遇到同样的错误。这是代码:@client.event async def on_message(message): emoji = client.get_emoji("<:redheart:815750746619379754>") if re.match("react to me", message.content.lower): await message.add_reaction(emoji)
  • 您只是假设使用表情符号 id 的数字,我也编辑了答案来解释如何将其与默认表情符号一起使用。
  • 很抱歉,如果我打扰了你,但我仍然得到同样的错误。这是代码pastebin.com/5D3Drwv2
猜你喜欢
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 2017-12-27
  • 2021-11-04
  • 2018-11-17
  • 1970-01-01
  • 2021-07-20
  • 2021-08-03
相关资源
最近更新 更多