【问题标题】:I made setprefix with js and it has an error我用js做了setprefix,它有一个错误
【发布时间】:2021-07-03 18:53:38
【问题描述】:

我有一个不和谐的音乐机器人,它有很多功能。我做了一个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)
        }
    }
} 

setprefix 命令:

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}`);
    }
}

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

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

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

    尝试运行下面的sn-p,还是会报同样的错误:

    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 之外的所有内容,如下面的sn-p 所示。尝试运行它,你可以看到它正确地打印了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);

    您还需要添加一个带有键 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}`);
      },
    };
    

    【讨论】:

    • 有一个错误TypeError: args.shift is not a function at Object.execute (/home/runner/Rayong/commands/setprefix.js:9:25)
    • 您检查过args 是什么吗?您是否尝试过记录它是什么?它是一个数组吗?您是否已将其传递给主文件中的 execute 函数?在另一个文件中是execute(client, message, args){...。您是否将 message 作为第二个参数传递给每个 execute 方法?
    • 我有个小东西别名不起作用
    • 我已经更新了我的代码。您需要将aliases 数组添加到您的info,即使它只是一个空数组。
    【解决方案2】:

    module.exports 放置错误,如果我对您的代码正确,module.exports 应该位于您要导出的目录中。

    例子:-

    answer.js:- module.exports = {
    'value': {
        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}`);
    }
    
    index.js:- const answer = require(./answer)
    
    message.channel.send("Name:- $(answer.name) \n argsNumber:- $(answer.argsNumber)") //etc....
    

    如果我错了:-

    1.请问你的命令在哪个目录下?

    2.你能说出你为什么在那个目录下做module.exports吗?

    3.尝试指出错误的确切原因或行。

    请在评论中告诉我...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多