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

求助:我的discord.js机器人为何不回复消息?

Discord Bot 无响应问题修复

问题原因分析

以下是导致机器人无法响应消息的核心问题:

  • 事件监听过时:Discord.js v14+ 已废弃 message 事件,需改用 messageCreate 事件监听消息。
  • 缺失关键意图权限:读取消息内容必须启用 MessageContent 意图,且需在开发者平台手动开启该权限。
  • 频道获取方法失效:client.channels.get() 已被移除,需使用异步的 client.channels.fetch() 方法获取频道。
  • 未处理异步操作:频道获取是异步任务,直接同步调用会导致 channel 变量为 undefined。

修复后的完整代码

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

const token = 'My Token';

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent // 必须添加此意图才能读取消息内容
    ]
});

client.on('ready', () => {
    console.log("Bot is on");
});

client.on('messageCreate', async (message) => { // 替换为 messageCreate 事件
    try {
        const channel = await client.channels.fetch("1028177045773094915");
        if (message.content === 'ping') {
            await channel.send("pong");
        }
    } catch (error) {
        console.error('操作失败:', error);
    }
});

client.login(token);

必要配置步骤

  1. 打开 Discord 开发者平台,找到你的机器人应用。
  2. 进入「Bot」页面,在「Privileged Gateway Intents」区域开启「Message Content Intent」权限。

内容的提问来源于stack exchange,提问作者A Long Zeep

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 01:30:47