From 81a23658f91e22c0d06c38d28c6ae675f2cb1a99 Mon Sep 17 00:00:00 2001
From: loicbersier <loic.bersier1@gmail.com>
Date: Thu, 16 Jul 2020 09:22:17 +0200
Subject: [PATCH] Use new attachment utils

---
 commands/fun/asciify.js     | 28 ++++++++++++++++++++++++----
 commands/fun/audio2image.js | 15 ++++++++-------
 commands/fun/image2audio.js | 15 ++++++++-------
 commands/fun/midify.js      | 15 ++++++++-------
 commands/fun/ytp.js         | 24 ++++++++++++++++--------
 commands/images/autocrop.js | 19 +++++++------------
 commands/images/blur.js     | 18 +++++++++---------
 commands/images/gaussian.js | 16 +++++++++-------
 commands/images/jpegify.js  | 15 +++++++--------
 commands/images/meme.js     | 16 +++++++++-------
 commands/images/mirror.js   | 15 +++++++--------
 commands/images/paint.js    | 13 +++++++------
 commands/images/rotate.js   | 19 +++++++++++--------
 13 files changed, 130 insertions(+), 98 deletions(-)

diff --git a/commands/fun/asciify.js b/commands/fun/asciify.js
index d43303cd..7908c1ef 100644
--- a/commands/fun/asciify.js
+++ b/commands/fun/asciify.js
@@ -1,4 +1,7 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
+const os = require('os');
+const fs = require('fs');
 const asciify = require('asciify-image');
 
 let options = {
@@ -14,6 +17,12 @@ class asciifyCommand extends Command {
 			aliases: ['asciify'],
 			category: 'fun',
 			clientPermissions: ['SEND_MESSAGES'],
+			args: [
+				{
+					id: 'link',
+					type: 'url',
+				}
+			],
 			cooldown: 600000,
 			ratelimit: 2,
 			description: {
@@ -24,14 +33,25 @@ class asciifyCommand extends Command {
 		});
 	}
 
-	async exec(message) {
-		let Attachment = (message.attachments).array();
+	async exec(message, args) {
+		let url;
 
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
-		return asciify(Attachment[0].url, options, function (err, asciified) {
+		return asciify(url, options, function (err, asciified) {
 			if (err) throw err;   
 			// Print to console
-			return message.channel.send(asciified,  { split: true, code: true });
+			fs.writeFile(`${os.tmpdir()}/${message.id}ascii.txt`, asciified, function (err) {
+				if (err) {
+					console.log(err);
+				}
+
+				return message.channel.send({files: [`${os.tmpdir()}/${message.id}ascii.txt`]});
+			});
+			//return message.channel.send(asciified,  { split: true, code: true });
 		});
 	}
 }
diff --git a/commands/fun/audio2image.js b/commands/fun/audio2image.js
index 2559afd1..7a7142a6 100644
--- a/commands/fun/audio2image.js
+++ b/commands/fun/audio2image.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const ffmpeg = require('fluent-ffmpeg');
 const fetch = require('node-fetch');
 const fs = require('fs');
@@ -19,7 +20,7 @@ class audio2imageCommand extends Command {
 				},
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 				}
 			],
 			description: {
@@ -31,12 +32,12 @@ class audio2imageCommand extends Command {
 	}
 
 	async exec(message, args) {
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		let url;
+
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
 
diff --git a/commands/fun/image2audio.js b/commands/fun/image2audio.js
index 5f898714..4fb9171c 100644
--- a/commands/fun/image2audio.js
+++ b/commands/fun/image2audio.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const ffmpeg = require('fluent-ffmpeg');
 const fetch = require('node-fetch');
 const fs = require('fs');
@@ -13,7 +14,7 @@ class image2audioCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 				},
 				{
 					id: 'wav',
@@ -30,12 +31,12 @@ class image2audioCommand extends Command {
 	}
 
 	async exec(message, args) {
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		let url;
+
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
 
diff --git a/commands/fun/midify.js b/commands/fun/midify.js
index e1907674..0ce592ae 100644
--- a/commands/fun/midify.js
+++ b/commands/fun/midify.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const util = require('util');
 const exec = util.promisify(require('child_process').exec);
 const downloader = require('../../utils/download');
@@ -15,7 +16,7 @@ class midifyCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 					match: 'rest'
 				},
 				{
@@ -43,12 +44,12 @@ class midifyCommand extends Command {
 	}
 
 	async exec(message, args) {
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		let url;
+
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		let input = `${os.tmpdir()}/${message.id}`;
 		let input2 = `${os.tmpdir()}/${message.id}.wav`;
diff --git a/commands/fun/ytp.js b/commands/fun/ytp.js
index 5a7ef1a0..7cb250d3 100644
--- a/commands/fun/ytp.js
+++ b/commands/fun/ytp.js
@@ -2,6 +2,7 @@ const { Command } = require('discord-akairo');
 const YTPGenerator = require('ytpplus-node');
 const os = require('os');
 const fs = require('fs');
+const attachment = require('../../utils/attachment');
 const downloader = require('../../utils/download');
 const md5File = require('md5-file');
 const ytpHash = require('../../models').ytpHash;
@@ -99,11 +100,18 @@ class ytpCommand extends Command {
 				},
 				{
 					id: 'link',
-					type: 'string'
+					type: 'url',
+					prompt: {
+						start: 'Please send the URL of which video you want to download. Say `cancel` to stop the command',
+						retry: 'Please send a valid URL of the video you want to download. Say `cancel` to stop the command',
+						optional: true,
+					},
+					unordered: true
 				},
 				{
 					id: 'max',
-					type: 'string'
+					type: 'string',
+					unordered: true
 				}
 			],
 			description: {
@@ -128,12 +136,12 @@ class ytpCommand extends Command {
 
 		if (args.add) {
 			let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
-			let Attachment = (message.attachments).array();
-			let url = args.link;
-			// Get attachment link
-			if (Attachment[0] && !args.link) {
-				url = Attachment[0].url;
-			}
+			let url;
+
+			if (args.link)
+				url = args.link.href;
+			else
+				url = await attachment(message);
 
 			if (url) {
 				return downloader(url, ['--format=mp4'], `./asset/ytp/userVid/${message.id}.mp4`)
diff --git a/commands/images/autocrop.js b/commands/images/autocrop.js
index 57a39161..c275c386 100644
--- a/commands/images/autocrop.js
+++ b/commands/images/autocrop.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,7 +12,7 @@ class autocropCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 				}
 			],
 			description: {
@@ -24,18 +25,12 @@ class autocropCommand extends Command {
 
 	async exec(message, args) {
 		let output = `${os.tmpdir()}/cropped${message.id}.jpg`;
+		let url;
 
-
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
-
-		if (!url) {
-			return message.channel.send('You need an image to use this command!');
-		}
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
 
diff --git a/commands/images/blur.js b/commands/images/blur.js
index 9797e284..3566fdd0 100644
--- a/commands/images/blur.js
+++ b/commands/images/blur.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,12 +12,13 @@ class blurCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
-
+					type: 'url',
+					unordered: true
 				},
 				{
 					id: 'radius',
 					type: 'integer',
+					unordered: true
 				}
 			],
 			description: {
@@ -31,14 +33,12 @@ class blurCommand extends Command {
 		let output = `${os.tmpdir()}/blurred${message.id}.jpg`;
 
 		if (!args.radius) args.radius = 10;
+		let url;
 
-
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');
diff --git a/commands/images/gaussian.js b/commands/images/gaussian.js
index 47b4f4a9..e5c3ddec 100644
--- a/commands/images/gaussian.js
+++ b/commands/images/gaussian.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,11 +12,13 @@ class gaussianCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
+					unordered: true
 				},
 				{
 					id: 'radius',
 					type: 'integer',
+					unordered: true
 				}
 			],
 			description: {
@@ -31,13 +34,12 @@ class gaussianCommand extends Command {
 
 		if (!args.radius) args.radius = 10;
 
+		let url;
 
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');
diff --git a/commands/images/jpegify.js b/commands/images/jpegify.js
index ca6ac73d..b6bc5e8c 100644
--- a/commands/images/jpegify.js
+++ b/commands/images/jpegify.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,7 +12,7 @@ class jpegifyCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 				}
 			],
 			description: {
@@ -24,14 +25,12 @@ class jpegifyCommand extends Command {
 
 	async exec(message, args) {
 		let output = `${os.tmpdir()}/jpegified${message.id}.jpg`;
+		let url;
 
-
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');
diff --git a/commands/images/meme.js b/commands/images/meme.js
index 501aec3f..50babc4e 100644
--- a/commands/images/meme.js
+++ b/commands/images/meme.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const gm = require('gm').subClass({imageMagick: true});
 const os = require('os');
 const fetch = require('node-fetch');
@@ -13,10 +14,7 @@ class memeCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					prompt: {
-						start: 'Please input a link to use, say `cancel` to stop the command'
-					},
-					type: 'string',
+					type: 'url',
 				},
 				{
 					id: 'message',
@@ -43,14 +41,18 @@ class memeCommand extends Command {
 	async exec(message, args) {
 		let options = args.message.trim().split('|');
 
+		let url;
+
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
+
 		if (options[0] == undefined)
 			options[0] = '';
 		else if (options[1] == undefined)
 			options[1] = '';
 
-		let url = args.link;
-
-
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');
 		}
diff --git a/commands/images/mirror.js b/commands/images/mirror.js
index 793c7c5b..3104ee33 100644
--- a/commands/images/mirror.js
+++ b/commands/images/mirror.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,7 +12,7 @@ class mirrorCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
 				}
 			],
 			description: {
@@ -24,14 +25,12 @@ class mirrorCommand extends Command {
 
 	async exec(message, args) {
 		let output = `${os.tmpdir()}/mirrored${message.id}.jpg`;
+		let url;
 
-
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
 
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');
diff --git a/commands/images/paint.js b/commands/images/paint.js
index 0d73e7f7..43eded5e 100644
--- a/commands/images/paint.js
+++ b/commands/images/paint.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const { createCanvas, loadImage } = require('canvas');
 const superagent = require('superagent');
 
@@ -19,14 +20,14 @@ class paintCommand extends Command {
 	}
 
 	async exec(message, args) {
-		let Attachment = (message.attachments).array();
 		let image = args.image;
-		if (!Attachment[0] && !image)
-			image = message.author.displayAvatarURL().replace('webp', 'png');
-		else if(Attachment[0] && Attachment[0].url.endsWith('gif'))
-			return message.channel.send('Gif dosent work, sorry');
+		if (!image)
+			image = await attachment(message);
 		else if (!image)
-			image = Attachment[0].url;
+			image = message.author.displayAvatarURL().replace('webp', 'png');
+		else if(image.endsWith('gif'))
+			return message.channel.send('Gif dosent work, sorry');
+
 		
 		message.channel.send('Processing <a:loadingmin:527579785212329984>')
 			.then(loadingmsg => loadingmsg.delete(1000));
diff --git a/commands/images/rotate.js b/commands/images/rotate.js
index 74391751..232a4d43 100644
--- a/commands/images/rotate.js
+++ b/commands/images/rotate.js
@@ -1,4 +1,5 @@
 const { Command } = require('discord-akairo');
+const attachment = require('../../utils/attachment');
 const jimp = require('jimp');
 const os = require('os');
 
@@ -11,7 +12,8 @@ class rotateCommand extends Command {
 			args: [
 				{
 					id: 'link',
-					type: 'string',
+					type: 'url',
+					unordered: true
 				},
 				{
 					id: 'rotate',
@@ -19,7 +21,8 @@ class rotateCommand extends Command {
 					prompt: {
 						start: 'Please enter the number of degrees you want to rotate.',
 						retry: 'This doesn\'t look like a number to me, please try again.'
-					}
+					},
+					unordered: true
 				}
 			],
 			description: {
@@ -34,12 +37,12 @@ class rotateCommand extends Command {
 		let output = `${os.tmpdir()}/rotated${message.id}.jpg`;
 
 
-		let Attachment = (message.attachments).array();
-		let url = args.link;
-		// Get attachment link
-		if (Attachment[0] && !args.link) {
-			url = Attachment[0].url;
-		}
+		let url;
+		if (args.link)
+			url = args.link.href;
+		else
+			url = await attachment(message);
+
 
 		if (!url) {
 			return message.channel.send('You need an image to use this command!');