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

Discord机器人无法发送消息且无报错,此前正常运行求助

问题排查与解决方案

核心问题梳理

你的机器人无法工作且无报错,主要由以下几个关键问题导致:


1. 缺失MESSAGE_CONTENT特权意图

Discord最新政策要求,机器人必须启用MESSAGE_CONTENT特权网关意图才能读取消息内容,否则无法识别前缀命令。

解决步骤:

  • 登录Discord开发者门户,进入你的机器人应用页面
  • 在「Privileged Gateway Intents」板块,开启MESSAGE CONTENT INTENT
  • 修改主文件中的Intents配置:
const client = new Discord.Client({ 
  intents: ["GUILDS", "GUILD_MESSAGES", "MESSAGE_CONTENT"] 
});

2. 事件监听版本不兼容

Discord.js v13+已废弃原message事件,改用messageCreate事件,你的事件处理器未适配此变更,导致命令逻辑无法触发。

解决步骤:

  • 确保event_handler正确加载事件文件,示例event_handler.js代码:
const fs = require('fs');

module.exports = (client, Discord) => {
  const eventFiles = fs.readdirSync('./events/').filter(file => file.endsWith('.js'));
  
  for (const file of eventFiles) {
    const event = require(`../events/${file}`);
    if (event.once) {
      client.once(event.name, (...args) => event.execute(...args, client, Discord));
    } else {
      client.on(event.name, (...args) => event.execute(...args, client, Discord));
    }
  }
}
  • 在项目根目录创建events文件夹,新建messageCreate.js文件:
const prefixHandler = require('../prefix');

module.exports = {
  name: 'messageCreate',
  execute(message, client, Discord) {
    prefixHandler(Discord, client, message);
  }
}
  • 修正prefix.js的冗余参数(移除未使用的cron):
module.exports = (Discord, client, message) => {
    const prefix = '!';
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd);

    if(command) command.execute(client, message, args, Discord);
}

3. 未添加机器人登录代码

你的主文件中没有调用client.login(),机器人根本无法连接到Discord服务器。

解决步骤:
在主文件末尾添加机器人登录代码(替换为你的机器人TOKEN):

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

4. Cron任务变量重复覆盖

你用同一个var task定义了两个定时任务,第二个会覆盖第一个,导致第一个任务失效。

解决步骤:
修改变量名避免冲突:

var task1 = cron.schedule('10,30,50 * * * * *', () => {
    client.user.setActivity('status', { type: "PLAYING" });
});

var task2 = cron.schedule('1,20,40,59 * * * * *', () => {
    client.user.setActivity('status', { type: "PLAYING" });
});

验证步骤

  1. 保存所有修改后重启机器人
  2. 在Discord服务器发送!regulamin测试命令
  3. 检查终端是否有报错,机器人是否正常显示活动状态

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 15:10:16