【问题标题】:node.js / discord.js API problem (superagent)node.js / discord.js API 问题(超级代理)
【发布时间】:2020-04-30 00:41:22
【问题描述】:

您好,我正在使用一个 API,当我提供 ID 时它会向我显示一些信息,它工作正常,但是当 ID 无效时,我得到 {"status":"nok", "reason": "Unable to get information."},我想让机器人说类似:当用户在命令中提供错误 ID 时,ID 无效。但相反,机器人崩溃了。

这是我的代码:

const Discord = require("discord.js");
const superagent = require("superagent");

module.exports.run = async (bot, message, args) => {

    let itemid = args.shift().toLowerCase();

    let {body} = await superagent
    .get(`https://us.api.blizzard.com/wow/item/${itemid}?locale=en_US&access_token=MYTOKEN`);

    let response2 = await superagent.get(`https://us.api.blizzard.com/data/wow/item/${itemid}?namespace=static-us&locale=en_US&access_token=MYTOKEN`);
    let body2 = response2.body;

    let response3 = await superagent.get(`https://us.api.blizzard.com/data/wow/media/item/${itemid}?namespace=static-us&locale=en_US&access_token=MYTOKEN`);
    let body3 = response3.body;

    let embed = new Discord.RichEmbed()
    .setColor('RANDOM')
    .setTitle('Item Lookup')
    .setThumbnail(body3.assets[0].value)
    .addField('Item Name:', body.name)
    .addField('Type:', `${body2.inventory_type.name} ${body2.item_subclass.name}`, true)
    .addField('Source:', body.itemSource.sourceType, true)
    .addField('Item ID:', body.id)
    .addField('Display ID:', body.displayInfoId)
    .addField('Item Level:', body.itemLevel)
    .addField('Required Level:', body.requiredLevel)

    if (body.itemSpells[0]) {
        embed.addField(`Effect**(${body.itemSpells[0].trigger})**:`, body.itemSpells[0].scaledDescription)
      } else {
        embed.addField(`Effect:`, `This item doesn't have any effects.`)
      }

    if (body.bonusStats[1].stat == 7) {
        embed.addField(`Stamina:`, `${body.bonusStats[1].amount}`, true)
    } else {

    }

    if (body.bonusStats[0].stat == 74) {
        embed.addField(`Strength/Intellect:`, `${body.bonusStats[0].amount}`, true)
    } else {

    }

    if (!body.id) {
        message.channel.send(`${body.reason}`)
    }

    message.channel.send(embed);

}

module.exports.help = {
    name: "getinfo"
}

请让我知道我的错误在哪里,我很难做到这一点......

【问题讨论】:

  • 你尝试过使用 try catch 吗?
  • 我还在学习过程中,你能告诉我怎么做吗?谢谢!

标签: javascript node.js discord.js


【解决方案1】:

请尝试使用 try catch 捕获您想要的运行时异常。

const Discord = require("discord.js");
const superagent = require("superagent");

module.exports.run = async (bot, message, args) => {

    let itemid = args.shift().toLowerCase();
    try{

        let {body} = await superagent
        .get(`https://us.api.blizzard.com/wow/item/${itemid}?locale=en_US&access_token=MYTOKEN`);

        try{
            let response2 = await superagent.get(`https://us.api.blizzard.com/data/wow/item/${itemid}?namespace=static-us&locale=en_US&access_token=MYTOKEN`);
            let body2 = response2.body;

            let response3 = await superagent.get(`https://us.api.blizzard.com/data/wow/media/item/${itemid}?namespace=static-us&locale=en_US&access_token=MYTOKEN`);
            let body3 = response3.body;
        }catch(error){
            //console.log("Id could be Invalid. Please check");
        }

        let embed = new Discord.RichEmbed()
        .setColor('RANDOM')
        .setTitle('Item Lookup')
        .setThumbnail(body3.assets[0].value)
        .addField('Item Name:', body.name)
        .addField('Type:', `${body2.inventory_type.name} ${body2.item_subclass.name}`, true)
        .addField('Source:', body.itemSource.sourceType, true)
        .addField('Item ID:', body.id)
        .addField('Display ID:', body.displayInfoId)
        .addField('Item Level:', body.itemLevel)
        .addField('Required Level:', body.requiredLevel)

        if (body.itemSpells[0]) {
            embed.addField(`Effect**(${body.itemSpells[0].trigger})**:`, body.itemSpells[0].scaledDescription)
        } else {
            embed.addField(`Effect:`, `This item doesn't have any effects.`)
        }

        if (body.bonusStats[1].stat == 7) {
            embed.addField(`Stamina:`, `${body.bonusStats[1].amount}`, true)
        } else {

        }

        if (body.bonusStats[0].stat == 74) {
            embed.addField(`Strength/Intellect:`, `${body.bonusStats[0].amount}`, true)
        } else {

        }

        if (!body.id) {
            message.channel.send(`${body.reason}`)
        }

        message.channel.send(embed);
    }
    catch(error){
        //console.log(error); //(or) you can throw error like the below
        //throw new error;
    }

}

module.exports.help = {
    name: "getinfo"
}

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 2017-03-22
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多