USPS Tracking Command

This commit is contained in:
Dragon Fire
2019-08-27 12:04:19 -04:00
parent 35b2466f8c
commit b6f7b3a636
4 changed files with 63 additions and 4 deletions
+1
View File
@@ -42,6 +42,7 @@ TMDB_KEY=
TUMBLR_KEY=
TWITTER_KEY=
TWITTER_SECRET=
USPS_USERID=
WATTPAD_KEY=
WEBSTER_KEY=
WHATANIME_KEY=
+2 -1
View File
@@ -59,7 +59,7 @@ Xiao is a Discord bot coded in JavaScript with
* [Storyteller](https://github.com/dragonfire535/storyteller) is a Mafia bot made for Discord's 2019 Hack Week, whose features were originally built into Xiao.
* [Meme Poster](https://github.com/dragonfire535/meme-poster) is a meme-posting webhook, inspired by an idea that started as part of Xiao.
## Commands (347)
## Commands (348)
### Utility:
* **eval:** Executes JavaScript code.
@@ -228,6 +228,7 @@ Xiao is a Discord bot coded in JavaScript with
* **tv-show:** Searches TMDB for your query, getting TV show results.
* **twitter:** Responds with information on a Twitter user.
* **urban:** Defines a word, but with Urban Dictionary.
* **usps-tracking:** Gets tracking information for a package shipped via USPS.
* **visual-novel:** Responds with information on a Visual Novel.
* **vocadb:** Searches VocaDB for your query.
* **wattpad:** Searches Wattpad for your query.
+57
View File
@@ -0,0 +1,57 @@
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-9]+$/.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}
`);
} 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: `<TrackRequest USERID="${USPS_USERID}"><TrackID ID="${id}"></TrackID></TrackRequest>`
});
if (text.includes('<Number>-2147219283</Number>')) return null;
if (text.includes('<Error>')) throw new Error(text.match(/<Description>(.+)<\/Description>/i)[1].trim());
const summary = text.match(/<TrackSummary>(.+)<\/TrackSummary>/i)[1].trim();
return summary;
}
};
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "107.6.0",
"version": "107.7.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
@@ -37,9 +37,9 @@
"custom-translate": "^2.2.8",
"discord.js": "github:discordjs/discord.js",
"discord.js-commando": "github:discordjs/Commando",
"dotenv": "^8.0.0",
"dotenv": "^8.1.0",
"gifencoder": "^2.0.1",
"mathjs": "^6.0.4",
"mathjs": "^6.1.0",
"moment": "^2.24.0",
"moment-duration-format": "^2.3.2",
"moment-timezone": "^0.5.26",