You need to enable JavaScript to run this app. Discord.js中如何将嵌入消息发送至用户私信而非频道
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js中如何将嵌入消息发送至用户私信而非频道

Discord Bot 帮助命令改发私信的调整方案

核心改动点

  • 替换原代码中指定公共频道发送的语句:原语句message.channel.send(embed)会把嵌入消息发到命令触发的服务器频道,要实现发私信的效果,只需将发送目标换为触发命令的用户对象message.author——该对象对应调用命令的用户,自带私人DM通道,直接调用send(embed)即可把内容投递到用户私信
  • 必须给发送动作增加错误兜底逻辑:如果用户关闭了接收机器人私信的权限,未捕获的发送异常会直接导致Bot进程崩溃,需要通过Promise的.catch()捕获失败场景,给出可落地的兜底提示
  • 补全原代码缺失的导入逻辑:你当前代码中使用new Discord.MessageEmbed()构造嵌入消息,需要确保文件顶部已经引入Discord.js核心库,避免出现Discord is not defined的运行报错

调整后的完整代码

// 补全缺失的库导入
const Discord = require('discord.js');

module.exports = {
  name: 'help',
  description: 'this is a help command!',
  execute(message, args){
      const embed = new Discord.MessageEmbed()
      .setTitle('List of Commands')
      .setDescription('This embed lists all of my commands.')
      .setColor('#ed2626')
      .addFields({
        name: '+help',
        value: 'Shows a list of commands',
        inline: true
      }, {
        name: '+ping',
        value: 'Show your insolence to krennic!',
        inline: true
      }, {
        name: 'Test Number 3',
        value: 'This is a example value'
      }, {
        name: 'Test Number 4',
        value: 'This is a example value'
      })
      .setTimestamp();

      // 替换原公共频道发送逻辑,改为给用户发私信,增加错误捕获
      message.author.send(embed)
        .catch(sendError => {
          // 私信发送失败时的兜底:在原频道提示用户检查隐私设置
          message.channel.send('我无法向你发送私信,请检查你的隐私权限设置后重试!');
        });
      console.log('Help command was ran!')
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.11 05:36:03