如何在discord.py中发送ephemeral临时消息
你现有代码不生效的核心原因是:ephemeral 是Discord交互体系下的专属属性,仅能在响应用户触发的交互(斜杠命令、按钮点击、选择菜单操作等)时使用,普通消息的发送、编辑接口均不支持该参数,额外添加也不会生效。
正确使用前提
必须基于交互对象 discord.Interaction 调用响应方法,不能使用普通 message 相关的发送、编辑方法。
常见使用场景示例
- 直接响应交互发送临时消息
# 斜杠命令场景示例 @discord.app_commands.command(name="cancel_review", description="取消提交审核") async def cancel_review(interaction: discord.Interaction): # 直接在send_message中加ephemeral=True即可,消息仅触发交互的用户可见 await interaction.response.send_message( content="Review submission cancelled.", embed=None, ephemeral=True )
- 延迟响应后发送临时消息
如果你的逻辑处理耗时超过3秒,需要先调用defer方法延迟响应,注意defer时也需要指定ephemeral=True:
@discord.app_commands.command(name="submit_review", description="提交审核") async def submit_review(interaction: discord.Interaction): # 先延迟响应,指定ephemeral属性 await interaction.response.defer(ephemeral=True, thinking=True) # 执行你的审核逻辑 # ... # 后续用followup发送临时消息 await interaction.followup.send( content="Review submission cancelled.", embed=None, ephemeral=True )
- 编辑已发送的临时消息
如果需要修改已经发送的临时消息,需要使用交互对象的编辑方法,不能调用普通message.edit:
# 方式1:直接编辑原始响应 await interaction.edit_original_response(content="修改后的临时消息内容") # 方式2:拿到消息对象后编辑,仅对ephemeral消息生效 msg = await interaction.original_response() await msg.edit(content="修改后的临时消息内容")
额外注意
普通前缀命令(如!cmd这类通过消息触发的命令)没有交互上下文,无法发送ephemeral临时消息。
内容的提问来源于stack exchange,提问作者Iced Dev
相关产品推荐
相关产品推荐

