From 1942e9ffb2c63736a130d9056e39674cf323969e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 8 Jun 2020 11:34:19 -0400 Subject: [PATCH] URL Shorten Command --- README.md | 5 +++- commands/edit-text/shorten-url.js | 47 +++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 commands/edit-text/shorten-url.js diff --git a/README.md b/README.md index ece39bc2..39310f9e 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 461 +Total: 462 ### Utility: @@ -679,6 +679,7 @@ Total: 461 * **sha-1:** Creates a hash of text with the SHA-1 algorithm. * **sha-256:** Creates a hash of text with the SHA-256 algorithm. * **ship-name:** Creates a ship name from two names. +* **shorten-url:** Shortens a URL using bit.ly. * **shuffle:** Shuffles text. * **snake-speak:** Convertsssss text to sssssnake ssssspeak. * **spoiler-letter:** Sends text with each and every character as an individual spoiler. @@ -838,6 +839,8 @@ here. * dislike ([Image, Original "Fallout" Game](https://fallout.bethesda.net/en/)) * like ([Image, Original "Fallout" Game](https://fallout.bethesda.net/en/)) * skyrim-skill ([Image, Original "The Elder Scrolls V: Skyrim" Game](https://elderscrolls.bethesda.net/en/skyrim)) +- [Bitly](https://bitly.com/) + * shorten-url ([API](https://dev.bitly.com/v4_documentation.html)) - [Bob Ross](https://www.bobross.com/) * bob-ross (Himself) - [Bowserinator](https://github.com/Bowserinator/) diff --git a/commands/edit-text/shorten-url.js b/commands/edit-text/shorten-url.js new file mode 100644 index 00000000..61abeed0 --- /dev/null +++ b/commands/edit-text/shorten-url.js @@ -0,0 +1,47 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const { BITLY_KEY } = process.env; + +module.exports = class ShortenUrlCommand extends Command { + constructor(client) { + super(client, { + name: 'shorten-url', + aliases: ['short-url', 'bit-ly', 'bit.ly', 'url-shorten', 'url-short'], + group: 'edit-text', + memberName: 'shorten-url', + description: 'Shortens a URL using bit.ly.', + credit: [ + { + name: 'Bitly', + url: 'https://bitly.com/', + reason: 'API', + reasonURL: 'https://dev.bitly.com/v4_documentation.html' + } + ], + args: [ + { + key: 'url', + prompt: 'What url would you like to shorten?', + type: 'string', + validate: url => { + if (encodeURI(url).length > 2083) return 'Your URL is too long.'; + return true; + } + } + ] + }); + } + + async run(msg, { url }) { + try { + const { body } = await request + .post('https://api-ssl.bitly.com/v4/shorten') + .query({ long_url: url }) + .set({ Authorization: `Bearer ${BITLY_KEY}` }); + return msg.say(body.link); + } catch (err) { + if (err.status === 400) return msg.reply('You provided an invalid URL. Please try again.'); + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 817195f4..5d44c0f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.6.0", + "version": "116.7.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {