You need to enable JavaScript to run this app. Discord.js Node.js机器人如何获取用户私信(Dm)回复内容
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js Node.js机器人如何获取用户私信(Dm)回复内容

问题核心排查

你的代码存在三个直接导致无法获取私信输入的问题:

  • 统一的前缀判断拦截了所有私信:原代码在消息事件开头的判断if(!message.content.startsWith(prefix) || message.author.bot ) return;,会直接拦截所有没有以a!开头的消息(包括用户发给机器人的私信内容),根本不会进入后续处理逻辑。
  • 未定义的同步调用:你想要获取用户在私信里的回复,但当前代码直接调用了不存在的Response()函数,既没有监听异步的私信输入,也没有定义函数逻辑,自然拿不到任何结果。
  • 缺少discord.js要求的权限配置:如果使用discord.js v14及以上版本,必须手动开启消息读取的权限开关,否则根本收不到任何消息的文本内容(包括频道消息和私信)。
修正后的完整可运行代码
const { Client, Intents } = require('discord.js');
// 初始化客户端时指定必须的权限标记
const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS, // 服务器内事件处理需要
        Intents.FLAGS.GUILD_MESSAGES, // 服务器频道消息处理需要
        Intents.FLAGS.DIRECT_MESSAGES, // 接收用户私信必须开启
        Intents.FLAGS.MESSAGE_CONTENT // 读取消息文本必须开启(v14要求的特权权限)
    ]
});

const prefix = 'a!';
// 修正原代码里的笔误:命令列表和实际前缀保持一致
const Commands = 'a!help a!version a!post';
const MarketplaceCommnads = "Looking for Devs For Hire Looking for Creations Sell Creations";
const Version = '1.0.0';

client.once('ready', () => {
    console.log('Bot is online!');
});

// 以下Embed定义和你原代码一致,未做改动
const helpEmbed = new Discord.MessageEmbed()
    .setTitle('Help')
    .setColor('#ff0000')
    .addField('Command List', Commands);
const postEmbed = new Discord.MessageEmbed()
    .setTitle("Post")
    .setColor("#ff0000")
    .addField('The bot has dmed you');
const dmEmbed = new Discord.MessageEmbed()
    .setTitle("New Post")
    .setColor("#ff0000")
    .addField("Create a new post", MarketplaceCommnads);
const lookingfordevsEmbed = new Discord.MessageEmbed()
    .setTitle("Looking for Devs")
    .setColor("#ff0000")
    .addField("Post everything here (What you're looking for, payment, etc): ");
const versionEmbed = new Discord.MessageEmbed()
    .setTitle('Version')
    .setColor('#ff0000')
    .addField('Version', Version)
    .setColor('#ff0000');

// 新增:存储等待回复的用户队列,标记正在走发帖流程的用户,避免普通私信误触发
const pendingReplyUsers = new Set();

// v14及以上版本用messageCreate事件,旧版本改回message即可
client.on('messageCreate', (message) => {
    // 先过滤机器人自己发的所有消息
    if (message.author.bot) return;

    // 单独处理用户发送的私信
    if (message.channel.type === 'DM') {
        // 只处理刚发过a!post、正在等待分类选择的用户
        if (pendingReplyUsers.has(message.author.id)) {
            // 直接获取用户私信的输入内容
            const userInput = message.content.trim();
            // 处理完后移除等待标记
            pendingReplyUsers.delete(message.author.id);

            // 匹配用户输入的分类
            if (userInput === 'Looking for Devs') {
                message.author.send(lookingfordevsEmbed);
                // 后续可以在这里新增逻辑,继续监听用户发送的帖子具体内容
            } else {
                message.author.send('输入无效,请重新发送a!post选择正确的分类');
            }
        }
        return;
    }

    // 仅对服务器频道内的消息判断命令前缀
    if (!message.content.startsWith(prefix)) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'help') {
        message.channel.send(helpEmbed);
    }
    if (command === 'version') {
        message.channel.send(versionEmbed);
    }
    if (command === 'post') {
        // 把用户加入等待回复队列
        pendingReplyUsers.add(message.author.id);
        message.channel.send(postEmbed);
        // 给用户发送私信选择分类,同时处理私信发送失败的异常
        message.author.send(dmEmbed).catch(() => {
            pendingReplyUsers.delete(message.author.id);
            message.reply('无法给你发送私信,请开启与机器人的私信权限后重试');
        });
    }
});

// 替换为你自己的机器人Token即可登录运行
client.login('你的机器人Token');
关键改动说明
  • 拆分了私信和频道消息的处理逻辑:只有服务器频道的消息才需要判断a!前缀,私信消息单独处理,不会被前缀规则拦截。
  • 用队列标记需要接收私信的用户:只有刚发过a!post的用户的私信才会被读取,避免普通的私人消息误触发逻辑。
  • 配置了所有需要的权限标记:解决了discord.js v14及以上版本无法读取消息内容的问题。
  • 增加了异常处理:如果用户关闭了和机器人的私信,会自动清理等待标记,不会出现逻辑卡死的情况。

补充注意:使用discord.js v14及以上版本时,除了代码里配置权限标记,还需要在Discord开发者后台的机器人设置页,手动开启Message Content Intent的开关,否则会报权限错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.27 05:06:04