首页 > 解决方案 > 我用js做了setprefix,它有一个错误

问题描述

我有一个不和谐的音乐机器人,它有很多功能。我先做了一个setprefix非常完美的命令,然后help出现了错误。错误是:

TypeError: Cannot read property 'name' of undefined

我不知道该怎么做有人可以帮助我吗?

help.js

const { MessageEmbed } = require('discord.js')

module.exports = {
    info: {
        name: "help",
        description: "To show all commands",
        usage: "[command]",
        aliases: ["commands", "help me", "pls help"]
    },

    execute(client, message, args){
        var allcmds = "";

        client.commands.forEach(cmd => {
            let cmdinfo = cmd.info
            allcmds+="``"+message.client.prefix+cmdinfo.name+" "+cmdinfo.usage+"`` ~ "+cmdinfo.description+"\n"
        })

        let embed = new MessageEmbed()
        .setAuthor("Commands of "+client.user.username, "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
        .setColor("BLUE")
        .setDescription(allcmds)
        .setFooter(`To get info of each command you can do ${client.config.prefix}help [command] and my prefix is % | Hander by Lavaboy0192#2014`)

        if(!args[0])return message.channel.send(embed)
        else {
            let cmd = args[0]
            let command = client.commands.get(cmd)
            if(!command)command = client.commands.find(x => x.info.aliases.includes(cmd))
            if(!command)return message.channel.send("Unknown Command")
            let commandinfo = new MessageEmbed()
            .setTitle("Command: "+command.info.name+" info")
            .setColor("YELLOW")
            .setDescription(`
Name: ${command.info.name}
Description: ${command.info.description}
Usage: \`\`${client.config.prefix}${command.info.name} ${command.info.usage}\`\`
Aliases: ${command.info.aliases.join(", ")}
`)
            message.channel.send(commandinfo)
        }
    }
} 

设置前缀命令:

module.exports = {
    name: "setprefix",
    description: "Sets the prefix for the bot",
    argsNumber: 1,
    usage: "<prefix>",
    execute(message, args){
        const prefix = args.shift();
        message.client.prefixes.set(message.guild.id, prefix);
        message.reply(`Prefix set to ${prefix}`);
    }
}

标签: javascriptnode.jsdiscord.js

解决方案


这是因为您导出的模块上没有info属性。从您help.js文件中的导出来看,您的其他命令文件导出一个带有两个键的对象;infoexecute

在您的execute方法中,您help.js迭代client.commands并尝试获取info属性(let cmdinfo = cmd.info)的每个命令。由于您的命令没有info道具,因此将是未定义的。在下一行,您尝试获取, ,但由于它无法读取属性,它会抛出"TypeError: Cannot read property 'name' of undefined"setprefixcmdinfocmdinfo.namecmdinfo.usagecmdinfo.descriptionname

尝试运行下面的代码片段,它会抛出同样的错误:

let cmd = {
  name: 'setprefix',
  description: 'Sets the prefix for the bot',
  argsNumber: 1,
  usage: '<prefix>',
  execute: {},
};

let cmdinfo = cmd.info;

console.log(cmdinfo.name);

要解决此问题,您需要添加一个新info对象并移动除execute其内部之外的所有内容,如下面的代码片段所示。尝试运行它,您可以看到它name正确打印:

let cmd = {
  info: {
    name: 'setprefix',
    description: 'Sets the prefix for the bot',
    argsNumber: 1,
    usage: '<prefix>',
  },
  execute: {},
};

let cmdinfo = cmd.info;

console.log(cmdinfo.name);

您还需要使用 key 添加一个数组aliases,即使没有,因为上面的代码也试图找到它们。这是您的最终代码。

module.exports = {
  info: {
    name: 'setprefix',
    description: 'Sets the prefix for the bot',
    argsNumber: 1,
    usage: '<prefix>',
    aliases: [],
  },
  execute(message, args) {
    const prefix = args.shift();
    message.client.prefixes.set(message.guild.id, prefix);
    message.reply(`Prefix set to ${prefix}`);
  },
};

推荐阅读