【问题标题】:How do I escape @everyone in discord.py?如何在 discord.py 中转义 @everyone?
【发布时间】:2021-05-07 06:49:35
【问题描述】:

我正在用 Python 开发一个 Discord 机器人,它根据用户输入输出文本。 我想避免用户说出@everyone(和@here)这会标记和惹恼所有人。

我尝试使用\@everyone,与@everyone 相比,它不会使文本本身变成蓝色,但它仍会触发 ping 并以黄色突出显示该行。这不仅会在我使用机器人发送消息时发生,而且在我直接使用 Discord 时也会发生。

【问题讨论】:

    标签: python escaping discord.py chatbot discord.py-rewrite


    【解决方案1】:

    我一直在使用的解决方案是在“@”之后插入一个zero-width space。这不会改变文本外观(“零宽度”),但额外的字符会阻止 ping。它具有 unicode 代码点 200b(十六进制):

    message_str = message_str.replace('@', '@​\u200b') 
    

    更明确地说,discord.py 库本身有 escape_mentions 用于此目的:

    message_str = discord.utils.escape_mentions(message_str)
    

    实现almost identically:

    def escape_mentions(text):
        return re.sub(r'@(everyone|here|[!&]?[0-9]{17,21})', '@\u200b\\1', text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多