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

discord.js中如何删除交互命令回复并停止斜杠命令自动回复?

如何停止Discord斜杠命令的自动回复

问题描述

我的机器人会对每个斜杠命令自动回复,推测是commandInteraction导致的。尝试使用interaction.deleteReply但没有效果,想知道如何停止斜杠命令的回复。以下是相关代码片段:

async execute(client, interaction, args, ee) {
    try {

        const guildData = await rrModel.findOne({
            guildId: interaction.guildId
        });

        if (!guildData?.roles) return interaction.reply({
            embeds: [new MessageEmbed()
                .setTitle(`${client.allEmojis.x} MenuRole System`)
                .setColor(ee.wrongcolor)
                .setFooter(ee.footertext, ee.footericon)
                .setDescription(`**There is no MenuRole Exist**`)
            ]
        });

解决方案

Discord要求斜杠命令交互必须在3秒内得到响应(回复或延迟回复),否则会触发超时错误。要实现“无可见回复”,可以通过以下方法处理:

  • 核心思路:先发送一个临时的延迟回复,再立刻删除它,既满足Discord的交互响应要求,又不会让用户看到任何内容。
  • 修改后的代码示例:
async execute(client, interaction, args, ee) {
    try {
        // 先发送仅自己可见的延迟回复,完成交互响应要求
        await interaction.deferReply({ ephemeral: true });
        // 立刻删除该延迟回复,避免显示
        await interaction.deleteReply();

        const guildData = await rrModel.findOne({ guildId: interaction.guildId });

        if (!guildData?.roles) {
            // 若仍需给用户提示,可使用仅用户可见的跟进回复(可选)
            await interaction.followUp({
                embeds: [new MessageEmbed()
                    .setTitle(`${client.allEmojis.x} 菜单角色系统`)
                    .setColor(ee.wrongcolor)
                    .setFooter(ee.footertext, ee.footericon)
                    .setDescription(`**当前没有菜单角色配置**`)
                ],
                ephemeral: true // 让提示仅触发命令的用户可见
            });
            return;
        }

        // 后续业务逻辑...
    } catch (error) {
        console.error(error);
    }
}
  • 关键说明:
    1. 直接使用interaction.deleteReply无效,通常是因为目标回复还未创建完成,而deferReply会先标记交互已响应,再删除就不会有问题。
    2. 如果完全不需要任何提示,只需保留deferReply+deleteReply的组合,去掉所有reply/followUp调用即可。
    3. 若需要给用户提示,建议使用ephemeral: true,让提示仅触发命令的用户可见,避免刷屏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 00:53:31