問題描述
我正在 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!