Discord slash命令执行后如何发送无回复关联的普通消息
问题描述
使用以下Python代码实现slash命令功能时,机器人发送的所有消息都会作为对自身第一条发送消息的回复,需要实现直接发送普通独立消息:
@slash.slash(name='test', ...) async def test(ctx, txt: str): await ctx.send(f'1st: {txt}') await ctx.send(f'2nd: {txt}') await ctx.send(f'3nd: {txt}')
解决方案
原因说明
slash命令的上下文ctx.send默认会绑定交互响应逻辑,后续调用时会默认引用前一条机器人发送的消息,因此会出现叠楼回复自身的效果。
修复方案
可选择任意一种适用的方案实现需求:
方案1:显式关闭自动引用
在每个ctx.send方法中添加reference=None参数,直接禁用自动引用逻辑即可:
@slash.slash(name='test', ...) async def test(ctx, txt: str): await ctx.send(f'1st: {txt}', reference=None) await ctx.send(f'2nd: {txt}', reference=None) # 原代码3nd为笔误,修正为3rd await ctx.send(f'3rd: {txt}', reference=None)
该方案改动小,不需要调整原有交互逻辑。
方案2:直接调用频道发送接口
直接使用频道对象的send方法发送消息,完全脱离交互上下文的默认引用规则。注意slash命令要求3秒内响应,因此需要先调用defer做预响应避免交互超时:
@slash.slash(name='test', ...) async def test(ctx, txt: str): # 预响应交互,避免3秒超时报错 await ctx.defer() await ctx.channel.send(f'1st: {txt}') await ctx.channel.send(f'2nd: {txt}') await ctx.channel.send(f'3rd: {txt}')
该方案适合需要发送大量独立消息、不需要绑定交互响应的场景。
补充说明
- 上述方案对discord-py-slash-command、pycord、nextcord等主流支持slash命令的库都通用
- 如果不需要显示交互加载状态,优先选择方案1即可
内容的提问来源于stack exchange,提问作者ChesterWOV
相关产品推荐
相关产品推荐

