在消息内容中找不到消息作者的提及

时间:2021-08-01 01:44:58

标签: python python-3.x discord discord.py

@client.command(pass_context=True)
async def autorole_test(ctx):
  print("trying")
  keystring5 = "Here you go!"
  mee6 = discord.utils.get(ctx.guild.roles, name="Fresh Player")
  channel = client.get_channel(870023245892575282)
  messages = await ctx.channel.history().flatten()
  for msg in messages:
    if str(keystring5) in msg.content:
      if mee6 in ctx.message.author.roles:
        if ctx.message.author.mention in msg.content:
          print("yes")

这是我上面的代码^

代码在发送“autorole_test”(我的命令前缀设置为“”)后工作正常,直到它到达“if ctx.message.author.mention in msg.content:”

即使有明显符合在消息内容中提及消息作者的标准的消息。有没有其他方法可以做到这一点,实际上是可行的,还是我在这里做错了什么?

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

我自己解决的。 discord 解释提及的方式如下: <@!{user-id}>

ctx.message.author.mention 的解释方式是: <@{user-id}>

这两个显然不同步,所以我们需要一个变通方法。对于遇到问题的任何其他人,请按照以下简单代码进行操作:

    author = ctx.message.author.mention
    author2 = author[:2] + '!' + author[2:]
    if str(author2) in msg.content:

答案 1 :(得分:0)

您可以使用 Message.raw_mentions

<块引用>

返回与消息内容中 <@user_id> 语法匹配的用户 ID 数组的属性。

即使在私人消息上下文中,这也允许您接收提及用户的用户 ID。

所以代码应该是这样的:

    if author2.id in msg.raw_mentions: