From 1e259ab312743978a8fe8ede06589874cffdc55e Mon Sep 17 00:00:00 2001 From: TheEssem Date: Thu, 20 Feb 2020 18:26:49 -0600 Subject: [PATCH] Removed node version check, promisified many gm calls --- app.js | 3 -- commands/bandicam.js | 14 ++++---- commands/caption.js | 30 +++++++--------- commands/deviantart.js | 14 ++++---- commands/funky.js | 14 ++++---- commands/haah.js | 24 +++++-------- commands/hooh.js | 24 +++++-------- commands/hypercam.js | 14 ++++---- commands/ifunny.js | 14 ++++---- commands/magik.js | 20 +++++------ commands/meme.js | 26 +++++--------- commands/memecenter.js | 28 +++++++-------- commands/motivate.js | 63 ++++++++++++---------------------- commands/shutterstock.js | 14 ++++---- commands/sonic.js | 14 ++++---- commands/speed.js | 41 +++++++++++----------- commands/tile.js | 14 ++++---- commands/waaw.js | 26 +++++--------- commands/wall.js | 14 ++++---- commands/wdt.js | 2 +- commands/wide.js | 16 ++++----- commands/woow.js | 24 +++++-------- events/ready.js | 8 +++++ utils/pagination/pagination.js | 2 +- 24 files changed, 187 insertions(+), 276 deletions(-) diff --git a/app.js b/app.js index 42204ac6..9117f1c8 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,3 @@ -// check if using node 10 or higher -if (process.version.slice(1).split(".")[0] < 10) throw new Error("Node 10.0.0 or higher is required. Update Node on your system."); - // load config from .env file require("dotenv").config(); diff --git a/commands/bandicam.js b/commands/bandicam.js index 08dc75f4..530a8d30 100644 --- a/commands/bandicam.js +++ b/commands/bandicam.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("North").resize(null, size.height).out("-layers", "composite"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `bandicam.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("North").resize(null, size.height).out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `bandicam.${image.outputType}` }); }; diff --git a/commands/caption.js b/commands/caption.js index 72905c8a..71baa2c3 100644 --- a/commands/caption.js +++ b/commands/caption.js @@ -9,24 +9,18 @@ exports.run = async (message, args) => { 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 processMessage = await message.channel.createMessage(" Processing... This might take a while"); - 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"); - gm(output).size(async (error, size2) => { - 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(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"); - 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.outputType}` - }); - }); - }); + const size = await gm(image.path).sizePromise(); + 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"); + const size2 = await gm(output).sizePromise(); + const output2 = await gm(output).gravity("Center").trim().out("+repage").extent(size.width, size2.height + (size.width / 10)).streamPromise(); + 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"); + 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.outputType}` }); }; diff --git a/commands/deviantart.js b/commands/deviantart.js index 17a78ff4..665057d6 100644 --- a/commands/deviantart.js +++ b/commands/deviantart.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `deviantart.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `deviantart.${image.outputType}` }); }; diff --git a/commands/funky.js b/commands/funky.js index d8f1ab9e..7fb724c5 100644 --- a/commands/funky.js +++ b/commands/funky.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthEast").resize(null, size.height).out("-layers", "composite"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `funky.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthEast").resize(null, size.height).out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `funky.${image.outputType}` }); }; diff --git a/commands/haah.js b/commands/haah.js index 1ba3fa3b..e68abf70 100644 --- a/commands/haah.js +++ b/commands/haah.js @@ -1,5 +1,3 @@ -// really don't like this file - const gm = require("gm").subClass({ imageMagick: true }); @@ -11,20 +9,14 @@ 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.path).size((error, size) => { - if (error) throw 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"); - const buffer = await gmToBuffer(command, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `haah.${image.outputType}` - }); - }); - }); + const size = await gm(image.path).sizePromise(); + await gm(image.path).coalesce().gravity("West").crop("50%", 0).out("+repage").writePromise(data2); + await gm(data2).flop().writePromise(data); + const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+${size.width / 2}+0`).out("-layers", "Composite"); + const buffer = await gmToBuffer(command, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `haah.${image.outputType}` }); }; diff --git a/commands/hooh.js b/commands/hooh.js index 82dde636..dffc6202 100644 --- a/commands/hooh.js +++ b/commands/hooh.js @@ -1,5 +1,3 @@ -// really don't like this file - const gm = require("gm").subClass({ imageMagick: true }); @@ -11,20 +9,14 @@ 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.path).size((error, size) => { - if (error) throw 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"); - const buffer = await gmToBuffer(command, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `hooh.${image.outputType}` - }); - }); - }); + const size = await gm(image.path).sizePromise(); + await gm(image.path).coalesce().gravity("South").crop(0, "50%").out("+repage").writePromise(data2); + await gm(data2).flip().writePromise(data); + const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+0+${size.height / 2}`).out("-layers", "Composite"); + const buffer = await gmToBuffer(command, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `hooh.${image.outputType}` }); }; diff --git a/commands/hypercam.js b/commands/hypercam.js index 70d3f423..d1703d07 100644 --- a/commands/hypercam.js +++ b/commands/hypercam.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthWest").resize(null, size.height).out("-layers", "composite"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `hypercam.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("NorthWest").resize(null, size.height).out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `hypercam.${image.outputType}` }); }; diff --git a/commands/ifunny.js b/commands/ifunny.js index f76a0af8..2957319d 100644 --- a/commands/ifunny.js +++ b/commands/ifunny.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("South").resize(size.width, null).out("-layers", "composite"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `ifunny.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const data = gm(image.path).coalesce().out("null:").out(watermark).gravity("South").resize(size.width, null).out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `ifunny.${image.outputType}` }); }; diff --git a/commands/magik.js b/commands/magik.js index 92b86fba..63406cbe 100644 --- a/commands/magik.js +++ b/commands/magik.js @@ -8,18 +8,14 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to add some magik!`; 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; - gm(stream).out("-liquid-rescale", "400x400").stream(async (error, stream2) => { - if (error) throw error; - const data = gm(stream2).out("-liquid-rescale", "1200x1200"); - const resultBuffer = await gmToBuffer(data); - await processMessage.delete(); - return message.channel.createMessage("", { - file: resultBuffer, - name: `magik.${image.outputType}` - }); - }); + const stream = await gm(image.path).coalesce().resize(600, 600).streamPromise("miff"); + const stream2 = await gm(stream).out("-liquid-rescale", "300x300").streamPromise(); + const data = gm(stream2).out("-liquid-rescale", "800x800"); + const resultBuffer = await gmToBuffer(data, image.outputType); + await processMessage.delete(); + return message.channel.createMessage("", { + file: resultBuffer, + name: `magik.${image.outputType}` }); }; diff --git a/commands/meme.js b/commands/meme.js index f5c9fa59..41dc234a 100644 --- a/commands/meme.js +++ b/commands/meme.js @@ -12,23 +12,15 @@ 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.path).coalesce().resize(600, 600).noProfile().write(file, (error) => { - if (error) throw error; - gm(file).size((error, size) => { - if (error) throw error; - gm().out("-size", size.width).background("none").gravity("Center").out("(", "(").font("Impact").out("-pointsize", 40).out(`pango:${topText.toUpperCase().replace(/&/g, "\\&").replace(/>/g, "\\>").replace(/`).out(")", "(", "+clone").out("-channel", "A").out("-morphology", "EdgeOut", "Octagon", "+channel", "+level-colors", "black", ")").compose("DstOver").out(")", "-composite").write(file2, (error) => { - if (error) throw error; - gm().out("-size", size.width).background("none").gravity("Center").out("(", "(").font("Impact").out("-pointsize", 40).out(`pango:${bottomText ? bottomText.toUpperCase().replace(/&/g, "\\&").replace(/>/g, "\\>").replace(/`).out(")", "(", "+clone").out("-channel", "A").out("-morphology", "EdgeOut", "Octagon", "+channel", "+level-colors", "black", ")").compose("DstOver").out(")", "-composite").write(file3, async (error) => { - 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"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `meme.${image.outputType}` - }); - }); - }); - }); + await gm(image.path).coalesce().resize(600, 600).noProfile().writePromise(file); + const size = await gm(file).sizePromise(); + await gm().out("-size", size.width).background("none").gravity("Center").out("(", "(").font("Impact").out("-pointsize", 40).out(`pango:${topText.toUpperCase().replace(/&/g, "\\&").replace(/>/g, "\\>").replace(/`).out(")", "(", "+clone").out("-channel", "A").out("-morphology", "EdgeOut", "Octagon", "+channel", "+level-colors", "black", ")").compose("DstOver").out(")", "-composite").writePromise(file2); + if (bottomText) await gm().out("-size", size.width).background("none").gravity("Center").out("(", "(").font("Impact").out("-pointsize", 40).out(`pango:${bottomText.toUpperCase().replace(/&/g, "\\&").replace(/>/g, "\\>").replace(/`).out(")", "(", "+clone").out("-channel", "A").out("-morphology", "EdgeOut", "Octagon", "+channel", "+level-colors", "black", ")").compose("DstOver").out(")", "-composite").writePromise(file3); + const data = gm(file).out("-coalesce").out("null:").gravity("North").out(file2).out("-layers", "composite").out("null:").gravity("South").out(bottomText ? file3 : "null:").out("-layers", "composite"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `meme.${image.outputType}` }); }; diff --git a/commands/memecenter.js b/commands/memecenter.js index 091d34e1..365bc1e7 100644 --- a/commands/memecenter.js +++ b/commands/memecenter.js @@ -9,22 +9,18 @@ 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.path).size(async (error, size) => { - if (error) throw error; - 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"); - const output = await gmToBuffer(command, image.outputType); - gm(output).size(async (error, size2) => { - if (error) throw error; - resultBuffer = output; - if (size.width !== size2.width) { - const command2 = gm(output).gravity("West").chop(size2.width - size.width, 0); - resultBuffer = await gmToBuffer(command2); - } - return message.channel.createMessage("", { - file: resultBuffer, - name: `memecenter.${image.outputType}` - }); - }); + const size = await gm(image.path).sizePromise(); + 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"); + const output = await gmToBuffer(command, image.outputType); + const size2 = await gm(output).sizePromise(); + resultBuffer = output; + if (size.width !== size2.width) { + const command2 = gm(output).gravity("West").chop(size2.width - size.width, 0); + resultBuffer = await gmToBuffer(command2); + } + return message.channel.createMessage("", { + file: resultBuffer, + name: `memecenter.${image.outputType}` }); }; diff --git a/commands/motivate.js b/commands/motivate.js index d4935ea8..096a7311 100644 --- a/commands/motivate.js +++ b/commands/motivate.js @@ -10,50 +10,29 @@ exports.run = async (message, args) => { const processMessage = await message.channel.createMessage(" Processing... This might take a while"); const [topText, bottomText] = args.join(" ").split(",").map(elem => elem.trim()); 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`; - 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; - gm(file).coalesce().background("black").gravity("Center").extent(600, size.height + 50).out("+swap").write(file2, () => { - // this is commented out because it was causing some issues and I couldn't find an elegant solution - // if (error) throw error; - gm(file2).size((error, size2) => { - if (error) throw error; - gm().background("black").out("-size", "600").fill("white").font("Times").pointSize(56).gravity("Center").out(`pango:${topText}`).gravity("South").out("-splice", bottomText ? "0x0" : "0x20").write(text, (error) => { - if (error) throw error; - gm(text).size(async (error, size3) => { - if (error) throw error; - const command = gm(file2).gravity("North").coalesce().background("black").extent(600, size2.height + size3.height).out("null:", "(", text, "-set", "page", `+0+${size2.height}`, ")", "-layers", "composite", "-layers", "optimize"); - if (bottomText) { - gm().background("black").out("-size", "600").fill("white").font("Times").pointSize(28).gravity("Center").out(`pango:${bottomText}`).gravity("South").out("-splice", "0x20").write(text2, (error) => { - if (error) throw error; - gm(text2).size(async (error, size4) => { - if (error) throw error; - 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.outputType}` - }); - }); - }); - } else { - const resultBuffer = await gmToBuffer(command, image.outputType); - processMessage.delete(); - return message.channel.createMessage("", { - file: resultBuffer, - name: `motivate.${image.outputType}` - }); - } - }); - }); - }); - }); - }); + const command = gm().in("(").in(image.path).coalesce().resize(500, 500).borderColor("black").border(5, 5).out(")").borderColor("white").border(3, 3); + const buffer = await gmToBuffer(command, "miff"); + const size = await gm(buffer).sizePromise(); + await gm(buffer).coalesce().background("black").gravity("Center").extent(600, size.height + 50).writePromise(file); + const size2 = await gm(file).sizePromise(); + await gm().background("black").out("-size", "600").fill("white").font("Times").pointSize(56).gravity("Center").out(`pango:${topText}`).gravity("South").out("-splice", bottomText ? "0x0" : "0x20").writePromise(text); + const size3 = await gm(text).sizePromise(); + const command2 = gm(file).gravity("North").coalesce().background("black").extent(600, size2.height + size3.height).out("null:", "(", text, "-set", "page", `+0+${size2.height}`, ")", "-layers", "composite", "-layers", "optimize"); + let resultBuffer; + if (bottomText) { + await gm().background("black").out("-size", "600").fill("white").font("Times").pointSize(28).gravity("Center").out(`pango:${bottomText}`).gravity("South").out("-splice", "0x20").writePromise(text2); + const size4 = await gm(text2).sizePromise(); + const command3 = gm(await gmToBuffer(command2, 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"); + resultBuffer = await gmToBuffer(command3, image.outputType); + } else { + resultBuffer = await gmToBuffer(command2, image.outputType); + } + processMessage.delete(); + return message.channel.createMessage("", { + file: resultBuffer, + name: `motivate.${image.outputType}` }); }; diff --git a/commands/shutterstock.js b/commands/shutterstock.js index 8499e476..f4c9fcc3 100644 --- a/commands/shutterstock.js +++ b/commands/shutterstock.js @@ -8,14 +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.path).size(async (error, size) => { - if (error) throw error; - const command = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); - const buffer = await gmToBuffer(command, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `shutterstock.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + const command = gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); + const buffer = await gmToBuffer(command, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `shutterstock.${image.outputType}` }); }; diff --git a/commands/sonic.js b/commands/sonic.js index c3e54a68..1f5d949c 100644 --- a/commands/sonic.js +++ b/commands/sonic.js @@ -10,14 +10,12 @@ exports.run = async (message, args) => { const template = "./assets/images/sonic.jpg"; const file = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; const cleanedMessage = args.join(" ").replace(/&/g, "\\&").replace(/>/g, "\\>").replace(/${wrap(cleanedMessage, {width: 15, indent: ""})}`).write(file, async (error) => { - if (error) throw error; - const command = gm(template).composite(file).gravity("Center").geometry("474x332+160+10"); - const buffer = await gmToBuffer(command, "png", "sonic"); - return message.channel.createMessage("", { - file: buffer, - name: "sonic.png" - }); + await gm(474, 332).out("+size").background("none").gravity("Center").out("-pointsize", 72).out("-font", "Bitstream Vera Sans").out(`pango:${wrap(cleanedMessage, {width: 15, indent: ""})}`).writePromise(file); + const command = gm(template).composite(file).gravity("Center").geometry("474x332+160+10"); + const buffer = await gmToBuffer(command, "png", "sonic"); + return message.channel.createMessage("", { + file: buffer, + name: "sonic.png" }); }; diff --git a/commands/speed.js b/commands/speed.js index bf45e1c2..326cf9f3 100644 --- a/commands/speed.js +++ b/commands/speed.js @@ -1,4 +1,6 @@ -const { exec } = require("child_process"); +const { + exec +} = require("child_process"); const util = require("util"); const gm = require("gm").subClass({ imageMagick: true @@ -10,25 +12,24 @@ exports.run = async (message) => { 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" && 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 ? 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(image.path); - return message.channel.createMessage("", { - file: await gmToBuffer(data, image.outputType), - name: "speed.gif" - }); - } else { - 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); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: "speed.gif" - }); - } - }); + const value = await gm(image.path).identifyPromise(); + 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(image.path); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: "speed.gif" + }); + } else { + 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); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: "speed.gif" + }); + } }; exports.aliases = ["speedup", "fast", "gifspeed"]; diff --git a/commands/tile.js b/commands/tile.js index f3256e01..2a5e50cb 100644 --- a/commands/tile.js +++ b/commands/tile.js @@ -7,14 +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.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).coalesce().resize("800x800>"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `tile.${image.outputType}` - }); + const output = await gm(image.path).coalesce().command("montage").out("-duplicate").out(24).tile("5x5").geometry("+0+0").streamPromise("miff"); + const data = gm(output).coalesce().resize("800x800>"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `tile.${image.outputType}` }); }; diff --git a/commands/waaw.js b/commands/waaw.js index 2beeb7df..a12e9e45 100644 --- a/commands/waaw.js +++ b/commands/waaw.js @@ -1,5 +1,3 @@ -// really don't like this file - const gm = require("gm").subClass({ imageMagick: true }); @@ -11,23 +9,17 @@ 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.path).size((error, size) => { - if (error) throw 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"); - const buffer = await gmToBuffer(command, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `waaw.${image.outputType}` - }); - }); - }); + const size = await gm(image.path).sizePromise(); + await gm(image.path).coalesce().gravity("East").crop("50%", 0).out("+repage").writePromise(data2); + await gm(data2).flop().writePromise(data); + const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+${size.width / 2}+0`).out("-layers", "Composite"); + const buffer = await gmToBuffer(command, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `waaw.${image.outputType}` }); }; exports.aliases = ["magik3", "mirror"]; exports.category = 5; -exports.help = "Mirrors the right side of an image onto the left"; +exports.help = "Mirrors the right side of an image onto the left"; \ No newline at end of file diff --git a/commands/wall.js b/commands/wall.js index 44edb4d4..3f309c81 100644 --- a/commands/wall.js +++ b/commands/wall.js @@ -7,14 +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.path).coalesce().resize(128).stream("miff", async (error, output) => { - if (error) throw error; - 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"); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `wall.${image.outputType}` - }); + const output = await gm(image.path).coalesce().resize(128).streamPromise("miff"); + 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"); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `wall.${image.outputType}` }); }; diff --git a/commands/wdt.js b/commands/wdt.js index 41d57b4e..c0dbc922 100644 --- a/commands/wdt.js +++ b/commands/wdt.js @@ -8,7 +8,7 @@ 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 make a "who did this" meme!`; const template = "./assets/images/whodidthis.png"; - const command = gm(template).coalesce().out("null:").out(image.path).gravity("Center").resize("374x374>").out("-layers", "composite"); + const command = gm(template).out("null:").out("(").out(image.path).coalesce().out(")").gravity("Center").resize("374x374>").out("-layers", "composite"); const buffer = await gmToBuffer(command, image.outputType); return message.channel.createMessage("", { file: buffer, diff --git a/commands/wide.js b/commands/wide.js index ae977820..fe8bab05 100644 --- a/commands/wide.js +++ b/commands/wide.js @@ -7,15 +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.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.path).coalesce().resize(`${(size.width * 19) / 2}x${size.height / 2}!`); - const buffer = await gmToBuffer(data, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `wide.${image.outputType}` - }); + const size = await gm(image.path).sizePromise(); + if (size.width > 10000) return `${message.author.mention}, this image is too wide!`; + const data = gm(image.path).coalesce().resize(`${(size.width * 19) / 2}x${size.height / 2}!`); + const buffer = await gmToBuffer(data, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `wide.${image.outputType}` }); }; diff --git a/commands/woow.js b/commands/woow.js index 88857b23..923a8edd 100644 --- a/commands/woow.js +++ b/commands/woow.js @@ -1,5 +1,3 @@ -// really don't like this file - const gm = require("gm").subClass({ imageMagick: true }); @@ -11,20 +9,14 @@ 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.path).size((error, size) => { - if (error) throw 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"); - const buffer = await gmToBuffer(command, image.outputType); - return message.channel.createMessage("", { - file: buffer, - name: `woow.${image.outputType}` - }); - }); - }); + const size = await gm(image.path).sizePromise(); + await gm(image.path).coalesce().gravity("North").crop(0, "50%").out("+repage").writePromise(data2); + await gm(data2).flip().writePromise(data); + const command = gm(data2).extent(size.width, size.height).out("null:").out(data).geometry(`+0+${size.height / 2}`).out("-layers", "Composite"); + const buffer = await gmToBuffer(command, image.outputType); + return message.channel.createMessage("", { + file: buffer, + name: `woow.${image.outputType}` }); }; diff --git a/events/ready.js b/events/ready.js index 38a47a9e..5cdcf7e0 100644 --- a/events/ready.js +++ b/events/ready.js @@ -1,3 +1,5 @@ +const gm = require("gm"); +const { promisify } = require("util"); const client = require("../utils/client.js"); const database = require("../utils/database.js"); const logger = require("../utils/logger.js"); @@ -33,6 +35,12 @@ module.exports = async () => { setTimeout(activityChanger, 900000); })(); + // add gm extensions + gm.prototype.writePromise = promisify(gm.prototype.write); + gm.prototype.streamPromise = promisify(gm.prototype.stream); + gm.prototype.sizePromise = promisify(gm.prototype.size); + gm.prototype.identifyPromise = promisify(gm.prototype.identify); + // tweet stuff if (twitter !== null && twitter.active === false) { const blocks = await twitter.client.get("blocks/ids", { stringify_ids: true }); diff --git a/utils/pagination/pagination.js b/utils/pagination/pagination.js index b14cefbd..b784ba1c 100644 --- a/utils/pagination/pagination.js +++ b/utils/pagination/pagination.js @@ -33,7 +33,7 @@ const paginationEmbed = async (message, pages, timeout = 120000) => { if (manageMessages) msg.removeReaction("🔢", userID); }); }).catch(error => { - if (error) throw error; + throw error; }); break; case "▶": -- 2.51.2