I have been working on creating a private Discord Bot and I've gotten pretty far with it as of now. There's just 1 issue so far, and I fear more in the future. I am trying to implement an economy system (money system) going off of a video tutorial (https://www.youtube.com/watch?v=Aw4b2VN1KW8) and I am coming across the same error. I have an idea as to what the issue is, but I am unsure of what to do to fix it and how. Below, is my existing index.js code as of now. (5/17/20) I am also using Visual studio code.
const Discord = require('discord.js');
const bot = new Discord.Client();
const fs = require('fs');
let userData = JSON.parse(fs.readFileSync('./Storage/userData.json', 'utf8'));
if (!userData[message.author.id + message.guild.id]) userData[message.author.id + message.guild.id] = {}
if (!userData[message.author.id + message.guild.id].money) userData[message.author.id + message.guild.id].money = 500;
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
if (err) console.error(err);
})
const token = 'my token';
const PREFIX = '+';
bot.on('ready', () =>{
console.log('This bot is now online.');
bot.user.setActivity('Mixing drinks');
})
bot.on('message', message=>{
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'badtouch':
message.channel.send('*pfffft* a bad touch...')
break;
case 'website':
message.channel.send('https://va11halla.fandom.com/wiki/VA-11_HALL-A_Wikia')
break;
case 'info':
if(args[1] === 'description'){
message.channel.send('Stuck here behind this screen in the "lovely" Glitch City, I mix drinks and change lives. Just be sure you have enough money to pay for drinks...');
}else{
message.channel.send('That is not a valid command')
}
break;
case 'clear':
if(!args[1]) return message.reply('There is an error')
message.channel.bulkDelete(args[1]);
break;
case 'random':
message.channel.send('I wonder if I left enough food at home for Fore... For fore, For four?')
break;
case 'shareabeer':
message.reply('Cheers. :beers:');
break;
case 'commands':
const attachment = new MessageAttachment('./command list.txt')
message.author.send(message.author, attachment);
break;
case 'aboutme':
message.reply('My name is Jill, and __NO__, you cannot know my full name. I am alergic to shrimp, I do not mind spicy foods, however I do not really prefer it. A little after graduation and other *events*, I decided to become a bartender.')
break;
case 'kira':
message.channel.send('MIKI!')
break;
case 'jules':
message.reply('Call me Jules and I will make sure every time you sit, you will hear the *cling* of the shaker.')
break;
}
})
bot.login(token);
下面是我添加到index.js代码中并开始收到此错误的内容:ReferenceError:消息未定义。如果(!userData [message.author.id + message.guild.id])userData [message.author.id + message.guild.id] = {}
ReferenceError:消息未定义 在Object。<匿名>
在向其添加这一小节之前,我的机器人工作正常,但尝试启动该机器人时会收到相同的ReferreceError。
let userData = JSON.parse(fs.readFileSync('./Storage/userData.json', 'utf8'));
if (!userData[message.author.id + message.guild.id]) userData[message.author.id + message.guild.id] = {}
if (!userData[message.author.id + message.guild.id].money) userData[message.author.id + message.guild.id].money = 500;
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
if (err) console.error(err);
})
如果我没有记错的话,我必须定义“消息”,但是我该如何以及在哪里做呢?如果可以,是否可以提供示例?
非常感谢。