我收到此错误:
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命令代码:
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 Home 64 Bit Ram:8GB,2GB专用 我的不和谐:Aawesome#6969
答案 0 :(得分:0)
代码div
抛出错误botcommandsmoney.get('auction').run(...)
,该错误指示Cannot read property 'run' of undefined
返回botcommandsmoney.get('auction')
。因此,我假设没有定义undefined
机器人命令。
我们可以看到,您正在使用行auction
在./cmds/money
中加载所有文件,并在botcommandsmoney
中将它们的导出设置为命令。看来,定义如何在botcommandsmoney.set(command.name, command)
内部调用命令的那条信息来自botcommandsmoney
,即文件内部的属性command.name
。
不过,从您的name
文件来看,我们可以看到属性auction.js
的值为name
而不是help
!因此,auction
而不是botcommandsmoney.get('help')
会返回您的命令...我认为这是一个复制粘贴错误,您可能是从另一个命令复制了文件,却忘记了更新botcommandsmoney.get('auction')
即使您重命名了文件本身。
因此,解决方法是将name
中的name: 'help'
更改为name: 'auction'
。