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

Discord机器人加载命令报错:Cannot read properties of null (reading 'commands')

解决Discord机器人TypeError: Cannot read properties of null (reading 'commands')错误

问题重现

运行Discord机器人时触发以下错误:

/Users/roopa/Desktop/projects/LLbot/src/handlers/commandHandler.js:27
    client.application.commands.set(commandsArray);
                       ^

TypeError: Cannot read properties of null (reading 'commands')
    at loadCommands (/Users/roopa/Desktop/projects/LLbot/src/handlers/commandHandler.js:27:24)
    at Object.<anonymous> (/Users/roopa/Desktop/projects/LLbot/src/bot.js:274:1)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Node.js v20.2.0

对应的commandHandler.js代码:

function loadCommands(client) {
    const fs = require('fs');
    const ascii = require('ascii-table');
    const table = new ascii().setHeading('Commands', 'Status');

    let commandsArray = [];

    const commandsFolder = fs.readdirSync('/Users/roopa/Desktop/projects/LLbot/src/Commands');
    
    for (const folder of commandsFolder) {
        const commandFiles = fs.readdirSync(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}`).filter((file) => file.endsWith('.js'));

        for (const file of commandFiles) {
            const commandFile = require(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}/${file}`);

            client.commands.set(commandFile.data.name, commandFile);
            commandsArray.push(commandFile.data.toJSON());
            table.addRow(file, '🟩');
            continue;
        }
    }

    client.application.commands.set(commandsArray);

    return console.log(table.toString(), '\nCommands Loaded')
}
module.exports = { loadCommands };

错误原因分析

你怀疑commandsArray为空是报错原因,但实际上直接触发错误的原因是client.application为null。如果仅commandsArray为空,调用.set()只会清空全局命令,不会触发Cannot read properties of null错误。

导致client.application为null的常见场景:

  • loadCommands被过早调用,Discord客户端还未完成登录初始化,client.application还未被Discord API赋值
  • 机器人邀请链接缺少applications.commands权限,导致客户端无法获取应用信息
  • 使用了Discord用户账户的token登录(而非机器人账户token),用户账户没有application属性

解决方法

1. 延迟命令加载到客户端就绪后

修改bot.js中调用loadCommands的时机,确保在客户端完全登录并触发ready事件后执行:

// 替换原来直接调用loadCommands的代码
client.on('ready', () => {
  console.log(`已登录为 ${client.user.tag}`);
  loadCommands(client); // 在这里调用命令加载函数
});

2. 检查并修复机器人权限

  • 生成机器人邀请链接时,确保包含applications.commands权限(权限代码:32768)
  • 重新邀请机器人到服务器,确保权限生效

3. 验证登录凭证类型

确认你使用的是机器人账户的token登录,而非Discord用户账户的token。用户账户无法使用client.application相关功能。

4. 添加安全检查避免崩溃

在commandHandler.js中添加判断,防止client.application为null时执行代码:

// 替换原来的client.application.commands.set(commandsArray);
if (!client.application) {
  console.error('客户端应用信息未就绪,无法加载命令');
  return console.log(table.toString(), '\n命令加载中断');
}
client.application.commands.set(commandsArray);

5. 排查commandsArray是否为空(可选)

虽然不是本次报错原因,但可以添加日志确认命令是否被正确读取:

// 在设置命令前添加
console.log(`共加载 ${commandsArray.length} 条命令`);
client.application.commands.set(commandsArray);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 20:05:18