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

Discord.js监听DM频道messageCreate事件失败求助

解决messageCreate事件无法触发的问题

1. 修复语法错误

你的代码最后一行缺少闭合括号,会直接导致代码运行失败,修正为:

Client.on('messageCreate', async message => {
    console.log('messageCreate event');
});

2. 正确配置Intents(discord.js v14+)

v14版本必须使用GatewayIntentBits枚举值,不能用字符串,同时要在Discord开发者后台的机器人页面开启Message Content Intent特权意图:

const { GatewayIntentBits } = require('discord.js');

const client = new Discord.Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.MessageContent // 必须开启后台特权意图
    ],
    // ...其他配置
});

3. 修正Partials配置

Partials的枚举值存在拼写错误,针对messageCreate事件,只需要以下几个必要配置:

const { Partials } = require('discord.js');

const client = new Discord.Client({
    // ...intents配置
    partials: [
        Partials.Channel, // 处理DM频道
        Partials.Message, // 处理部分消息对象
        Partials.User // 处理用户对象
    ]
});

4. 补全客户端登录代码

必须调用login方法传入机器人Token,否则客户端无法连接到Discord网关:

client.login('你的机器人Token');

完整修正后的代码

const { GatewayIntentBits, Partials } = require('discord.js');
const Discord = require('discord.js');

const client = new Discord.Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.MessageContent
    ],
    partials: [
        Partials.Channel,
        Partials.Message,
        Partials.User
    ]
});

client.on('messageCreate', async message => {
    console.log('messageCreate event');
});

// 必须登录
client.login('你的机器人Token');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:50:45