Discord.js v14开发机器人遇TypeError读取'set'属性未定义求助
Discord机器人报错:TypeError: Cannot read properties of undefined (reading 'set') 解决方法
错误信息
- 报错内容:
TypeError: Cannot read properties of undefined (reading 'set') - 报错位置:
at Client.client.handleCommands (C:\Users\Admin\Documents\galax-bot\functions\handlers\handleCommands.js:19:37)at Object.<anonymous> (C:\Users\Admin\Documents\galax-bot\index.js:19:8)
问题背景
我正在为朋友的服务器开发Discord机器人,遇到上述错误,已查阅官方文档及多种资料仍无法解决,相关代码如下:
handleCommands.js
const { ClientUser } = require("discord.js"); const { REST } = require("@discordjs/rest") const { Routes } = require("discord-api-types/v9") const fs = require("fs") module.exports = (client) => { client.handleCommands = async () => { const commandFolders = fs.readdirSync(`./commands`); for (const folder of commandFolders) { const commandFiles = fs .readdirSync(`./commands/${folder}`) .filter((file) => file.endsWith(".js")); const { commands, commandArray } = client; for (const file of commandFiles) { const command = require(`../../commands/${folder}/${file}`) if (command) { console.log(command.data) client.commands.set(command.name, command); commandArray.push(command.data.toJSON()); console.log(`Command: ${command.data.name} has been passed through the handler`) } } } } const clientId = '1033991303165390878'; const guildId = '905393827488071701'; const rest = new REST({ version: "9" }).setToken("Not letting you see the token"); try { console.log('Started refreshing application (/) commands.'); rest.put(Routes.applicationCommands(clientId, guildId), { body: client.commandArray, }); console.log('Successfully reloaded application (/) commands.'); } catch (error) { console.error(error); } }
index.js
require("dotenv").config const { Client, Collection , GatewayIntentBits, EmbedBuilder, PermissionsBitField, Permissions } = require("discord.js"); const Token = process.env.token const prefix = '^'; const fs = require('fs') const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.commads = new Collection(); client.commandArray = [] const functionFolders = fs.readdirSync(`./functions`) for (const folder of functionFolders) { const functionFiles = fs .readdirSync(`./functions/${folder}`) .filter((file) => file.endsWith(".js")); for (const file of functionFiles) require(`./functions/${folder}/${file}`)(client); } client.handleEvents(); client.handleCommands(); client.login(Token)
问题分析与解决
核心问题:拼写错误
报错的直接原因是client.commands未定义——在index.js中你把commands拼写成了commads(少了一个n),导致handleCommands.js中调用client.commands.set()时,目标对象不存在,触发报错。
修复步骤
打开
index.js,找到这一行:client.commads = new Collection();修正为:
client.commands = new Collection();额外修复:
require("dotenv").config需要添加括号才能执行初始化逻辑,否则环境变量无法正常加载,修正为:require("dotenv").config();
其他优化建议
- 刷新应用命令的代码目前在
handleCommands函数外部,而handleCommands是异步函数,可能导致刷新时commandArray还未填充完成。建议将REST相关代码移到handleCommands函数内部末尾,确保命令数组准备好后再发送:
同时注意使用client.handleCommands = async () => { // 原有遍历命令文件夹的代码... // 将REST刷新代码移到这里 const clientId = '1033991303165390878'; const guildId = '905393827488071701'; const rest = new REST({ version: "9" }).setToken(process.env.token); try { console.log('Started refreshing application (/) commands.'); await rest.put(Routes.applicationCommands(clientId, guildId), { body: client.commandArray, }); console.log('Successfully reloaded application (/) commands.'); } catch (error) { console.error(error); } }await确保刷新请求完成,并且用环境变量Token代替硬编码,提升安全性。
内容的提问来源于stack exchange,提问作者tulisnice
相关产品推荐
相关产品推荐

