如何使代码适用于每个国家/地区? 现在,我刚刚为该机器人编写了代码,因此它可以用已确认的德国电晕案例来回答!corona。
这是代码:
bot.on('message', msg=>{
if(msg.content === '!corona'){
const url = "https://coronavirus-19-api.herokuapp.com/countries/germany"
req(url, function(err, response, body){
if(err) return msg.reply("err")
body = JSON.parse(body)
msg.reply(`Corona Fälle für: + \*\*${body.cases}\*\*\ ` ,)
}
,)}
答案 0 :(得分:1)
您可以在命令中添加对参数的支持,然后使用它来接受用户的输入以相应地检查不同的国家/地区。
您的解决方案将是:
bot.on('message', msg => {
const prefix = '!';
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'corona') {
if (!args[0]) return message.reply('you need to enter a country name')
const url = `https://coronavirus-19-api.herokuapp.com/countries/${args[0]}`
req(url, function(err, response, body){
if(err) return msg.reply("err")
body = JSON.parse(body)
msg.reply(`Corona Fälle für: + \*\*${body.cases}\*\*\ ` ,)
}
,)}