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

Discord.js报错TypeError: Cannot read property 'get' of undefined 如何解决

问题修复方案

错误原因

  • 报错TypeError: Cannot read property 'get' of undefined指向消息处理器第8行的client.commands.get(cmd),根因是Discord客户端实例未挂载commands集合,你需要在项目主入口文件中初始化该集合并完成所有指令的加载注册。
  • 消息处理器调用execute方法的传参顺序和seed指令定义的接收顺序不匹配,会导致后续参数错位无法正常取值。
  • seed指令内部声明的Embed实例变量名为seedEmbed,但发送时用了未定义的newEmbed变量,会额外触发引用错误。

修复步骤

1. 补全主入口文件的commands初始化逻辑

在你初始化client的主文件(通常为index.js)中添加如下代码:

const { Collection } = require('discord.js');
// 给client挂载commands集合
client.commands = new Collection();

// 加载所有指令到集合中,按你实际的指令存放路径调整即可
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

2. 统一execute方法的参数顺序

两边任选一种方案调整即可:
方案一:修改消息处理器的调用逻辑,删去多余的client参数

// 把原来的command.execute(client, message, args, Discord);改成
if(command) command.execute(message, args, Discord);

方案二:修改seed指令的参数定义,适配传入的client参数

// 把原来的execute(message, args, Discord){改成
execute(client, message, args, Discord){

3. 修复seed指令的变量名错误

把seed指令里的发送代码改成使用正确的变量名:

// 把原来的message.channel.send({embeds: [newEmbed]});改成
message.channel.send({embeds: [seedEmbed]});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 23:09:04