From 18330d3db9a850a347130ce93cb207f7a84cf11f Mon Sep 17 00:00:00 2001 From: TheEssem Date: Thu, 23 Apr 2020 17:38:01 -0500 Subject: [PATCH] Remove image URL from arguments of image commands --- commands/caption.js | 3 ++- commands/caption2.js | 3 ++- commands/magik.js | 2 +- commands/meme.js | 3 ++- commands/motivate.js | 3 ++- utils/imagedetect.js | 15 ++++++++------- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/commands/caption.js b/commands/caption.js index 9f36d96..45cdb1a 100644 --- a/commands/caption.js +++ b/commands/caption.js @@ -6,10 +6,11 @@ const gm = require("gm").subClass({ 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!`; + const newArgs = args.filter(item => !item.includes(image.url) ); 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"); const size = await gm(image.path).sizePromise(); - const output = await gm().out("-size", `${size.width - ((size.width / 25) * 2)}x`).background("white").fill("black").font("./assets/caption.otf", size.width / 10).gravity("Center").out(`caption:${args.join(" ")}`).extent(size.width, `%[fx:h+${size.width / 25}]`).bufferPromise("png"); + const output = await gm().out("-size", `${size.width - ((size.width / 25) * 2)}x`).background("white").fill("black").font("./assets/caption.otf", size.width / 10).gravity("Center").out(`caption:${newArgs.join(" ")}`).extent(size.width, `%[fx:h+${size.width / 25}]`).bufferPromise("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 outputFinal = await gm(output).in("(").gravity("Center").trim().out("+repage").extent(size.width, size2.height + (size.width / 10)).out(")").background("white").out("-alpha", "set").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").bufferPromise(image.type, image.delay); diff --git a/commands/caption2.js b/commands/caption2.js index 3f66fcd..838d607 100644 --- a/commands/caption2.js +++ b/commands/caption2.js @@ -6,10 +6,11 @@ const words = ["me irl", "dank", "follow my second account @esmBot_", "2016", "m 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!`; + const newArgs = args.filter(item => !item.includes(image.url) ); const processMessage = await message.channel.createMessage(" Processing... This might take a while"); const text = `/tmp/${Math.random().toString(36).substring(2, 15)}.png`; const size = await gm(image.path).sizePromise(); - await gm().out("-size", `${size.width - ((size.width / 25) * 2)}x`).background("white").fill("black").font("Helvetica Neue").out("-pointsize", size.width / 17).out(`pango:${args.length !== 0 ? args.join(" ") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" ")}`).gravity("Center").extent(size.width, `%[fx:h+${size.width / 25}]`).writePromise(text); + await gm().out("-size", `${size.width - ((size.width / 25) * 2)}x`).background("white").fill("black").font("Helvetica Neue").out("-pointsize", size.width / 17).out(`pango:${newArgs.length !== 0 ? newArgs.join(" ") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" ")}`).gravity("Center").extent(size.width, `%[fx:h+${size.width / 25}]`).writePromise(text); const size2 = await gm(text).sizePromise(); const outputFinal = await gm(image.path).gravity("North").extent(size.width, size2.height + size.height).out("null:", "(", text, "-set", "page", `+0+${size.height}`, ")", "-layers", "composite", "-layers", "optimize").bufferPromise(image.type, image.delay); await processMessage.delete(); diff --git a/commands/magik.js b/commands/magik.js index 2a35dc7..34585f3 100644 --- a/commands/magik.js +++ b/commands/magik.js @@ -7,7 +7,7 @@ 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") return `${message.author.mention}, this command doesn't work with GIFs!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); - const resultBuffer = await gm(image.path).in("(").in("(").coalesce().scale(600, 600).out(")").out("-liquid-rescale", "300x300").out(")").out("-liquid-rescale", "800x800").bufferPromise(image.type, image.delay); + const resultBuffer = await gm(image.path).in("(").in("(").coalesce().scale(600, 600).out(")").out("-liquid-rescale", "300x300").out(")").out("-liquid-rescale", "600x600").bufferPromise(image.type, image.delay); await processMessage.delete(); return { file: resultBuffer, diff --git a/commands/meme.js b/commands/meme.js index ac38fdf..0d7470c 100644 --- a/commands/meme.js +++ b/commands/meme.js @@ -6,8 +6,9 @@ exports.run = async (message, args) => { 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 generate a meme!`; + const newArgs = args.filter(item => !item.includes(image.url) ); if (args.length === 0) return `${message.author.mention}, you need to provide some text to generate a meme!`; - const [topText, bottomText] = args.join(" ").split(/(? elem.trim()); + const [topText, bottomText] = newArgs.join(" ").split(/(? elem.trim()); 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`; diff --git a/commands/motivate.js b/commands/motivate.js index e5232c7..2c8b689 100644 --- a/commands/motivate.js +++ b/commands/motivate.js @@ -5,9 +5,10 @@ const gm = require("gm").subClass({ 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 make a motivational poster!`; + const newArgs = args.filter(item => !item.includes(image.url) ); 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(/(? elem.trim()); + const [topText, bottomText] = newArgs.join(" ").split(/(? elem.trim()); const file = `/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`; diff --git a/utils/imagedetect.js b/utils/imagedetect.js index cd22b72..6f1f075 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -6,7 +6,7 @@ const execPromise = promisify(require("child_process").exec); const urlRegex = /(?:\w+:)?\/\/(\S+)/; // this checks if the file is, in fact, an image -const typeCheck = async (image, gifv = false) => { +const typeCheck = async (image, image2, gifv = false) => { // download the file to a buffer const imageRequest = await fetch(image); const imageBuffer = await imageRequest.buffer(); @@ -23,7 +23,8 @@ const typeCheck = async (image, gifv = false) => { const payload = { data: imageBuffer, type: imageType.ext !== "mp4" ? (imageType.ext === "jpg" ? "jpeg" : imageType.ext) : "gif", - path: path + path: path, + url: image2 }; if (gifv) payload.delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${path}`)).stdout.replace("\n", ""); return payload; @@ -46,22 +47,22 @@ module.exports = async (cmdMessage) => { 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); + const type = await typeCheck(message.embeds[0].video.url, 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.proxy_url); + const type = await typeCheck(message.embeds[0].thumbnail.proxy_url, 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.proxy_url); + const type = await typeCheck(message.embeds[0].image.proxy_url, 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].proxy_url); + const type = await typeCheck(message.attachments[0].proxy_url, message.attachments[0].url); // move to the next message if the file isn't an image if (type === false) continue; // if the file is an image then return it @@ -71,7 +72,7 @@ module.exports = async (cmdMessage) => { // get url const url = message.content.match(urlRegex); // get type of file - const type = await typeCheck(url[0]); + const type = await typeCheck(url[0], url[0]); // move to the next message if the file isn't an image if (type === false) continue; // if the file is an image then return it