首页 > 解决方案 > discord.js 无法读取未定义的属性“执行”

问题描述

我收到此错误:

botcommandsmoney.get('auction').run(bot, message, args, err, auctionhouse)
                                           ^

TypeError: Cannot read property 'run' of undefined

我在 index.js(开始文件)中的代码是这样的:

//#region Consts

const Discord = require('discord.js')
const bot = new Discord.Client()
const fs = require('fs')
const userdata = require('./userdata.json')
const botcommands = new Discord.Collection()
const auctionhouse = new Object()

//#endregion

//#region Command Files

const commandFiles = fs.readdirSync('./cmds/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./cmds/${file}`)

    botcommands.set(command.name, command)
}

const botcommandsfun = new Discord.Collection()
const commandFilesFun = fs.readdirSync('./cmds/fun/').filter(file => file.endsWith('.js'))
for(const file of commandFilesFun){
    const command = require(`./cmds/fun/${file}`)

    botcommandsfun.set(command.name, command)
}

const botcommandsinfo = new Discord.Collection()
const commandFilesInfo = fs.readdirSync('./cmds/info/').filter(file => file.endsWith('.js'))
for(const file of commandFilesInfo){
    const command = require(`./cmds/info/${file}`)

    botcommandsinfo.set(command.name, command)
}

const botcommandsitems = new Discord.Collection()
const commandFilesItems = fs.readdirSync('./cmds/items/').filter(file => file.endsWith('.js'))
for(const file of commandFilesItems){
    const command = require(`./cmds/items/${file}`)

    botcommandsitems.set(command.name, command)
}

const botcommandsmisc = new Discord.Collection()
const commandFilesMisc = fs.readdirSync('./cmds/misc/').filter(file => file.endsWith('.js'))
for(const file of commandFilesMisc){
    const command = require(`./cmds/misc/${file}`)

    botcommandsmisc.set(command.name, command)
}

const botcommandsmod = new Discord.Collection()
const commandFilesmod = fs.readdirSync('./cmds/mod/').filter(file => file.endsWith('.js'))
for(const file of commandFilesmod){
    const command = require(`./cmds/mod/${file}`)

    botcommandsmod.set(command.name, command)
}

const botcommandsmoney = new Discord.Collection()
const commandFilesmoney = fs.readdirSync('./cmds/money/').filter(file => file.endsWith('.js'))
for(const file of commandFilesmoney){
    const command = require(`./cmds/money/${file}`)

    botcommandsmoney.set(command.name, command)
}

//#endregion

bot.login('my token, i\'m not gonna tell you it')

bot.on('ready', () => {
    bot.user.setActivity('*help', { type: "WATCHING" })
})

bot.on('message', message => {


    //#region Setting up data
        if(!userdata[message.author.id]) {
            userdata[message.author.id] = {
                name: message.author.username,
                referer: null,
                joinguild: {
                    guildid: message.guild.id,
                    guildname: message.guild.name
                },
                userconfig: {
                    commands: {
                        amount: 0,
                        category: {
                            fun: 0,
                            info: 0,
                            moderation: 0,
                            misc: 0,
                            money: 0,
                            item: 0,
                            premium: 0
                        }
                    },
                    messages: 1,
                    coupons: 0,
                    passive: false
                },
                profile: {
                    rob: {
                        robs: 0,
                        successrobs: 0,
                        failedrobs: 0,
                        heists: 0
                    },
                    pay: {
                        payed: 0,
                        recieved: 0
                    },
                    account: {
                        status: "A waffle bot user!",
                        skin: "default",
                        food: "waffles",
                        wishlist: [],
                        funfact: "A cool waffle bot user!",
                        pfp: message.author.avatarURL()
                    }
                }
            }
            fs.writeFile("./userdata.json", JSON.stringify(userdata), err => {
                if (err) console.log(err);
              });
        }
        var item1id = Math.floor((Math.random() * 3) + 1);
        var item2id = Math.floor((Math.random() * 10) + 1);
        while(item2id == item1id){
            var item2id = Math.floor((Math.random() * 10) + 1);
        }
        var item3id = Math.floor((Math.random() * 10) + 1);
        while(item3id == item2id || item3id == item1id){
            var item3id = Math.floor((Math.random() * 10) + 1);
        }
        if(!auctionhouse){
            auctionhouse = {
                item1: {
                    name: "Spot One",
                    seller: null,
                    startingprice: 0,
                    currentprice: 0,
                    winningbidder: null,
                    id: item1id
                },
                item2: {
                    name: "Spot Two",
                    seller: null,
                    startingprice: 0,
                    currentprice: 0,
                    winningbidder: null,
                    id: item2id
                },
                item3: {
                    name: "Spot Three",
                    seller: null,
                    startingprice: 0,
                    currentprice: 0,
                    winningbidder: null,
                    id: item3id
                }
            }
        }

    //#endregion
    
    //#region Setting Up RichEmbeds
        function err(error){
            embed = new Discord.MessageEmbed()
            .setTitle('<a:no:676180589895876611> ' + error)
            .setColor("RED")
            message.channel.send(embed)
        }
        function success(msg){
            embed = new Discord.MessageEmbed()
            .setTitle('<a:yes:676180565434695747> ' + msg)
            .setColor("GREEN")
            message.channel.send(embed)
        }
    //#endregion
    
    const args = message.content.substring("*").split(" ");
    switch(args[0]){
        case `*help`:
            botcommandsinfo.get('help').execute(message, args)
            break;
        case '*snipe':
            botcommandsinfo.get('snipe').execute(bot, message, args, err)
            break;
        case '*auctionhouse':
            botcommandsmoney.get('auction').execute(bot, message, args, err, auctionhouse)
            break;
    }
})
bot.snipes = new Map()

//#region Firing Events
    bot.on('messageDelete', function(message, channel) {
        bot.snipes.set(message.channel.id, {
            content: message.content,
            author: message.author,
            image: message.attachments.first() ? message.attachments.first().proxyURL : null
        })
    })

//#endregion

我所有的其他命令都可以工作(狙击和帮助),但 *auctionhouse 不行。

我的 *auctionhouse 命令代码:

module.exports = {
    name: 'help',
    description: "Help command!",
    execute(bot, message, args, err, auctionhouse){
    const Discord = require('discord.js')
    const embed = new Discord.MessageEmbed()
    .setTitle('Auction House')
    .setDescription('To bid on an item, run `*bid <id> <amount>`.')
    .addField(`${auctionhouse.item1.name} - $${auctionhouse.item1.currentprice} - ID: \`${auctionhouse.item1.id}\``)
    .addField(`${auctionhouse.item2.name} - $${auctionhouse.item2.currentprice} - ID: \`${auctionhouse.item2.id}\``)
    .addField(`${auctionhouse.item3.name} - $${auctionhouse.item3.currentprice} - ID: \`${auctionhouse.item3.id}\``)
    .setColor("RANDOM")
    message.channel.send(embed)
    }
}


这是目录清单:堆栈溢出不会让我添加图像:(所以这是一个 imgur 链接

细节:

运行时:Node.JS v12 Discord.JS 版本:12.0.0 操作系统:Windows 10 Home 64 Bit Ram:8GB,2GB 专用我的 Discord:Aawesome#6969

标签: javascriptnode.jsdiscorddiscord.jstypeerror

解决方案


代码botcommandsmoney.get('auction').run(...)抛出Cannot read property 'run' of undefined指示botcommandsmoney.get('auction')返回的错误undefined。因此,我假设没有auction定义任何 bot 命令。

我们可以看到您正在使用该行加载所有文件./cmds/money并将其导出设置为命令。看起来,定义如何在内部调用命令的信息来自,即文件内的属性。botcommandsmoneybotcommandsmoney.set(command.name, command)botcommandsmoneycommand.namename

但是查看您的auction.js文件,我们可以看到该属性name具有 valuehelp而不是auction! 所以,botcommandsmoney.get('help')而不是botcommandsmoney.get('auction')返回你的命令......我认为这是一个复制粘贴错误,你可能从另一个命令复制了文件并且忘记更新它的name内部,即使你重命名了文件本身。

因此,解决方法是更改name: 'help'​​为name: 'auction'in auction.js


推荐阅读