Discord中@everyone无法提及?ctx.response.send_message触发失败
Discord机器人@everyone提及失效问题解决
问题描述
我开发了一个向Discord推送新闻的机器人功能,需要@全体成员(@everyone)。使用ctx.send(...)时可正常触发全体提及,但使用ctx.response.send_message(...)时@everyone无法生效。我希望移除ctx.response的使用,尝试过ctx.defer()、ctx.response.defer(),也尝试通过response发送消息,但@everyone始终无法正常工作。
相关代码如下:
@commands.slash_command(name="news",description="news") async def news(self, ctx: discord.ApplicationContext, title, news): if ctx.author.top_role.name not in settings['high_admin_role']: await ctx.response.send_message("not permission",ephemeral=True) return else: news = ''.join(news).replace(';', '\n') embed = discord.Embed(title=f"""{title}""", description=f"""{news} От: {ctx.author.mention}""", color=discord.Color.from_rgb(199, 135, 24)) await ctx.send('||@everyone||', embed=embed)
解决方案
核心原因
在Discord的slash命令交互逻辑里,ctx.response.send_message()属于交互响应消息,这类消息默认限制触发@everyone/@here提及(哪怕机器人拥有对应权限);而ctx.send()是发送独立的普通频道消息,不受这个限制。
调整方案
要完全移除ctx.response的使用,需先对slash命令做延迟响应避免超时,同时用followup处理权限提示,保留ctx.send发送带提及的新闻:
- 命令开头立即调用
await ctx.defer(),告知Discord服务器正在处理请求 - 权限校验失败时,用
ctx.followup.send()发送临时提示 - 保留原
ctx.send()逻辑发送带@everyone的新闻内容
修改后的代码:
@commands.slash_command(name="news", description="推送新闻") async def news(self, ctx: discord.ApplicationContext, title, news): # 延迟响应,避免交互超时 await ctx.defer() if ctx.author.top_role.name not in settings['high_admin_role']: # 发送临时权限提示 await ctx.followup.send("无权限", ephemeral=True) return news = ''.join(news).replace(';', '\n') embed = discord.Embed( title=title, description=f"""{news}\n\nОт: {ctx.author.mention}""", color=discord.Color.from_rgb(199, 135, 24) ) # 发送带全体提及的新闻 await ctx.send('@everyone', embed=embed)
注意事项
- 确保机器人在目标服务器拥有提及@everyone的权限(服务器设置→角色→机器人角色中开启“提及@everyone、@here和所有角色”权限)
ctx.defer()必须在收到交互后的3秒内调用,否则Discord会判定交互超时- 若无需隐藏提及,直接写
@everyone即可,不用隐藏内容包裹||@everyone||
内容的提问来源于stack exchange,提问作者MrDoberman
相关产品推荐
相关产品推荐

