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

discord.js v12运行nuke命令报Cannot read property 'get' of undefined如何修复

问题根因

报错Cannot read property 'get' of undefined指向client.commands.get('nuke'),说明你没有在客户端初始化时挂载client.commands集合,也没有提前注册加载nuke.js命令文件,导致client.commands为undefined,调用get方法时报错。

修复方案

方案1:补全命令注册逻辑(推荐,适合多命令项目)

  1. 初始化客户端时挂载commands集合
    在main.js中初始化Discord客户端的代码段后,添加如下代码:
const Discord = require('discord.js');
const client = new Discord.Client();
// 新增挂载集合的代码
client.commands = new Discord.Collection();
  1. 加载注册nuke.js命令
    在main.js中添加文件读取逻辑,把命令文件加载到集合中:
const fs = require('fs');
const path = require('path');

// 请替换为你存放nuke.js的文件夹相对路径,示例是nuke.js放在项目根目录的commands文件夹下
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(path.join(commandsPath, file));
    client.commands.set(command.name, command);
}
  1. 修复nuke.js的逻辑漏洞
    当前代码即使用户没有管理员权限,依然会执行删除克隆频道的操作,需要在权限判断后加return终止代码执行:
const Discord = require('discord.js')

module.exports = {
    name: 'nuke',
    execute(message) {
        if (!message.member.hasPermission('ADMINISTRATOR')) {
            // 新增return,无权限时直接返回不再执行后续代码
            return message.channel.send('missing permissions')
        }

        message.channel.clone().then(channel => {
            channel.setPosition(message.channel.position)
            channel.send('nuked')
        })
        message.channel.delete()
        
    },
};

方案2:直接调用命令(适合单命令小型项目)

如果你暂时不需要做命令注册体系,可以直接修改main.js中的命令判断逻辑,跳过client.commands直接引入执行nuke.js:

if(command === 'nuke'){
    // 替换为nuke.js相对于main.js的实际路径
    require('./nuke.js').execute(message, args);
}

同样需要按照方案1的第3步修复nuke.js的权限逻辑漏洞。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 02:57:03