const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); const { USPS_USERID } = process.env; module.exports = class USPSTrackingCommand extends Command { constructor(client) { super(client, { name: 'usps-tracking', aliases: ['usps-track', 'usps'], group: 'search', memberName: 'usps-tracking', description: 'Gets tracking information for a package shipped via USPS.', credit: [ { name: 'USPS', url: 'https://www.usps.com/' } ], args: [ { key: 'id', label: 'tracking id', prompt: 'What is the tracking ID of the package you would like to track?', type: 'string', validate: id => /^[0-9A-Z]+$/i.test(id) } ] }); } async run(msg, { id }) { try { const summary = await this.fetchSummary(id); if (!summary) return msg.say('A status update is not yet available on your package. Check back soon.'); return msg.say(stripIndents` **Tracking info for ${id}:** ${summary} More Info: `); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } async fetchSummary(id) { const { text } = await request .get('https://secure.shippingapis.com/ShippingApi.dll') .query({ API: 'TrackV2', XML: `` }); if (text.includes('-2147219283')) return null; if (text.includes('')) throw new Error(text.match(/(.+)<\/Description>/i)[1].trim()); const summary = text.match(/(.+)<\/TrackSummary>/i)[1].trim(); return summary; } };