【问题标题】:How to fix UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined discord.js v12如何修复 UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined discord.js v12
【发布时间】:2021-10-31 12:36:59
【问题描述】:

我正在处理 nuke 命令,但是当我执行命令 ($nuke) 时出现此错误:

(node:3888) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined

这是我的 main.js nuke 命令代码

const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if(command === 'nuke'){
    client.commands.get('nuke').execute(message, args);
  }

这是我的 nuke.js 代码

const Discord = require('discord.js')

module.exports = {
    name: 'nuke',
    execute(message) {
        if (!message.member.hasPermission('ADMINISTRATOR')) {
            message.channel.send('missing permissions')
        }

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

错误来自

if(command === 'nuke'){
    client.commands.get('nuke').execute(message, args);
  }

【问题讨论】:

  • 您如何导入/实例化客户端。该错误表明client.commandsundefined,这意味着client 对象上没有属性commands
  • client.commands 未定义。您是否尝试使用client.commands = new Discord.Collection()new Collection()new Map() 来定义它?

标签: javascript node.js discord discord.js bots


【解决方案1】:

这里的问题似乎是,你从来没有设置你的命令到client.commands。为此,您首先必须在您的index.js / main.js(您的启动文件)中创建一个新的collection

这是一个例子:

const Discord = require('discord.js');

// Your other code

client.commands = new Discord.Collection();

最简单的继续方法是使用命令处理程序自动设置所有命令。如果你没有,这是我的:

command_handler.js

const fs = require('fs');
const path = require('path');
const rootDir = path.dirname(require.main.filename);
const fileArray = [];

const readCommands = (dir) => {

    const __dirname = rootDir;

    // Read out all command files
    const files = fs.readdirSync(path.join(__dirname, dir));

    // Loop through all the files in ./commands
    for (const file of files) {
        // Get the status of 'file' (is it a file or directory?)
        const stat = fs.lstatSync(path.join(__dirname, dir, file));

        // If the 'file' is a directory, call the 'readCommands' function
        // again with the path of the subdirectory
        if (stat.isDirectory()) {
            readCommands(path.join(dir, file));
        }
        else {
            const fileDir = dir.replace('\\', '/');
            fileArray.push(fileDir + '/' + file);
        }
    }
};


readCommands(/*name of directory your commands are stored in*/);

for(const file of fileArray) {
        const command = require(`../${file}`);

        if(command.name) {
            client.commands.set(command.name, command);
        }
    }

请注意,此文件位于名为handlers 的自己的目录中。此外,您可能需要调整项目结构的一些路径

现在看起来不错,但要使用它,您必须在登录机器人之前在 index.js / main.js 中调用它:

require(`./handlers/command_handler`)(client);

差不多就是这样。如果您还有其他问题,请告诉我,在这种情况下,只需加入我的个人资料中链接的 Discord 服务器即可。也有人可以帮助你。

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2020-05-02
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多