简体   繁体   中英

node.js / discord.js API problem (superagent)

Hello I'm using an API that shows me some information when I provide an ID, and it works fine but when the ID is invalid I get {"status":"nok", "reason": "Unable to get information."} and I want to make the bot say something like: Invalid ID when the user provide a wrong ID in the command. But instead the bot crashes.

Here's my code:

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"
}

Please let me know where my mistake is, I'm having a really hard time with this...

Please try using try catch to catch the runtime Exceptions you want.

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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM