【问题标题】:Message author's mention isn't being found inside of message content在消息内容中找不到消息作者的提及
【发布时间】:2021-08-01 01:44:58
【问题描述】:
@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:”

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

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    你可以使用Message.raw_mentions

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

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

    所以代码应该是这样的:

        if author2.id in msg.raw_mentions:
    

    【讨论】:

    • 谢谢你,但我已经想通了,无论如何我都会接受你的回答!此外,机器人发送的提及是 ,普通用户发送的提及是 如果您正在寻找一种方法来计算寻找普通用户提及的消息,请按照我的方法。
    • 我看到了你的方法,这样做是正确的方法,因为你可以比较多个用户而不是为每个用户更改字符串if (user1.id or user2.id) in msg.raw_mentions:
    【解决方案2】:

    我自己解决了。不和谐解释提及的方式如下:

    ctx.message.author.mention 的解释方式是:

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

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

    【讨论】:

    • 您也可以简单地if ctx.author.mentioned_in(msg),这是一种更清洁、更好的方式。
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2021-05-12
    • 2021-09-14
    • 1970-01-01
    • 2018-03-20
    • 2018-03-18
    • 2021-07-23
    相关资源
    最近更新 更多