discord.js无法读取未定义的属性“执行”[关闭]

Posted

技术标签:

【中文标题】discord.js无法读取未定义的属性“执行”[关闭]【英文标题】:discord.js Cannot read property 'execute' of undefined [closed] 【发布时间】:2020-10-22 12:23:59 【问题描述】:

我收到此错误:

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,
                        succes-s-robs: 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)
    



这是目录列表:stack overflow won't let me add images yet :( so this is an imgur link

详情:

运行时:Node.JS v12 Discord.JS 版本:12.0.0 操作系统:Windows 10 家庭版 64 位 内存:8GB,2GB 专用 我的不和谐:太棒了#6969

【问题讨论】:

你的auction.js里面有什么?我敢打赌name 没有auction,可能是您从另一个文件中复制了它而忘记更新名称。所以,botcommandsmoney.set 会被调用,而不是auction,而botcommandsmoney.get('auction') 会返回undefined,因此butcommandsmoney.get('auction').run() 会抛出错误cannot read property 'run' of undefined @CherryDT 只是仔细检查了一遍,不,肯定是auction.js 不,我说的是:检查 auction.jsinside 设置的 name 属性。您的代码通过名称属性而不是文件名分配机器人功能。 哦,谢谢!这是“帮助”! 不客气。 (这个问题现在可能要结束了。) 【参考方案1】:

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

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

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

因此,解决方法是将auction.js 中的name: 'help' 更改为name: 'auction'

【讨论】:

以上是关于discord.js无法读取未定义的属性“执行”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

错误:无法读取未定义的属性“执行”

Discord.js 无法读取未定义的属性“获取”

Discord.js 的欢迎消息 - “无法读取未定义的属性 'get'”

discord.js 成员计数 - 无法读取未定义的属性“通道”

无法读取未定义的属性“发送”(Discord.js)

Discord.js 无法读取未定义的属性“发送”