diff --git a/commands/analyze/is-it-down.js b/commands/analyze/is-it-down.js index 845bea08..ffd0d255 100644 --- a/commands/analyze/is-it-down.js +++ b/commands/analyze/is-it-down.js @@ -1,7 +1,5 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); -const { URL } = require('url'); -const validURL = require('valid-url'); module.exports = class IsItDownCommand extends Command { constructor(client) { @@ -20,21 +18,19 @@ module.exports = class IsItDownCommand extends Command { ], args: [ { - key: 'site', + key: 'url', prompt: 'What URL do you want to test?', - type: 'string', - validate: site => Boolean(validURL.isWebUri(site)) + type: 'url' } ] }); } - async run(msg, { site }) { - const parsed = new URL(site); + async run(msg, { url }) { try { const { text } = await request .post('https://www.isitdownrightnow.com/check.php') - .query({ domain: parsed.host }); + .query({ domain: url.host }); const down = text.includes('div class="statusdown"'); if (!down) return msg.reply('👍 This site is up and running.'); return msg.reply('👎 Looks like this site is down for everyone...'); diff --git a/commands/other/screenshot.js b/commands/other/screenshot.js index 5c6c76f7..5649fb33 100644 --- a/commands/other/screenshot.js +++ b/commands/other/screenshot.js @@ -1,7 +1,5 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); -const { URL } = require('url'); -const validURL = require('valid-url'); module.exports = class ScreenshotCommand extends Command { constructor(client) { @@ -21,22 +19,20 @@ module.exports = class ScreenshotCommand extends Command { ], args: [ { - key: 'site', + key: 'url', prompt: 'What webpage do you want to take a screenshot of?', - type: 'string', - validate: site => Boolean(validURL.isWebUri(site)) + type: 'url' } ] }); } - async run(msg, { site }) { + async run(msg, { url }) { try { - const parsed = new URL(site); - if (!msg.channel.nsfw && this.client.adultSiteList.includes(parsed.host)) { + if (!msg.channel.nsfw && this.client.adultSiteList.includes(url.host)) { return msg.reply('This site is NSFW.'); } - const { body } = await request.get(`https://image.thum.io/get/width/1920/crop/675/noanimate/${site}`); + const { body } = await request.get(`https://image.thum.io/get/width/1920/crop/675/noanimate/${url.href}`); return msg.say({ files: [{ attachment: body, name: 'screenshot.png' }] }); } catch (err) { if (err.status === 404) return msg.say('Could not find any results. Invalid URL?'); diff --git a/types/url.js b/types/url.js new file mode 100644 index 00000000..77c1afac --- /dev/null +++ b/types/url.js @@ -0,0 +1,17 @@ +const { ArgumentType } = require('discord.js-commando'); +const { URL } = require('url'); +const validURL = require('valid-url'); + +module.exports = class UrlType extends ArgumentType { + constructor(client) { + super(client, 'url'); + } + + validate(value) { + return Boolean(validURL.isWebUri(value)); + } + + async parse(value) { + return new URL(value); + } +};