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

如何用discord.py实现斜杠命令或其响应仅自身可见?

实现Discord斜杠命令/响应仅自己可见的方法

让命令响应仅自己可见

核心是使用ephemeral=True参数,在发送响应时添加该参数,就能让这条回复仅命令发起者可见,频道内其他成员看不到:

await interaction.response.send_message("只有你能看到这条内容", ephemeral=True)

除了send_message,send_modal、followup.send等其他响应方法也支持这个参数,同样能实现仅自己可见的效果。

限制斜杠命令仅自己可使用

如果要让某个斜杠命令只有你自己能触发,可以通过两种方式实现:

  • 自定义检查器(推荐)
    注册一个检查函数,判断发起命令的用户ID是否为你的ID,将其绑定到命令上:

    import discord
    from discord import app_commands
    
    # 替换成你的Discord用户ID
    MY_USER_ID = 123456789012345678
    
    @app_commands.check
    def is_self(interaction: discord.Interaction) -> bool:
        return interaction.user.id == MY_USER_ID
    
    # 绑定检查器到命令
    @tree.command(name="secret_cmd", description="仅本人可用的命令")
    @is_self
    async def secret_command(interaction: discord.Interaction):
        await interaction.response.send_message("只有我能看到这条响应", ephemeral=True)
    
  • 命令内直接判断
    在命令逻辑开头检查用户ID,不符合则返回提示(同样可以用ephemeral=True让提示仅对方可见):

    @tree.command(name="secret_cmd", description="仅本人可用的命令")
    async def secret_command(interaction: discord.Interaction):
        if interaction.user.id != MY_USER_ID:
            await interaction.response.send_message("你无权使用此命令", ephemeral=True)
            return
        await interaction.response.send_message("只有我能看到的内容", ephemeral=True)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 06:35:24