复刻Carl Bot的Echo命令:如何隐藏用户斜杠命令仅显示机器人消息
解决Discord.js中隐藏斜杠命令系统消息的问题
要实现仅显示机器人回复、隐藏用户执行斜杠命令的系统消息(即“XX使用了/echo”),可以通过临时回复替换系统消息,再发送公开回复并删除临时回复的方式实现。以下是修改后的代码:
const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('echo') .setDescription('Replies with your input!') .addStringOption(option => option.setName('input') .setDescription('The input to echo back') .setRequired(false)), async execute(interaction) { const input = interaction.options.getString('input') || '没有输入内容!'; // 发送仅用户可见的临时回复,替换默认的系统命令消息 await interaction.deferReply({ ephemeral: true }); // 向频道发送公开的回复内容,所有人可见 await interaction.channel.send(input); // 删除临时回复,彻底移除命令痕迹 await interaction.deleteReply(); }, };
代码说明:
interaction.deferReply({ ephemeral: true }):生成一条仅执行命令的用户能看到的“正在回复”提示,这会覆盖Discord默认显示的用户执行命令的系统消息。interaction.channel.send(input):直接在当前频道发送公开的回复内容,所有频道成员都能查看。interaction.deleteReply():删除之前的临时回复,最终频道内只会保留机器人的公开回复。
如果想要模拟Carl Bot那种“以用户身份发送消息”的效果,可以改用webhook发送:
// 替换上述代码中的channel.send部分 const webhook = await interaction.channel.createWebhook({ name: interaction.user.username, avatar: interaction.user.displayAvatarURL(), }); await webhook.send(input); await webhook.delete(); // 使用后立即删除临时webhook
内容的提问来源于stack exchange,提问作者Aloxenツ
相关产品推荐
相关产品推荐

