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

如何修复Discord.js v13命令处理器代码的'set is undefined'错误

错误原因与解决方案

错误1:未初始化集合属性

Discord.js v13 不会默认给 Client 实例挂载 commands 和 aliases 存储属性,你没有手动将这两个属性初始化为 Collection(或原生Map)的情况下,二者的值为undefined,调用set方法就会抛出该错误。

错误2:变量引用错误

你提供的代码中存在无效变量引用:

// 错误写法,command变量未定义
client.commands.set(pull.name, command);

你导入的命令配置对象为pull,此处应该传入pull而不是不存在的command变量。


修复步骤

  1. 在客户端初始化代码中添加集合挂载逻辑
    在你创建Client实例的入口文件(通常为index.js)中添加以下代码:
const { Client, Collection, Intents } = require('discord.js');
// v13必须显式配置所需Intents,此处为示例,可根据你机器人功能调整
const client = new Client({ 
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] 
});

// 手动初始化命令和别名存储集合
client.commands = new Collection();
client.aliases = new Collection();
  1. 修正command.js中的变量错误
    将错误行替换为正确写法:
client.commands.set(pull.name, pull);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 18:48:03