diff --git a/.env.example b/.env.example index 4f1f885b..6660faa0 100644 --- a/.env.example +++ b/.env.example @@ -42,6 +42,7 @@ TMDB_KEY= TUMBLR_KEY= TWITTER_KEY= TWITTER_SECRET= +USPS_USERID= WATTPAD_KEY= WEBSTER_KEY= WHATANIME_KEY= diff --git a/README.md b/README.md index 47d72178..9e746512 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/commands/search/usps-tracking.js b/commands/search/usps-tracking.js new file mode 100644 index 00000000..4daa7786 --- /dev/null +++ b/commands/search/usps-tracking.js @@ -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: `` + }); + 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; + } +}; diff --git a/package.json b/package.json index 19a45947..ee14645a 100644 --- a/package.json +++ b/package.json @@ -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",