From ef071a39d49c9cf40aa002691720bbe3801785b9 Mon Sep 17 00:00:00 2001 From: TheEssem Date: Mon, 11 Nov 2019 14:54:19 -0600 Subject: [PATCH] Removed extra image request for image commands, various other tweaks --- commands/9gag.js | 5 +---- commands/bandicam.js | 7 ++----- commands/blur.js | 5 +---- commands/blurple.js | 5 +---- commands/circle.js | 5 +---- commands/deviantart.js | 7 ++----- commands/explode.js | 5 +---- commands/flip.js | 5 +---- commands/flop.js | 5 +---- commands/funky.js | 7 ++----- commands/gamexplain.js | 5 +---- commands/haah.js | 5 +---- commands/hooh.js | 5 +---- commands/hypercam.js | 7 ++----- commands/ifunny.js | 7 ++----- commands/implode.js | 5 +---- commands/invert.js | 5 +---- commands/jpeg.js | 5 +---- commands/magik.js | 5 +---- commands/meme.js | 5 +---- commands/memecenter.js | 11 ++++------- commands/play.js | 4 ++-- commands/qrread.js | 5 +---- commands/scott.js | 7 ++----- commands/sharpen.js | 5 +---- commands/shutterstock.js | 9 +++------ commands/sonic.js | 2 +- commands/swirl.js | 5 +---- commands/tile.js | 5 +---- commands/trump.js | 7 ++----- commands/waaw.js | 5 +---- commands/wall.js | 5 +---- commands/wdt.js | 7 ++----- commands/woow.js | 5 +---- events/messageCreate.js | 2 +- utils/imagedetect.js | 14 +++++++------- utils/logger.js | 2 +- utils/misc.js | 3 ++- utils/soundplayer.js | 10 ++++++++-- 39 files changed, 67 insertions(+), 156 deletions(-) diff --git a/commands/9gag.js b/commands/9gag.js index 3b26793..feb4f7a 100644 --- a/commands/9gag.js +++ b/commands/9gag.js @@ -2,16 +2,13 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/9gag.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).composite(watermark).gravity("East"); + const data = gm(image.data).composite(watermark).gravity("East"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/bandicam.js b/commands/bandicam.js index afaaf40..6cc8a15 100644 --- a/commands/bandicam.js +++ b/commands/bandicam.js @@ -2,18 +2,15 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/bandicam.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const data = gm(imageBuffer).composite(watermark).gravity("North").resize(null, size.height); + const data = gm(image.data).composite(watermark).gravity("North").resize(null, size.height); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/blur.js b/commands/blur.js index 86bc0fe..90e1ef9 100644 --- a/commands/blur.js +++ b/commands/blur.js @@ -1,13 +1,10 @@ const sharp = require("sharp"); -const fetch = require("node-fetch"); 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 blur!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const resultBuffer = await sharp(imageBuffer).blur(5).toBuffer(); + const resultBuffer = await sharp(image.data).blur(5).toBuffer(); return message.channel.createMessage("", { file: resultBuffer, name: `blur.${image.type}` diff --git a/commands/blurple.js b/commands/blurple.js index a3fd02c..4043d78 100644 --- a/commands/blurple.js +++ b/commands/blurple.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 blurple!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).threshold(75, true).out("+level-colors").out("\"#7289DA\",white"); + const data = gm(image.data).threshold(75, true).out("+level-colors").out("\"#7289DA\",white"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/circle.js b/commands/circle.js index d9ae4f5..de04789 100644 --- a/commands/circle.js +++ b/commands/circle.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 radial blur!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).out("-radial-blur", 10); + const data = gm(image.data).out("-radial-blur", 10); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/deviantart.js b/commands/deviantart.js index 36d89e3..6c88595 100644 --- a/commands/deviantart.js +++ b/commands/deviantart.js @@ -2,18 +2,15 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/deviantart.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const data = gm(imageBuffer).composite(watermark).gravity("Center").resize(null, size.height); + const data = gm(image.data).composite(watermark).gravity("Center").resize(null, size.height); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/explode.js b/commands/explode.js index 44ec327..93ac5e7 100644 --- a/commands/explode.js +++ b/commands/explode.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 explode!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).implode([-2]); + const data = gm(image.data).implode([-2]); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/flip.js b/commands/flip.js index 7ff8893..e859258 100644 --- a/commands/flip.js +++ b/commands/flip.js @@ -1,13 +1,10 @@ const sharp = require("sharp"); -const fetch = require("node-fetch"); 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 flip!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const resultBuffer = await sharp(imageBuffer).flip().toBuffer(); + const resultBuffer = await sharp(image.data).flip().toBuffer(); return message.channel.createMessage("", { file: resultBuffer, name: `flip.${image.type}` diff --git a/commands/flop.js b/commands/flop.js index 9d8a2dd..46b8eea 100644 --- a/commands/flop.js +++ b/commands/flop.js @@ -1,13 +1,10 @@ const sharp = require("sharp"); -const fetch = require("node-fetch"); 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 flop!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const resultBuffer = await sharp(imageBuffer).flop().toBuffer(); + const resultBuffer = await sharp(image.data).flop().toBuffer(); return message.channel.createMessage("", { file: resultBuffer, name: `flop.${image.type}` diff --git a/commands/funky.js b/commands/funky.js index fe37316..927111f 100644 --- a/commands/funky.js +++ b/commands/funky.js @@ -2,18 +2,15 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/funky.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const data = gm(imageBuffer).composite(watermark).gravity("NorthEast").resize(null, size.height); + const data = gm(image.data).composite(watermark).gravity("NorthEast").resize(null, size.height); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/gamexplain.js b/commands/gamexplain.js index 7fb5113..520ce7d 100644 --- a/commands/gamexplain.js +++ b/commands/gamexplain.js @@ -2,7 +2,6 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); const fs = require("fs"); exports.run = async (message) => { @@ -10,10 +9,8 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to make a GameXplain thumbnail meme!`; message.channel.sendTyping(); const template = "./assets/images/gamexplain.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, imageBuffer); + 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("transparent").out("-resize").out("1181x571!").out(")").out("-geometry").out("+0+40").out("-composite").out(")").out("+swap").out("-composite"); const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { diff --git a/commands/haah.js b/commands/haah.js index f7b1696..14e927e 100644 --- a/commands/haah.js +++ b/commands/haah.js @@ -3,17 +3,14 @@ const gm = require("gm").subClass({ imageMagick: true }); -const fetch = require("node-fetch"); 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 mirror!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(imageBuffer).gravity("West").crop("50%", 0).strip().write(data2, (error) => { + gm(image.data).gravity("West").crop("50%", 0).strip().write(data2, (error) => { if (error) console.error; gm(data2).flop().strip().write(data, async (error) => { if (error) console.error; diff --git a/commands/hooh.js b/commands/hooh.js index 311bf1f..68b1aeb 100644 --- a/commands/hooh.js +++ b/commands/hooh.js @@ -3,17 +3,14 @@ const gm = require("gm").subClass({ imageMagick: true }); -const fetch = require("node-fetch"); 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 mirror!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(imageBuffer).gravity("South").crop(0, "50%").strip().write(data2, (error) => { + gm(image.data).gravity("South").crop(0, "50%").strip().write(data2, (error) => { if (error) console.error; gm(data2).flip().strip().write(data, async (error) => { if (error) console.error; diff --git a/commands/hypercam.js b/commands/hypercam.js index 92bcbce..1b863c7 100644 --- a/commands/hypercam.js +++ b/commands/hypercam.js @@ -2,18 +2,15 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/hypercam.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const data = gm(imageBuffer).composite(watermark).gravity("NorthWest").resize(null, size.height); + const data = gm(image.data).composite(watermark).gravity("NorthWest").resize(null, size.height); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/ifunny.js b/commands/ifunny.js index bb03d04..15e9d13 100644 --- a/commands/ifunny.js +++ b/commands/ifunny.js @@ -2,18 +2,15 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/ifunny.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const data = gm(imageBuffer).append(watermark).gravity("South").resize(size.width, null); + const data = gm(image.data).append(watermark).gravity("South").resize(size.width, null); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/implode.js b/commands/implode.js index 1945bb8..de3b828 100644 --- a/commands/implode.js +++ b/commands/implode.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 implode!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).implode([1]); + const data = gm(image.data).implode([1]); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/invert.js b/commands/invert.js index 8cc592f..6df203c 100644 --- a/commands/invert.js +++ b/commands/invert.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 invert!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).negative(); + const data = gm(image.data).negative(); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/jpeg.js b/commands/jpeg.js index 4c4c7b8..68e2eeb 100644 --- a/commands/jpeg.js +++ b/commands/jpeg.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 more JPEG!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).setFormat("jpg").quality(1); + const data = gm(image.data).setFormat("jpg").quality(1); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/magik.js b/commands/magik.js index 9610027..130819c 100644 --- a/commands/magik.js +++ b/commands/magik.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).resize(800, 800).stream((error, stream) => { + gm(image.data).resize(800, 800).stream((error, stream) => { if (error) console.error; gm(stream).out("-liquid-rescale", "400x400").stream(async (error, stream2) => { if (error) console.error; diff --git a/commands/meme.js b/commands/meme.js index c4e8e74..81f4348 100644 --- a/commands/meme.js +++ b/commands/meme.js @@ -1,16 +1,13 @@ const { spawn } = require("child_process"); -const fetch = require("node-fetch"); 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 to generate a meme!`; if (args.length === 0) return `${message.author.mention}, you need to provide some text to generate a meme!`; message.channel.sendTyping(); - const request = await fetch(image.url); - const buffer = await request.buffer(); const [topText, bottomText] = args.join(" ").split(",").map(elem => elem.trim()); const child = spawn("./utils/meme.sh", [topText.toUpperCase().replace(/\\/g, "\\\\"), bottomText ? bottomText.toUpperCase().replace(/\\/g, "\\\\") : ""]); - child.stdin.write(buffer); + child.stdin.write(image.data); child.stdin.end(); const chunks = []; child.stdout.on("data", (data) => { diff --git a/commands/memecenter.js b/commands/memecenter.js index 8813143..53d02e7 100644 --- a/commands/memecenter.js +++ b/commands/memecenter.js @@ -2,20 +2,17 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 MemeCenter watermark!`; message.channel.sendTyping(); const watermark = "./assets/images/memecenter.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); let resultBuffer; - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const command = gm(imageBuffer).out(watermark).background("#FFFFFF").gravity("East").out("-smush").out("-9"); - const output = await gmToBuffer(command); + const command = gm(image.data).out(watermark).background("#FFFFFF").gravity("East").out("-smush").out("-9"); + const output = await gmToBuffer(command, "png"); gm(output).size(async (error, size2) => { if (error) console.error; resultBuffer = output; @@ -25,7 +22,7 @@ exports.run = async (message) => { } return message.channel.createMessage("", { file: resultBuffer, - name: "memecenter.png" + name: `memecenter.${image.type}` }); }); }); diff --git a/commands/play.js b/commands/play.js index bec13c2..1b55370 100644 --- a/commands/play.js +++ b/commands/play.js @@ -11,9 +11,9 @@ exports.run = async (message) => { inlineVolume: true }); connection.setVolume(0.5); - connection.on("error", () => { + connection.on("error", (error) => { voiceChannel.leave(); - console.error; + throw error; }); connection.on("end", () => { voiceChannel.leave(); diff --git a/commands/qrread.js b/commands/qrread.js index d63f6cd..ca65e76 100644 --- a/commands/qrread.js +++ b/commands/qrread.js @@ -1,4 +1,3 @@ -const fetch = require("node-fetch"); const jsqr = require("jsqr"); const sharp = require("sharp"); @@ -6,9 +5,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 with a QR code to read it!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const rawData = await sharp(imageBuffer).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); + const rawData = await sharp(image.data).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height); return `\`\`\`\n${qrBuffer.data}\n\`\`\``; }; \ No newline at end of file diff --git a/commands/scott.js b/commands/scott.js index 3ce6d43..2668aa4 100644 --- a/commands/scott.js +++ b/commands/scott.js @@ -2,7 +2,6 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); const fs = require("fs"); exports.run = async (message) => { @@ -10,12 +9,10 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Scott the Woz TV meme!`; message.channel.sendTyping(); const template = "./assets/images/scott.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, imageBuffer); + require("util").promisify(fs.writeFile)(path, image.data); const command = gm(template).out("-gravity").out("Center").out("(").out(path).out("-virtual-pixel").out("transparent").out("-resize").out("415x234!").out("+distort").out("Perspective").out("0,0 129,187 415,0 517,182 415,234 517,465 0,234 132,418").out("-geometry").out("-110+83").out(")").out("-composite"); - const resultBuffer = await gmToBuffer(command); + const resultBuffer = await gmToBuffer(command, "png"); return message.channel.createMessage("", { file: resultBuffer, name: "scott.png" diff --git a/commands/sharpen.js b/commands/sharpen.js index 68e7b42..9a65da7 100644 --- a/commands/sharpen.js +++ b/commands/sharpen.js @@ -1,13 +1,10 @@ const sharp = require("sharp"); -const fetch = require("node-fetch"); 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 sharpen!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const resultBuffer = await sharp(imageBuffer).sharpen(5).toBuffer(); + const resultBuffer = await sharp(image.data).sharpen(5).toBuffer(); return message.channel.createMessage("", { file: resultBuffer, name: "sharpen.png" diff --git a/commands/shutterstock.js b/commands/shutterstock.js index ff7f24c..33d145f 100644 --- a/commands/shutterstock.js +++ b/commands/shutterstock.js @@ -2,22 +2,19 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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!`; message.channel.sendTyping(); const watermark = "./assets/images/shutterstock.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).size(async (error, size) => { + gm(image.data).size(async (error, size) => { if (error) console.error; - const command = gm(imageBuffer).composite(watermark).gravity("Center").resize(null, size.height); + const command = gm(image.data).composite(watermark).gravity("Center").resize(null, size.height); const output = await gmToBuffer(command); return message.channel.createMessage("", { file: output, - name: "shutterstock.png" + name: `shutterstock.${image.type}` }); }); }; diff --git a/commands/sonic.js b/commands/sonic.js index ebb95c0..62019e0 100644 --- a/commands/sonic.js +++ b/commands/sonic.js @@ -13,7 +13,7 @@ exports.run = async (message, args) => { gm(474, 332).out("+size").gravity("Center").out("-pointsize", 40).out("-font", "Bitstream Vera Sans").out(`pango:${wrap(cleanedMessage, {width: 15, indent: ""})}`).negative().out("-fuzz", "30%").transparent("black").write(file, async (error) => { if (error) console.error; const command = gm(template).composite(file).gravity("Center").geometry("474x332+160+10"); - const resultBuffer = await gmToBuffer(command); + const resultBuffer = await gmToBuffer(command, "png"); return message.channel.createMessage("", { file: resultBuffer, name: "sonic.png" diff --git a/commands/swirl.js b/commands/swirl.js index 4a8409d..faaf43b 100644 --- a/commands/swirl.js +++ b/commands/swirl.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 swirl!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - const data = gm(imageBuffer).swirl(180); + const data = gm(image.data).swirl(180); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/tile.js b/commands/tile.js index d96849c..f89ac4e 100644 --- a/commands/tile.js +++ b/commands/tile.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 tile!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).command("montage").out("-duplicate").out(24).tile("5x5").geometry("+0+0").stream(async (error, output) => { + gm(image.data).command("montage").out("-duplicate").out(24).tile("5x5").geometry("+0+0").stream(async (error, output) => { if (error) console.error; const data = gm(output).resize("800x800>"); const resultBuffer = await gmToBuffer(data); diff --git a/commands/trump.js b/commands/trump.js index 1289c46..9e3264e 100644 --- a/commands/trump.js +++ b/commands/trump.js @@ -2,7 +2,6 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); const fs = require("fs"); exports.run = async (message) => { @@ -10,12 +9,10 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Trump meme!`; message.channel.sendTyping(); const template = "./assets/images/trump.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, imageBuffer); + require("util").promisify(fs.writeFile)(path, image.data); const command = gm(template).out("-background").out("none").out("-gravity").out("South").out("(").out("-clone").out("0").out("(").out(path).out("-virtual-pixel").out("transparent").out("-resize").out("365x179!").out("+distort").out("Perspective").out("0,0 207,268 365,0 548,271 365,179 558,450 0,179 193,450").out(")").out("-geometry").out("-25-1").out("-composite").out(")").out("+swap").out("-composite"); - const resultBuffer = await gmToBuffer(command); + const resultBuffer = await gmToBuffer(command, "png"); return message.channel.createMessage("", { file: resultBuffer, name: "trump.png" diff --git a/commands/waaw.js b/commands/waaw.js index 6d6e3d5..b726065 100644 --- a/commands/waaw.js +++ b/commands/waaw.js @@ -3,17 +3,14 @@ const gm = require("gm").subClass({ imageMagick: true }); -const fetch = require("node-fetch"); 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 mirror!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(imageBuffer).gravity("East").crop("50%", 0).strip().write(data2, (error) => { + gm(image.data).gravity("East").crop("50%", 0).strip().write(data2, (error) => { if (error) console.error; gm(data2).flop().strip().write(data, async (error) => { if (error) console.error; diff --git a/commands/wall.js b/commands/wall.js index 5c8624c..df28361 100644 --- a/commands/wall.js +++ b/commands/wall.js @@ -2,15 +2,12 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); 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 wall from!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); - gm(imageBuffer).resize(128).stream(async (error, output) => { + gm(image.data).resize(128).stream(async (error, output) => { if (error) console.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 resultBuffer = await gmToBuffer(data); diff --git a/commands/wdt.js b/commands/wdt.js index 9ad0b50..354b139 100644 --- a/commands/wdt.js +++ b/commands/wdt.js @@ -2,7 +2,6 @@ const gm = require("gm").subClass({ imageMagick: true }); const gmToBuffer = require("../utils/gmbuffer.js"); -const fetch = require("node-fetch"); const fs = require("fs"); exports.run = async (message) => { @@ -10,12 +9,10 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to make a "who did this" meme!`; message.channel.sendTyping(); const template = "./assets/images/whodidthis.png"; - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - require("util").promisify(fs.writeFile)(path, imageBuffer); + require("util").promisify(fs.writeFile)(path, image.data); const command = gm(template).composite(path).gravity("Center").geometry("374x374+0+0"); - const resultBuffer = await gmToBuffer(command); + const resultBuffer = await gmToBuffer(command, "png"); return message.channel.createMessage("", { file: resultBuffer, name: "wdt.png" diff --git a/commands/woow.js b/commands/woow.js index f4209ee..55fe53e 100644 --- a/commands/woow.js +++ b/commands/woow.js @@ -3,17 +3,14 @@ const gm = require("gm").subClass({ imageMagick: true }); -const fetch = require("node-fetch"); 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 mirror!`; message.channel.sendTyping(); - const imageData = await fetch(image.url); - const imageBuffer = await imageData.buffer(); const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(imageBuffer).gravity("North").crop(0, "50%").strip().write(data2, (error) => { + gm(image.data).gravity("North").crop(0, "50%").strip().write(data2, (error) => { if (error) console.error; gm(data2).flip().strip().write(data, async (error) => { if (error) console.error; diff --git a/events/messageCreate.js b/events/messageCreate.js index 55cee5f..7eaa0ec 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -101,7 +101,7 @@ module.exports = async (message) => { } } catch (error) { logger.error(error.stack); - await client.createMessage(message.channel.id, "Uh oh! I ran into an error while running this command. Please report the content of the attached file here: ", [{ + await client.createMessage(message.channel.id, "Uh oh! I ran into an error while running this command. Please report the content of the attached file here or on the esmBot Support server: ", [{ file: Buffer.from(`Message: ${error}\n\nStack Trace: ${error.stack}`), name: "error.txt" }]); diff --git a/utils/imagedetect.js b/utils/imagedetect.js index b539a90..c0d7f36 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -14,14 +14,14 @@ const typeCheck = async (image) => { // if it is, then return the url with the file type return { type: imageType.ext, - url: image + data: imageBuffer }; } else { - // if not, then return a message - return "Not the correct file type"; + // if not, then return false + return false; } } catch (error) { - if (error) console.error; + throw error; } }; @@ -36,7 +36,7 @@ module.exports = async (cmdMessage) => { // get type of file const type = await typeCheck(message.attachments[0].url); // move to the next message if the file isn't an image - if (type === "Not the correct file type") continue; + if (type === false) continue; // if the file is an image then return it return type; // if there's nothing in the attachments check the embeds next @@ -44,12 +44,12 @@ module.exports = async (cmdMessage) => { // 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 === "Not the correct file type") continue; + 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 === "Not the correct file type") continue; + if (type === false) continue; return type; } } diff --git a/utils/logger.js b/utils/logger.js index d2544be..2777c04 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -9,7 +9,7 @@ const logger = winston.createLogger({ format: winston.format.printf(log => `[${moment().format("YYYY-MM-DD HH:mm:ss")}]: [${log.level.toUpperCase()}] - ${log.message}`) }); -exports.log = (type, content) => content ? logger.log(type, content) : logger.log("info", content); +exports.log = (type, content) => content ? logger.log(type, content) : logger.log("info", type); exports.error = (...args) => this.log("error", ...args); diff --git a/utils/misc.js b/utils/misc.js index f84b544..97feb92 100644 --- a/utils/misc.js +++ b/utils/misc.js @@ -19,7 +19,8 @@ exports.clean = async (text) => { .replace(config.catToken, "") .replace(config.googleKey, "") .replace(config.cseID, "") - .replace(config.dblToken, ""); + .replace(config.dblToken, "") + .replace(config.mongoURL, ""); return text; }; diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 93eb0d9..b34dc65 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -6,18 +6,24 @@ module.exports = async (sound, message) => { if (message.member.voiceState.channelID) { if (!message.channel.guild.members.get(client.user.id).permission.has("voiceConnect") || !message.channel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I can't join this voice channel!`); const voiceChannel = message.channel.guild.channels.get(message.member.voiceState.channelID); - client.createMessage(message.channel.id, "🔊 Playing sound..."); + if (!voiceChannel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I don't have permission to join this voice channel!`); + const playingMessage = await client.createMessage(message.channel.id, "🔊 Playing sound..."); const connection = await client.joinVoiceChannel(voiceChannel.id); + console.log(connection.current); if (connection.playing) { connection.stopPlaying(); + connection.play(fs.createReadStream(sound)); + } else { + connection.play(fs.createReadStream(sound)); } - connection.play(fs.createReadStream(sound)); connection.on("error", (error) => { voiceChannel.leave(); + playingMessage.delete(); logger.error(error); }); connection.once("end", () => { voiceChannel.leave(); + playingMessage.delete(); }); } else { client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`);