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

Discord.js v14命令处理器故障:Collection未定义求助

问题解决步骤

1. 修复 Collection is not defined 错误

你在index.js里使用了Collection但未从discord.js导入,修改导入部分即可解决:

const {
    Client, Collection // 新增Collection导入
} = require("discord.js");
const client = new Client({
    intents: 32767
});
const {
    Token
} = require("./config.json");

client.commands = new Collection(); // 现在可正常初始化

require("./Handlers/Events")(client);

client.login(Token);

2. 修复命令处理器中的多处错误

你的处理器代码存在拼写、逻辑等问题,逐一修正如下:

2.1 修正导出拼写错误

module.export 是拼写错误,正确写法是module.exports,否则无法导出处理器函数:

// 替换开头的错误写法
module.exports = async (client) => {

2.2 声明局部变量

CommandsArray未用let/const声明,会变成全局变量,加上let约束作用域:

let CommandsArray = [];

2.3 替换async map为for...of

map搭配async不会等待每个文件加载完成,导致表格输出不完整,改用for...of确保顺序执行:

// 替换原有的map代码块
const commandFiles = await PG(`${process.cwd()}/Command/*/*.js`);
for (const file of commandFiles) {
    const command = require(file);

    if(!command.name) {
        Table.addRow(file.split("/")[7], "⛔ FAILED", "MISSING A NAME");
        continue;
    }

    if(!command.description) {
        Table.addRow(file.split("/")[7], "⛔ FAILED", "MISSING A DESCRIPTION"); // 修正错误提示文本
        continue;
    }

    if(command.permission) {
        if(Perms.includes(command.permission)) {
            command.defaultPermission = false;
        } else {
            Table.addRow(file.split("/")[7], "⛔ FAILED", "PERMISSION IS INVALID");
            continue;
        }
    }

    client.commands.set(command.name, command);
    CommandsArray.push(command);

    Table.addRow(command.name, "🕶 SUCCESSFUL"); // 修正拼写错误SUCCESFUL→SUCCESSFUL
}

2.4 修复ready事件中的逻辑错误

  • 修正reduce遍历的目标数组(command→CommandsArray)
  • 修复对象属性拼写错误({id: r,id,→{id: r.id,)
  • 修正权限类型字符串(ROLES→ROLE,符合Discord API要求)
client.on("ready", async () => {
    const MainGuild = client.guilds.cache.get("1117802218595291216");
    if (!MainGuild) {
        console.log("无法找到指定服务器");
        return;
    }

    const getRolesForCommand = (commandName) => {
        const cmd = CommandsArray.find(c => c.name === commandName);
        if (!cmd || !cmd.permission) return null;
        return MainGuild.roles.cache.filter(r => r.permissions.has(cmd.permission));
    };

    const fullPermissions = CommandsArray.reduce((accumulator, cmd) => {
        const roles = getRolesForCommand(cmd.name);
        if (!roles) return accumulator;

        const rolePermissions = roles.map(r => ({
            id: r.id,
            type: "ROLE",
            permission: true
        }));

        return [...accumulator, ...rolePermissions];
    }, []);

    try {
        await MainGuild.commands.permissions.set({ fullPermissions });
        console.log("权限设置完成");
    } catch (err) {
        console.error("设置权限时出错:", err);
    }
});

3. 额外注意事项

  • 确保Permissions.js中的Perms数组包含正确的Discord权限字符串,比如"ADMINISTRATOR"、"MANAGE_GUILD"等
  • 检查命令文件路径是否与${process.cwd()}/Command/*/*.js匹配
  • 建议在文件顶部添加'use strict';,提前发现未声明变量等低级错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:00:26