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

如何让Discord.py机器人对斜杠命令进行私密回复?

让Discord机器人斜杠命令回复仅对发起用户可见

要实现机器人的回复仅命令发起用户可见,只需在发送消息时添加ephemeral=True参数——该参数会将消息标记为临时消息,仅命令执行者能查看。

修改后的完整代码如下:

import discord
from discord.ext import commands
from datetime import datetime
import asyncio

@commands.hybrid_command(name='ping', description='Fetch the bots ping')
async def ping(ctx):
    # 发送临时消息,仅发起用户可见
    embed = discord.Embed(title="Calculating ping...", color=discord.Color.blue())
    msg = await ctx.send(embed=embed, ephemeral=True)

    await asyncio.sleep(1)

    latency = round(ctx.bot.latency * 1000)
    embed.title = "Pong!"
    embed.description = f'Latency: {latency}ms'
    embed.color = discord.Color.green()
    embed.set_footer(text="Internal Audio")
    embed.timestamp = datetime.now()

    # 编辑临时消息,无需再次指定ephemeral
    await msg.edit(embed=embed)

关键改动说明

  • 在ctx.send()中加入ephemeral=True,直接将初始消息设为仅发起用户可见
  • 后续编辑消息时无需重复添加该参数,消息的临时属性会自动保留

临时消息特性:仅命令发起者可见、不会存入频道历史、其他用户无法对其进行互动操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:20:01