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

Discord.py:如何在服务器发送仅命令执行者可见的消息?

解决发送仅命令执行者可见消息的问题

你使用ephemeral=True参数无效,核心原因是:传统前缀命令的ctx.send()方法不支持ephemeral参数,这个参数是Discord交互类命令(比如斜杠命令)的专属参数。

下面提供两种可行的解决方案:

方案1:给执行者发送私信

直接通过用户对象发送私信,只有执行者能看到:

@bot.command(description="says hello... yeah")
async def hello(ctx):
    await ctx.author.send(f"hello {ctx.author.mention}!!!")
    # 可选:在服务器频道短暂提示消息已发送到私信
    await ctx.send("已给你发送私信~", delete_after=5)

方案2:在服务器频道发送带权限限制的消息

通过修改消息的权限覆盖,让只有执行者能查看这条消息:

import discord

@bot.command(description="says hello... yeah")
async def hello(ctx):
    embed = discord.Embed(description=f"hello {ctx.author.mention}!!!")
    # 先发送消息
    msg = await ctx.send(embed=embed)
    # 移除服务器默认角色的查看权限
    await msg.channel.set_permissions(ctx.guild.default_role, view_channel=False)
    # 给执行者单独开放查看权限
    await msg.channel.set_permissions(ctx.author, view_channel=True)

更推荐:改用斜杠命令

如果你的机器人支持,改用斜杠命令可以原生使用ephemeral参数,代码更简洁:

@bot.tree.command(name="hello", description="says hello... yeah")
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f"hello {interaction.user.mention}!!!", ephemeral=True)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:25:16