From 09f7c0e93a8042b19915a67a14643fe6fd282a88 Mon Sep 17 00:00:00 2001
From: loicbersier <loic.bersier1@gmail.com>
Date: Mon, 30 Sep 2019 21:07:51 +0200
Subject: [PATCH] Convert vid to gif

---
 commands/utility/vid2gif.js | 58 +++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 commands/utility/vid2gif.js

diff --git a/commands/utility/vid2gif.js b/commands/utility/vid2gif.js
new file mode 100644
index 00000000..45c448fd
--- /dev/null
+++ b/commands/utility/vid2gif.js
@@ -0,0 +1,58 @@
+const { Command } = require('discord-akairo');
+const fs = require('fs');
+const os = require('os');
+const util = require('util');
+const exec = util.promisify(require('child_process').exec);
+const superagent = require('superagent');
+
+class vid2giftCommand extends Command {
+	constructor() {
+		super('vid2gift', {
+			aliases: ['vid2gif', 'v2g'],
+			category: 'utility',
+			args: [
+				{
+					id: 'vid',
+					type: 'string'
+				}
+			],
+			description: {
+				content: 'Transform video into gif',
+				usage: '[link to video]',
+				examples: ['']
+			}
+		});
+	}
+
+	async exec(message, args) {
+		let vid = args.vid;
+
+		if (!vid) {
+			return message.channel.send('I need a video to do that!');
+		} else if (vid) {
+
+			const { body: buffer } = await superagent.get(vid).catch(() => {
+				return message.channel.send('An error as occured, please try again');
+			});
+
+			fs.writeFile(`${os.tmpdir()}/${message.id}v2g`, buffer, () => {
+				exec(`ffmpeg -i ${os.tmpdir()}/${message.id}v2g ${os.tmpdir()}/${message.id}v2g.gif -hide_banner`)
+					.then(() => {
+						return message.channel.send({files: [`${os.tmpdir()}/${message.id}v2g.gif`]})
+							.catch(err => {
+								console.error(err);
+								return message.channel.send('Could not send the file! Perhaps the file is too big?');
+							});
+					})
+					.catch(err => {
+						console.error(err);
+						return message.channel.send('There was an error during conversion! maybe try with another file type?');
+					});
+			});
+
+		}
+
+	}
+}
+
+module.exports = vid2giftCommand;
\ No newline at end of file