You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让discord.py开发的Discord机器人发送仅触发者可见的消息

discord.py 实现仅指令触发者可见的响应消息

你要实现的是Discord的*临时消息(Ephemeral Message)*效果,效果示意如下:
效果示意图
该能力仅支持斜杠指令(Slash Command)响应调用,普通前缀消息指令无法实现。discord.py v2.0及以上版本原生支持该能力,实现方法如下:

基础实现代码

import discord
from discord import app_commands

# 配置机器人权限
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

# 定义斜杠指令
@tree.command(name="private_response", description="返回仅你可见的消息")
async def private_response(interaction: discord.Interaction):
    # 新增ephemeral=True参数即可实现仅触发者可见
    await interaction.response.send_message("这条消息仅你可见,其他用户无法查看", ephemeral=True)

@client.event
async def on_ready():
    # 同步斜杠指令到Discord,测试时可指定单服务器降低同步延迟
    await tree.sync()
    print(f"机器人登录成功:{client.user}")

client.run("替换为你的机器人Token")

进阶用法

如果已经回复了公开响应,需要额外补发仅触发者可见的消息,可使用followup接口:

# 先发送公开响应
await interaction.response.send_message("这是所有人可见的公开消息")
# 补发仅触发者可见的临时消息
await interaction.followup.send("这是仅你可见的补充消息", ephemeral=True)

注意事项

  • 临时消息不会永久存储,用户刷新Discord客户端后消息会消失
  • 除触发指令的用户外,包括服务器管理员在内的所有其他用户都无法查看该消息
  • 必须使用斜杠指令才能触发临时消息,普通!、/开头的前缀消息指令不支持该能力

内容的提问来源于stack exchange,提问作者user14696101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:06:03