How do I escape @everyone in discord.py?(如何在 discord.py 中转义 @everyone?)
问题描述
我正在 Python 中开发一个 Discord 机器人,它根据用户输入输出文本.我想避免用户让它说 @everyone
(和 @here
),这会标记和惹恼所有人.
I'm developing a Discord bot in Python which outputs text based on user input. I want to avoid users getting it to say @everyone
(and @here
) which would tag and annoy everyone.
我尝试使用 @everyone
与 @everyone
相比,它不会使文本本身变成蓝色,但它仍然会触发 ping 并突出显示该行黄色.这不仅会在我使用机器人发送消息时发生,而且在我直接使用 Discord 时也会发生.
I tried using @everyone
which in contrast to @everyone
does not make the text itself blue, but it still triggers a ping and highlights the line in yellow. This does not only happen when I send a message with the bot but also if I use Discord directly.
推荐答案
我一直使用的解决方案是插入一个 零宽度空格在@"之后.这不会改变文本外观(零宽度"),但额外的字符会阻止 ping.它具有 unicode 代码点 200b
(十六进制):
The solution I've been using is to insert a zero-width space after the '@'. This will not change the text appearance ('zero-width') but the extra character prevents the ping. It has unicode codepoint 200b
(in hex):
message_str = message_str.replace('@', '@u200b')
更明确地说,discord.py 库本身有 escape_mentions
用于此目的:
More explicitly, the discord.py library itself has escape_mentions
for that purpose:
message_str = discord.utils.escape_mentions(message_str)
实现几乎相同:
def escape_mentions(text):
return re.sub(r'@(everyone|here|[!&]?[0-9]{17,21})', '@u200b\1', text)
这篇关于如何在 discord.py 中转义 @everyone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 discord.py 中转义 @everyone?


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01