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

启动Discord机器人时出现TypeError: bot.commands.get is not a function错误如何解决?

修复Discord机器人 TypeError: bot.commands.get is not a function 错误

错误原因分析

这个报错说明你的bot.commands并不是一个有效的Discord Collection实例,因此它没有get()方法。最常见的诱因是Discord.js v13及以上版本的API结构变动,旧的Collection创建方式已经不再适配新版本。

具体修复步骤

1. 调整Collection的导入与初始化逻辑

如果你使用的是Discord.js v13+版本,需要通过解构语法单独导入Collection,而不是从Discord全局对象中调用:

// 替换原有的Discord导入语句
const { Client, Collection } = require("discord.js");
// v13+版本必须指定intents,按需添加机器人需要的权限,比如Guilds、GuildMessages等
const bot = new Client({ intents: ["Guilds", "GuildMessages"] });

// 重新初始化commands为标准Collection实例
bot.commands = new Collection();

2. 检查并更新Discord.js版本(可选但推荐)

先在项目根目录执行命令查看当前版本:

npm list discord.js

如果版本低于v13,建议更新到稳定版:

npm install discord.js@latest

3. 确认命令加载逻辑无误(额外校验)

确保你已经把命令文件正确存入bot.commands集合中,比如常见的命令加载代码:

const fs = require("fs");
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    // 以命令名为键,命令模块为值存入集合
    bot.commands.set(command.name, command);
}

只有命令被正确存入集合后,bot.commands.get()才能正常找到目标命令。

验证修复

修改完成后重启机器人,如果不再抛出TypeError且命令能正常响应,说明修复生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:22:16