因此,我一直在关注如何制作经济机器人的教程,该教程可在tutorial中找到,我还添加了一些自己的变量和命令。我想知道机器人是否有办法基本询问“您想购买多少这种类型的物品?”,然后将其提供给他们,而不必用垃圾邮件的购买命令来获取某物品的100项目。
这是我的代码:
const bot = new Discord.Client();
const fs = require('fs');
const moment = require('moment');
let userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'));
bot.on('message', message => {
let sender = message.author;
let msg = message.content.toUpperCase();
let prefix = '!'
if (bot.user.id === message.author.id) {
return
}
if (!userData[sender.id + message.guild.id]) userData[sender.id + message.guild.id] = {}
if (!userData[sender.id + message.guild.id].money) userData[sender.id + message.guild.id].money = 10000;
if (!userData[sender.id + message.guild.id].taxes) userData[sender.id + message.guild.id].taxes = 'Not Collected';
if (!userData[sender.id + message.guild.id].soldiers) userData[sender.id + message.guild.id].soldiers = 0;
if (!userData[sender.id + message.guild.id].battleships) userData[sender.id + message.guild.id].battleships = 0;
if (!userData[sender.id + message.guild.id].fighterjets) userData[sender.id + message.guild.id].fighterjets = 0;
if (!userData[sender.id + message.guild.id].tanks) userData[sender.id + message.guild.id].tanks = 0;
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
if (err) console.error(err);
})
if (msg === prefix + 'STORE' || msg === prefix + 'SHOP' || msg === prefix + 'S') {
message.channel.send({
embed: {
title: "Personalized Shop",
color: 0xE40000,
fields: [{
name: "Every nation's store is different. This was requested by: " + message.author.username,
value: "Soldiers:$150, Battleship:$10000, Fighter Jet: $5000, Tank:$3000, Welfare:$3000",
inline: true
}]
}
})
}
if (msg === prefix + 'BUY') {
}
// END OF STORE.....................................................
if (msg === prefix + 'COL' || msg === prefix + 'COLLECT' || msg === prefix + 'TAX' || msg === prefix + 'TAXES') {
if (userData[sender.id + message.guild.id].taxes != moment().format('L')) {
userData[sender.id + message.guild.id].taxes = moment().format('L')
userData[sender.id + message.guild.id].money += 10 * 1000;
message.channel.send({
embed: {
title: "Your Balance",
color: 0xE40000,
fields: [{
name: "You made 10000$",
value: message.author.username,
inline: true
},
]
}
})
} else {
message.channel.send({
embed: {
title: "Error",
color: 0xE40000,
fields: [{
name: "You have already collected taxes today:",
value: message.author.username,
inline: true
}
]
}
})
}
}
if (msg === prefix + 'A' || msg === prefix + 'ARMY' || msg === prefix + 'MILITARY') {
message.channel.send({
embed: {
title: message.author.username + "'s Army",
color: 0xE40000,
fields: [
{
name: "Soldiers:",
value: userData[sender.id + message.guild.id].soldiers,
inline: true
},
{
name: "Battleships:",
value: userData[sender.id + message.guild.id].battleships,
inline: true
},
{
name: "Fighter Jets:",
value: userData[sender.id + message.guild.id].fighterjets,
inline: true
},
{
name: "Tanks:",
value: userData[sender.id + message.guild.id].tanks,
inline: true
}
]
}
})
}
if (msg === prefix + 'MONEY' || msg === prefix + 'BAL' || msg === prefix + 'BALANCE') {
message.channel.send({
embed: {
title: "Your Balance",
color: 0xE40000,
fields: [{
name: "Nation:",
value: message.author.username,
inline: true
},
{
name: "Money:",
value: userData[sender.id + message.guild.id].money,
inline: true
}
]
}
})
}
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
if (err) console.error(err);
})
});
bot.on('ready', () => {
console.log('bot is ready')
})
我的JSON文件如下:
{"Id goes here, deleted it for safety purposes":{"money":11000,"taxes":"02/17/2020","amount":0,"soldiers":0,"battleships":0,"fighterjets":0,"tanks":0}}