在 discord.py 中,若使用 “ 格式发送消息却未触发实际通知(即“幽灵提及”),是因为默认禁用了角色提及权限;需显式配置 `allowed_mentions` 参数启用角色提及。
Discord 的安全机制默认禁止机器人随意提及角色或用户,以防止滥用和骚扰。即使你在消息中写入了 这样的格式,若未明确授权提及行为,Discord 将仅渲染为纯文本(即“幽灵提及”),不会触发通知、高亮或声音提醒。
要解决该问题,必须在调用 interaction.response.send_message() 时传入 allowed_mentions 参数,并指定允许提及的角色:
@client.tree.command(name="duty", description="ping @SASP wbijajcie na służbę (only adm)")
@discord.app_commands.checks.has_permissions(administrator=True)
async def nagana(interaction: discord.Interaction):
await interaction.response.send_message(
f"<@&1064986634963140628>, Zapraszam na służbę",
allowed_mentions=discord.AllowedMentions(roles=True),
)
✅ 关键点说明: