From 777b86e48050552384c7dfd3704b09e74f7fec9f Mon Sep 17 00:00:00 2001 From: TheEssem Date: Tue, 18 Feb 2020 14:44:39 -0600 Subject: [PATCH] Added support for "gifv"s, fixed embed issue with image --- commands/9gag.js | 7 ++++--- commands/bandicam.js | 8 ++++---- commands/blur.js | 6 +++--- commands/blurple.js | 6 +++--- commands/caption.js | 13 ++++++------ commands/circle.js | 6 +++--- commands/deviantart.js | 8 ++++---- commands/explode.js | 6 +++--- commands/flip.js | 6 +++--- commands/flop.js | 6 +++--- commands/funky.js | 8 ++++---- commands/gamexplain.js | 9 +++----- commands/haah.js | 8 ++++---- commands/hooh.js | 8 ++++---- commands/hypercam.js | 8 ++++---- commands/ifunny.js | 8 ++++---- commands/image.js | 2 +- commands/implode.js | 6 +++--- commands/invert.js | 6 +++--- commands/jpeg.js | 2 +- commands/leak.js | 9 +++----- commands/magik.js | 4 ++-- commands/meme.js | 6 +++--- commands/memecenter.js | 8 ++++---- commands/motivate.js | 15 ++++++-------- commands/scott.js | 9 +++----- commands/sharpen.js | 6 +++--- commands/shutterstock.js | 8 ++++---- commands/speed.js | 19 ++++++++--------- commands/spin.js | 4 ++-- commands/stretch.js | 6 +++--- commands/swirl.js | 6 +++--- commands/tile.js | 8 ++++---- commands/trump.js | 9 +++----- commands/waaw.js | 8 ++++---- commands/wall.js | 8 ++++---- commands/wdt.js | 9 +++----- commands/wide.js | 8 ++++---- commands/woow.js | 8 ++++---- messages.json | 3 ++- utils/imagedetect.js | 44 +++++++++++++++++++++++++--------------- 41 files changed, 167 insertions(+), 175 deletions(-) diff --git a/commands/9gag.js b/commands/9gag.js index 99fceb0..ff3d71d 100644 --- a/commands/9gag.js +++ b/commands/9gag.js @@ -1,3 +1,4 @@ +const fs = require("fs"); const gm = require("gm").subClass({ imageMagick: true }); @@ -8,10 +9,10 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a 9GAG watermark!`; const watermark = "./assets/images/9gag.png"; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("East").out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("East").out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `9gag.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `9gag.${image.outputType}` }); }; diff --git a/commands/bandicam.js b/commands/bandicam.js index c899150..bdb5859 100644 --- a/commands/bandicam.js +++ b/commands/bandicam.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a Bandicam watermark!`; const watermark = "./assets/images/bandicam.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("North").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("North").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `bandicam.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `bandicam.${image.outputType}` }); }); }; diff --git a/commands/blur.js b/commands/blur.js index e91b3c3..a466320 100644 --- a/commands/blur.js +++ b/commands/blur.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to blur!`; - const command = gm(image.data).blur(10); + const command = gm(image.path).blur(10); return message.channel.createMessage("", { - file: await gmToBuffer(command), - name: `blur.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `blur.${image.outputType}` }); }; diff --git a/commands/blurple.js b/commands/blurple.js index 00bfc40..475bc1b 100644 --- a/commands/blurple.js +++ b/commands/blurple.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make blurple!`; - const data = gm(image.data).threshold(75, true).out("+level-colors").out("\"#7289DA\",white"); + const data = gm(image.path).threshold(75, true).out("+level-colors").out("\"#7289DA\",white"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `blurple.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `blurple.${image.outputType}` }); }; diff --git a/commands/caption.js b/commands/caption.js index 98049a3..0df0af3 100644 --- a/commands/caption.js +++ b/commands/caption.js @@ -1,17 +1,15 @@ -const fs = require("fs"); const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); +//const upload = require("../utils/upload.js"); exports.run = async (message, args) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image/GIF to add a caption!`; if (args.length === 0) return `${message.author.mention}, you need to provide some text to add a caption!`; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); - require("util").promisify(fs.writeFile)(path, image.data); - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; const command = gm().out("-size", `${size.width}x`).background("white").fill("black").font("./assets/caption.otf", size.width / 10).gravity("Center").out(`caption:${args.join(" ")}`); const output = await gmToBuffer(command, "png"); @@ -19,12 +17,13 @@ exports.run = async (message, args) => { if (error) throw error; gm(output).gravity("Center").trim().out("+repage").extent(size.width, size2.height + (size.width / 10)).stream(async (error, output2) => { if (error) throw error; - const command3 = gm(output2).out("-alpha", "set").background("none").out("(").out(path).out("-coalesce").out(")").out("-set", "page", "%[fx:u.w]x%[fx:u.h+v.h]+%[fx:t?(u.w-v.w)/2:0]+%[fx:t?u.h:0]").out("-coalesce").out("null:").out("-insert", 1).out("-layers", "composite").out("-layers", "optimize"); - const outputFinal = await gmToBuffer(command3, image.type); + const command3 = gm(output2).out("-alpha", "set").background("none").out("(").out(image.path).out("-coalesce").out(")").colorspace("sRGB").out("-set", "page", "%[fx:u.w]x%[fx:u.h+v.h]+%[fx:t?(u.w-v.w)/2:0]+%[fx:t?u.h:0]").out("-coalesce").out("null:").out("-insert", 1).out("-layers", "composite").out("-layers", "optimize"); + const outputFinal = await gmToBuffer(command3, image.outputType); await processMessage.delete(); + //return upload(message, outputFinal, `caption.${image.type}`); return message.channel.createMessage("", { file: outputFinal, - name: `caption.${image.type}` + name: `caption.${image.outputType}` }); }); }); diff --git a/commands/circle.js b/commands/circle.js index 34d0e91..1bab266 100644 --- a/commands/circle.js +++ b/commands/circle.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add radial blur!`; - const data = gm(image.data).out("-radial-blur", 10); + const data = gm(image.path).coalesce().out("-radial-blur", 10); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `circle.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `circle.${image.outputType}` }); }; diff --git a/commands/deviantart.js b/commands/deviantart.js index 69c2964..bc3e497 100644 --- a/commands/deviantart.js +++ b/commands/deviantart.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a DeviantArt watermark!`; const watermark = "./assets/images/deviantart.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `deviantart.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `deviantart.${image.outputType}` }); }); }; diff --git a/commands/explode.js b/commands/explode.js index 7c846ac..4b731cf 100644 --- a/commands/explode.js +++ b/commands/explode.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to explode!`; - const data = gm(image.data).implode([-2]); + const data = gm(image.path).coalesce().implode([-2]); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `explode.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `explode.${image.outputType}` }); }; diff --git a/commands/flip.js b/commands/flip.js index e50e06f..3844247 100644 --- a/commands/flip.js +++ b/commands/flip.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flip!`; - const command = gm(image.data).flip(); + const command = gm(image.path).flip(); return message.channel.createMessage("", { - file: await gmToBuffer(command), - name: `flip.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `flip.${image.outputType}` }); }; diff --git a/commands/flop.js b/commands/flop.js index c0bf630..2f70b67 100644 --- a/commands/flop.js +++ b/commands/flop.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flop!`; - const command = gm(image.data).flop(); + const command = gm(image.path).flop(); return message.channel.createMessage("", { - file: await gmToBuffer(command), - name: `flop.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `flop.${image.outputType}` }); }; diff --git a/commands/funky.js b/commands/funky.js index b4270c7..f40a90e 100644 --- a/commands/funky.js +++ b/commands/funky.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add New Funky Mode!`; const watermark = "./assets/images/funky.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("NorthEast").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthEast").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `funky.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `funky.${image.outputType}` }); }); }; diff --git a/commands/gamexplain.js b/commands/gamexplain.js index 4f73cdc..8e697a5 100644 --- a/commands/gamexplain.js +++ b/commands/gamexplain.js @@ -2,19 +2,16 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fs = require("fs"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a GameXplain thumbnail meme!`; const template = "./assets/images/gamexplain.png"; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).background("white").out("null:").out("(").out(path).coalesce().out("-virtual-pixel", "transparent").resize("1181x571!").out(")").compose("over").gravity("Center").out("-geometry", "+0+40").out("-layers", "composite").out("-layers", "optimize"); + const command = gm(template).background("white").out("null:").out("(").out(image.path).coalesce().out("-virtual-pixel", "transparent").resize("1181x571!").out(")").compose("over").gravity("Center").out("-geometry", "+0+40").out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `gamexplain.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `gamexplain.${image.outputType}` }); }; diff --git a/commands/haah.js b/commands/haah.js index e36f5d1..11f6d9c 100644 --- a/commands/haah.js +++ b/commands/haah.js @@ -11,16 +11,16 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; - gm(image.data).size((error, size) => { + gm(image.path).size((error, size) => { if (error) throw error; - gm(image.data).coalesce().gravity("West").crop("50%", 0).out("+repage").write(data2, (error) => { + gm(image.path).coalesce().gravity("West").crop("50%", 0).out("+repage").write(data2, (error) => { if (error) throw error; gm(data2).flop().write(data, async (error) => { if (error) throw error; const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+${size.width / 2}+0`).out("-layers", "Composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `haah.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `haah.${image.outputType}` }); }); }); diff --git a/commands/hooh.js b/commands/hooh.js index b54f6fc..0aa3123 100644 --- a/commands/hooh.js +++ b/commands/hooh.js @@ -11,16 +11,16 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; - gm(image.data).size((error, size) => { + gm(image.path).size((error, size) => { if (error) throw error; - gm(image.data).coalesce().gravity("South").crop(0, "50%").out("+repage").write(data2, (error) => { + gm(image.path).coalesce().gravity("South").crop(0, "50%").out("+repage").write(data2, (error) => { if (error) throw error; gm(data2).flip().write(data, async (error) => { if (error) throw error; const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+0+${size.height / 2}`).out("-layers", "Composite").out("-layers", "Optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `hooh.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `hooh.${image.outputType}` }); }); }); diff --git a/commands/hypercam.js b/commands/hypercam.js index f816cfb..b0efe00 100644 --- a/commands/hypercam.js +++ b/commands/hypercam.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a Hypercam watermark!`; const watermark = "./assets/images/hypercam.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("NorthWest").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthWest").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `hypercam.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `hypercam.${image.outputType}` }); }); }; diff --git a/commands/ifunny.js b/commands/ifunny.js index a4b9aa2..1213a56 100644 --- a/commands/ifunny.js +++ b/commands/ifunny.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a iFunny watermark!`; const watermark = "./assets/images/ifunny.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("South").resize(size.width, null).out("-layers", "composite").out("-layers", "optimize"); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("South").resize(size.width, null).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `ifunny.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `ifunny.${image.outputType}` }); }); }; diff --git a/commands/image.js b/commands/image.js index 14b6119..342801f 100644 --- a/commands/image.js +++ b/commands/image.js @@ -17,7 +17,7 @@ exports.run = async (message, args) => { "title": "Search Results", "color": 16711680, "footer": { - "text": `Page ${i + 1} of ${images.length}` + "text": `Page ${i + 1} of ${images.items.length}` }, "image": { "url": value.link diff --git a/commands/implode.js b/commands/implode.js index b2cabc1..9fecdd0 100644 --- a/commands/implode.js +++ b/commands/implode.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to implode!`; - const data = gm(image.data).implode([1]); + const data = gm(image.path).implode([1]); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `implode.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `implode.${image.outputType}` }); }; diff --git a/commands/invert.js b/commands/invert.js index 29ec550..15d1d0c 100644 --- a/commands/invert.js +++ b/commands/invert.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to invert!`; - const data = gm(image.data).negative(); + const data = gm(image.path).negative(); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `invert.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `invert.${image.outputType}` }); }; diff --git a/commands/jpeg.js b/commands/jpeg.js index 35d26be..fff4861 100644 --- a/commands/jpeg.js +++ b/commands/jpeg.js @@ -7,7 +7,7 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add more JPEG!`; - const data = gm(image.data).setFormat("jpg").quality(1); + const data = gm(image.path).setFormat("jpg").quality(1); return message.channel.createMessage("", { file: await gmToBuffer(data), name: "jpeg.jpg" diff --git a/commands/leak.js b/commands/leak.js index 1727d43..800f44d 100644 --- a/commands/leak.js +++ b/commands/leak.js @@ -2,21 +2,18 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fs = require("fs"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Super Smash Bros. leak meme!`; - if (image.type === "gif") return `${message.author.mention}, this command doesn't work with GIFs!`; + if (image.type === "gif" || image.type === "mp4") return `${message.author.mention}, this command doesn't work with GIFs!`; const template = "./assets/images/leak.png"; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).out("-background").out("white").out("-gravity").out("Center").out("(").out("-clone").out("0").out("(").out(path).out("-virtual-pixel").out("white").out("-resize").out("640x360!").rotate("white", 15).out(")").out("-geometry").out("+450-200").out("-composite").out(")").out("+swap").out("-composite").out("-alpha").out("remove").out("-alpha").out("off"); + const command = gm(template).out("-background").out("white").out("-gravity").out("Center").out("(").out("-clone").out("0").out("(").out(image.path).out("-virtual-pixel").out("white").out("-resize").out("640x360!").rotate("white", 15).out(")").out("-geometry").out("+450-200").out("-composite").out(")").out("+swap").out("-composite").out("-alpha").out("remove").out("-alpha").out("off"); // const command = gm(template).out("-background", "white").gravity("Center").out("null:").out("(").out(path).out("-resize", "640x360!").out("-virtual-pixel", "white").rotate("white", 15).coalesce().geometry("+450-200").out(")").compose("over").out("-alpha", "remove").out("-alpha", "off").out("-layers", "composite"); return message.channel.createMessage("", { file: await gmToBuffer(command), - name: `leak.${image.type}` + name: `leak.${image.outputType}` }); }; diff --git a/commands/magik.js b/commands/magik.js index 44dce82..92b86fb 100644 --- a/commands/magik.js +++ b/commands/magik.js @@ -6,7 +6,7 @@ const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add some magik!`; - if (image.type === "gif") return `${message.author.mention}, this command doesn't work with GIFs!`; + if (image.type === "gif" || image.type === "mp4") return `${message.author.mention}, this command doesn't work with GIFs!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); gm(image.data).resize(800, 800).stream("miff", (error, stream) => { if (error) throw error; @@ -17,7 +17,7 @@ exports.run = async (message) => { await processMessage.delete(); return message.channel.createMessage("", { file: resultBuffer, - name: `magik.${image.type}` + name: `magik.${image.outputType}` }); }); }); diff --git a/commands/meme.js b/commands/meme.js index 48183eb..4599ca3 100644 --- a/commands/meme.js +++ b/commands/meme.js @@ -12,7 +12,7 @@ exports.run = async (message, args) => { const file = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const file2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; const file3 = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; - gm(image.data).out("-coalesce").resize(600, 600).noProfile().write(file, (error) => { + gm(image.path).coalesce().resize(600, 600).noProfile().write(file, (error) => { if (error) throw error; gm(file).size((error, size) => { if (error) throw error; @@ -22,8 +22,8 @@ exports.run = async (message, args) => { if (error) throw error; const data = gm(file).out("-coalesce").out("null:").gravity("North").out(file2).out("-layers", "composite").out("null:").gravity("South").out(file3).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data, image.type), - name: `meme.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `meme.${image.outputType}` }); }); }); diff --git a/commands/memecenter.js b/commands/memecenter.js index 08cf577..7176c78 100644 --- a/commands/memecenter.js +++ b/commands/memecenter.js @@ -9,10 +9,10 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to add a MemeCenter watermark!`; const watermark = "./assets/images/memecenter.png"; let resultBuffer; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const command = gm(image.data).coalesce().background("white").extent(size.width, size.height + 15).out("null:").out(watermark).gravity("SouthEast").compose("over").out("-layers", "composite").out("-layers", "optimize"); - const output = await gmToBuffer(command, image.type); + const command = gm(image.path).coalesce().background("white").extent(size.width, size.height + 15).out("null:").out(watermark).gravity("SouthEast").compose("over").out("-layers", "composite").out("-layers", "optimize"); + const output = await gmToBuffer(command, image.outputType); gm(output).size(async (error, size2) => { if (error) throw error; resultBuffer = output; @@ -22,7 +22,7 @@ exports.run = async (message) => { } return message.channel.createMessage("", { file: resultBuffer, - name: `memecenter.${image.type}` + name: `memecenter.${image.outputType}` }); }); }); diff --git a/commands/motivate.js b/commands/motivate.js index c1e21d3..d4935ea 100644 --- a/commands/motivate.js +++ b/commands/motivate.js @@ -1,4 +1,3 @@ -const fs = require("fs"); const gm = require("gm").subClass({ imageMagick: true }); @@ -10,13 +9,11 @@ exports.run = async (message, args) => { if (args.length === 0) return `${message.author.mention}, you need to provide some text to make a motivational poster!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); const [topText, bottomText] = args.join(" ").split(",").map(elem => elem.trim()); - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const file = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const file2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const text = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; const text2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; - require("util").promisify(fs.writeFile)(path, image.data); - gm().in("(").in(path).coalesce().resize(500, 500).borderColor("black").border(5, 5).out(")").borderColor("white").border(3, 3).out("-layers", "optimize").write(file, (error) => { + gm().in("(").in(image.path).coalesce().resize(500, 500).borderColor("black").border(5, 5).out(")").borderColor("white").border(3, 3).out("-layers", "optimize").write(file, (error) => { if (error) throw error; gm(file).size((error, size) => { if (error) throw error; @@ -35,21 +32,21 @@ exports.run = async (message, args) => { if (error) throw error; gm(text2).size(async (error, size4) => { if (error) throw error; - const command2 = gm(await gmToBuffer(command, image.type)).gravity("North").coalesce().background("black").extent(600, size2.height + size3.height + size4.height).out("null:", "(", text2, "-set", "page", `+0+${size2.height + size3.height}`, ")", "-layers", "composite", "-layers", "optimize"); - const resultBuffer = await gmToBuffer(command2, image.type); + const command2 = gm(await gmToBuffer(command, image.outputType)).gravity("North").coalesce().background("black").extent(600, size2.height + size3.height + size4.height).out("null:", "(", text2, "-set", "page", `+0+${size2.height + size3.height}`, ")", "-layers", "composite", "-layers", "optimize"); + const resultBuffer = await gmToBuffer(command2, image.outputType); processMessage.delete(); return message.channel.createMessage("", { file: resultBuffer, - name: `motivate.${image.type}` + name: `motivate.${image.outputType}` }); }); }); } else { - const resultBuffer = await gmToBuffer(command, image.type); + const resultBuffer = await gmToBuffer(command, image.outputType); processMessage.delete(); return message.channel.createMessage("", { file: resultBuffer, - name: `motivate.${image.type}` + name: `motivate.${image.outputType}` }); } }); diff --git a/commands/scott.js b/commands/scott.js index 01dea0b..15b2204 100644 --- a/commands/scott.js +++ b/commands/scott.js @@ -2,19 +2,16 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fs = require("fs"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Scott the Woz TV meme!`; const template = "./assets/images/scott.png"; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).out("null:").out("(").out(path).out("-virtual-pixel", "transparent").resize("415x234!").coalesce().out("+distort", "Perspective", "0,0 129,187 415,0 517,182 415,234 517,465 0,234 132,418").out(")").compose("over").gravity("Center").geometry("-238-98").out("-layers", "composite").out("-layers", "optimize"); + const command = gm(template).out("null:").out("(").out(image.path).coalesce().out("-virtual-pixel", "transparent").resize("415x234!").out("+distort", "Perspective", "0,0 129,187 415,0 517,182 415,234 517,465 0,234 132,418").out(")").compose("over").gravity("Center").geometry("-238-98").out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `scott.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `scott.${image.outputType}` }); }; diff --git a/commands/sharpen.js b/commands/sharpen.js index cba3e75..fb7ea57 100644 --- a/commands/sharpen.js +++ b/commands/sharpen.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to sharpen!`; - const command = gm(image.data).sharpen(10); + const command = gm(image.path).coalesce().sharpen(10); return message.channel.createMessage("", { - file: await gmToBuffer(command), - name: `sharpen.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `sharpen.${image.outputType}` }); }; diff --git a/commands/shutterstock.js b/commands/shutterstock.js index 070dfd5..4b2b47c 100644 --- a/commands/shutterstock.js +++ b/commands/shutterstock.js @@ -8,12 +8,12 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a Shutterstock watermark!`; const watermark = "./assets/images/shutterstock.png"; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; - const command = gm(image.data).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); + const command = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command), - name: `shutterstock.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `shutterstock.${image.outputType}` }); }); }; diff --git a/commands/speed.js b/commands/speed.js index d61e7a6..15d1f9e 100644 --- a/commands/speed.js +++ b/commands/speed.js @@ -1,4 +1,3 @@ -const fs = require("fs"); const { exec } = require("child_process"); const util = require("util"); const gm = require("gm").subClass({ @@ -10,23 +9,21 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide a GIF to speed up!`; - if (image.type !== "gif") return `${message.author.mention}, that isn't a GIF!`; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.gif`; - util.promisify(fs.writeFile)(path, image.data); - gm(image.data).identify(async (error, value) => { + if (image.type !== "gif" && image.type !== "mp4") return `${message.author.mention}, that isn't a GIF!`; + gm(image.path).identify(async (error, value) => { if (error) throw error; - const delay = value.Delay[0].split("x"); + const delay = value.Delay ? value.Delay[0].split("x") : [0, 100]; if (Math.round(parseInt(delay[0]) / 2) >= 2) { - const data = gm().delay(`${parseInt(delay[0]) / 2}x${delay[1]}`).out(path); + const data = gm().delay(`${parseInt(delay[0]) / 2}x${delay[1]}`).out(image.path); return message.channel.createMessage("", { - file: await gmToBuffer(data), + file: await gmToBuffer(data, image.outputType), name: "speed.gif" }); } else { - const numbers = (await util.promisify(exec)(`seq 0 2 ${value.Delay.length}`)).stdout.split("\n").join(","); - const data = gm().out("(").out(path).coalesce().out(")").out("-delete", numbers).out("-layers", "optimize"); + const numbers = (await util.promisify(exec)(`seq 0 2 ${value.Scene.length}`)).stdout.split("\n").join(","); + const data = gm().out("(").out(image.path).coalesce().out(")").out("-delete", numbers).out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(data), + file: await gmToBuffer(data, image.outputType), name: "speed.gif" }); } diff --git a/commands/spin.js b/commands/spin.js index 0cf1ffa..1286b21 100644 --- a/commands/spin.js +++ b/commands/spin.js @@ -7,8 +7,8 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to spin!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); - const command = gm(image.data).out("-duplicate", "29").scale("256x256>").scale("256x256<").background("transparent").virtualPixel("background").out("-distort", "SRT", "'%[fx:360*t/n]'").set("delay", "5").set("dispose", "Background").out("-loop", "0"); - const resultBuffer = await gmToBuffer(command, "gif"); + const command = gm(image.path).out("-duplicate", "29").scale("256x256>").scale("256x256<").background("transparent").virtualPixel("background").out("-distort", "SRT", "'%[fx:360*t/n]'").set("delay", "5").set("dispose", "Background").out("-loop", "0"); + const resultBuffer = await gmToBuffer(command, image.outputType); await processMessage.delete(); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/stretch.js b/commands/stretch.js index a782aa1..3ec2c5e 100644 --- a/commands/stretch.js +++ b/commands/stretch.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to stretch!`; - const data = gm(image.data).resize("512x512!"); + const data = gm(image.path).coalesce().resize("512x512!"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `stretch.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `stretch.${image.outputType}` }); }; diff --git a/commands/swirl.js b/commands/swirl.js index 3c30789..2140288 100644 --- a/commands/swirl.js +++ b/commands/swirl.js @@ -7,10 +7,10 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to swirl!`; - const data = gm(image.data).swirl(180); + const data = gm(image.path).coalesce().swirl(180); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `swirl.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `swirl.${image.outputType}` }); }; diff --git a/commands/tile.js b/commands/tile.js index 9305238..2df8f38 100644 --- a/commands/tile.js +++ b/commands/tile.js @@ -7,12 +7,12 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to tile!`; - gm(image.data).command("montage").out("-duplicate").out(24).tile("5x5").geometry("+0+0").stream(async (error, output) => { + gm(image.path).coalesce().command("montage").out("-duplicate").out(24).tile("5x5").geometry("+0+0").stream("miff", async (error, output) => { if (error) throw error; - const data = gm(output).resize("800x800>"); + const data = gm(output).coalesce().resize("800x800>"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `tile.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `tile.${image.outputType}` }); }); }; diff --git a/commands/trump.js b/commands/trump.js index 8751227..97fda9d 100644 --- a/commands/trump.js +++ b/commands/trump.js @@ -2,19 +2,16 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fs = require("fs"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Trump meme!`; const template = "./assets/images/trump.png"; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).background("white").out("null:").out("(").out(path).out("-virtual-pixel", "transparent").resize("365x179!").coalesce().out("+distort", "Perspective", "0,0 207,268 365,0 548,271 365,179 558,450 0,179 193,450").out(")").compose("over").gravity("Center").geometry("-217-135").out("-layers", "composite").out("-layers", "optimize"); + const command = gm(template).background("white").out("null:").out("(").out(image.path).coalesce().out("-virtual-pixel", "transparent").resize("365x179!").out("+distort", "Perspective", "0,0 207,268 365,0 548,271 365,179 558,450 0,179 193,450").out(")").compose("over").gravity("Center").geometry("-217-135").out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `trump.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `trump.${image.outputType}` }); }; diff --git a/commands/waaw.js b/commands/waaw.js index b52d14d..0469669 100644 --- a/commands/waaw.js +++ b/commands/waaw.js @@ -11,16 +11,16 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; - gm(image.data).size((error, size) => { + gm(image.path).size((error, size) => { if (error) throw error; - gm(image.data).coalesce().gravity("East").crop("50%", 0).out("+repage").write(data2, (error) => { + gm(image.path).coalesce().gravity("East").crop("50%", 0).out("+repage").write(data2, (error) => { if (error) throw error; gm(data2).flop().write(data, async (error) => { if (error) throw error; const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+${size.width / 2}+0`).out("-layers", "Composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `waaw.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `waaw.${image.outputType}` }); }); }); diff --git a/commands/wall.js b/commands/wall.js index 2c8f240..c462927 100644 --- a/commands/wall.js +++ b/commands/wall.js @@ -7,12 +7,12 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a wall from!`; - gm(image.data).resize(128).stream(async (error, output) => { + gm(image.path).coalesce().resize(128).stream("miff", async (error, output) => { if (error) throw error; - const data = gm(output).virtualPixel("tile").matteColor("none").out("-background", "none").resize("512x512!").out("-distort").out("Perspective").out("0,0,57,42 0,128,63,130 128,0,140,60 128,128,140,140"); + const data = gm(output).coalesce().virtualPixel("tile").matteColor("none").out("-background", "none").resize("512x512!").out("-distort").out("Perspective").out("0,0,57,42 0,128,63,130 128,0,140,60 128,128,140,140"); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `wall.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `wall.${image.outputType}` }); }); }; diff --git a/commands/wdt.js b/commands/wdt.js index 0bfed3c..f81ca7a 100644 --- a/commands/wdt.js +++ b/commands/wdt.js @@ -2,19 +2,16 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fs = require("fs"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a "who did this" meme!`; const template = "./assets/images/whodidthis.png"; - const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).coalesce().out("null:").out(path).gravity("Center").resize("374x374>").out("-layers", "composite").out("-layers", "optimize"); + const command = gm(template).coalesce().out("null:").out(image.path).gravity("Center").resize("374x374>").out("-layers", "composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `wdt.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `wdt.${image.outputType}` }); }; diff --git a/commands/wide.js b/commands/wide.js index 9421431..092279b 100644 --- a/commands/wide.js +++ b/commands/wide.js @@ -7,13 +7,13 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to stretch!`; - gm(image.data).size(async (error, size) => { + gm(image.path).size(async (error, size) => { if (error) throw error; if (size.width > 10000) return `${message.author.mention}, this image is too wide!`; - const data = gm(image.data).resize(`${(size.width * 19) / 2}x${size.height / 2}!`); + const data = gm(image.path).coalesce().resize(`${(size.width * 19) / 2}x${size.height / 2}!`); return message.channel.createMessage("", { - file: await gmToBuffer(data), - name: `wide.${image.type}` + file: await gmToBuffer(data, image.outputType), + name: `wide.${image.outputType}` }); }); }; diff --git a/commands/woow.js b/commands/woow.js index b3668a8..f3c9817 100644 --- a/commands/woow.js +++ b/commands/woow.js @@ -11,16 +11,16 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.miff`; - gm(image.data).size((error, size) => { + gm(image.path).size((error, size) => { if (error) throw error; - gm(image.data).coalesce().gravity("North").crop(0, "50%").out("+repage").write(data2, (error) => { + gm(image.path).coalesce().gravity("North").crop(0, "50%").out("+repage").write(data2, (error) => { if (error) throw error; gm(data2).flip().write(data, async (error) => { if (error) throw error; const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+0+${size.height / 2}`).out("-layers", "Composite").out("-layers", "optimize"); return message.channel.createMessage("", { - file: await gmToBuffer(command, image.type), - name: `woow.${image.type}` + file: await gmToBuffer(command, image.outputType), + name: `woow.${image.outputType}` }); }); }); diff --git a/messages.json b/messages.json index 2ee8db0..0325fb7 100644 --- a/messages.json +++ b/messages.json @@ -87,5 +87,6 @@ "foobar2000", "XMPlay", "OpenMPT", - "follow @esmBot_ on Twitter" + "follow @esmBot_ on Twitter", + "with GIFs" ] \ No newline at end of file diff --git a/utils/imagedetect.js b/utils/imagedetect.js index 0803bd1..3fb641f 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -1,9 +1,10 @@ const fetch = require("node-fetch"); const fileType = require("file-type"); +const writeFile = require("util").promisify(require("fs").writeFile); const urlRegex = /(?:\w+:)?\/\/(\S+)/; // this checks if the file is, in fact, an image -const typeCheck = async (image) => { +const typeCheck = async (image, gifv = false) => { // download the file to a buffer const imageRequest = await fetch(image); const imageBuffer = await imageRequest.buffer(); @@ -11,11 +12,18 @@ const typeCheck = async (image) => { // get the file type const imageType = await fileType.fromBuffer(imageBuffer); // check if the file is a jpeg, png, or webp - if (imageType && ["image/jpeg", "image/png", "image/webp", "image/gif"].includes(imageType.mime)) { + const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"]; + if (gifv) formats.push("video/mp4"); + if (imageType && formats.includes(imageType.mime)) { // if it is, then return the url with the file type + const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${imageType.ext}`; + await writeFile(path, imageBuffer); return { type: imageType.ext, - data: imageBuffer + data: imageBuffer, + url: image, + outputType: imageType.ext === "mp4" ? "gif" : imageType.ext, + path: path }; } else { // if not, then return false @@ -33,7 +41,23 @@ module.exports = async (cmdMessage) => { // iterate over each message for (const message of messages) { // check the attachments first - if (message.attachments.length !== 0) { + if (message.embeds.length !== 0) { + // embeds can have 2 possible entries with images, we check the thumbnail first + if (message.embeds[0].type === "gifv") { + const type = await typeCheck(message.embeds[0].video.url, true); + if (type === false) continue; + return type; + } else if (message.embeds[0].thumbnail) { + const type = await typeCheck(message.embeds[0].thumbnail.url); + if (type === false) continue; + return type; + // if there isn't a thumbnail check the image area + } else if (message.embeds[0].image) { + const type = await typeCheck(message.embeds[0].image.url); + if (type === false) continue; + return type; + } + } else if (message.attachments.length !== 0) { // get type of file const type = await typeCheck(message.attachments[0].url); // move to the next message if the file isn't an image @@ -51,18 +75,6 @@ module.exports = async (cmdMessage) => { // if the file is an image then return it return type; // if there's no urls then check the embeds - } else if (message.embeds.length !== 0) { - // embeds can have 2 possible entries with images, we check the thumbnail first - if (message.embeds[0].thumbnail) { - const type = await typeCheck(message.embeds[0].thumbnail.url); - if (type === false) continue; - return type; - // if there isn't a thumbnail check the image area - } else if (message.embeds[0].image) { - const type = await typeCheck(message.embeds[0].image.url); - if (type === false) continue; - return type; - } } } }; \ No newline at end of file