From 8af53ba5223d4449854b9fe0331f6bd91e746474 Mon Sep 17 00:00:00 2001 From: Jeremiah <43737601+bullfrog0981@users.noreply.github.com> Date: Sun, 8 Dec 2019 22:02:00 -0500 Subject: [PATCH] Allow urls to be used as images (#38) * Allow urls ending to be used as images This will allow commands that use the image argument type to accept urls that end in PNG, JPG, or GIF * Fix lint * Fix lint properly * Remove useless else/if --- types/image.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/types/image.js b/types/image.js index c2fcea28..5e77f389 100644 --- a/types/image.js +++ b/types/image.js @@ -1,12 +1,13 @@ const { ArgumentType } = require('discord.js-commando'); const fileTypeRe = /\.(jpe?g|png|gif)$/i; +const request = require('node-superfetch'); module.exports = class ImageArgumentType extends ArgumentType { constructor(client) { super(client, 'image'); } - validate(value, msg, arg) { + async validate(value, msg, arg) { const attachment = msg.attachments.first(); if (attachment) { if (!attachment.height || !attachment.width) return false; @@ -14,12 +15,21 @@ module.exports = class ImageArgumentType extends ArgumentType { if (!fileTypeRe.test(attachment.name)) return 'Please only send PNG, JPG, or GIF format images.'; return true; } + if (fileTypeRe.test(value.toLowerCase())) { + try { + await request.get(value); + return true; + } catch (err) { + return false; + } + } return this.client.registry.types.get('user').validate(value, msg, arg); } async parse(value, msg, arg) { const attachment = msg.attachments.first(); if (attachment) return attachment.url; + if (fileTypeRe.test(value.toLowerCase())) return value; const user = await this.client.registry.types.get('user').parse(value, msg, arg); return user.displayAvatarURL({ format: 'png', size: 512 }); }