From 0ebd0a0cee3ae27bfb911dab9d99ac37307ca5a7 Mon Sep 17 00:00:00 2001 From: TheEssem Date: Thu, 23 Jul 2020 17:43:39 -0500 Subject: [PATCH] Added crop and loop, fixed database not properly adding new commands --- commands/crop.js | 16 +++++++++++++ commands/freeze.js | 2 +- commands/loop.js | 18 +++++++++++++++ commands/warn.js | 1 - events/ready.js | 2 +- natives/crop.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++ natives/crop.h | 8 +++++++ natives/freeze.cc | 23 ++++++++++--------- natives/image.cc | 2 ++ 9 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 commands/crop.js create mode 100644 commands/loop.js create mode 100644 natives/crop.cc create mode 100644 natives/crop.h diff --git a/commands/crop.js b/commands/crop.js new file mode 100644 index 0000000..c6a44aa --- /dev/null +++ b/commands/crop.js @@ -0,0 +1,16 @@ +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); + +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 crop!`; + const buffer = await promisify(magick.crop)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + return { + file: buffer, + name: `crop.${image.type}` + }; +}; + +exports.category = 5; +exports.help = "Crops an image to 1:1"; \ No newline at end of file diff --git a/commands/freeze.js b/commands/freeze.js index e3f5974..8e06138 100644 --- a/commands/freeze.js +++ b/commands/freeze.js @@ -6,7 +6,7 @@ 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 freeze!`; if (image.type !== "gif") return `${message.author.mention}, that isn't a GIF!`; - const buffer = await promisify(magick.freeze)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + const buffer = await promisify(magick.freeze)(image.path, false, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `freeze.${image.type}` diff --git a/commands/loop.js b/commands/loop.js new file mode 100644 index 0000000..dcc2552 --- /dev/null +++ b/commands/loop.js @@ -0,0 +1,18 @@ +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); + +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 loop!`; + if (image.type !== "gif") return `${message.author.mention}, that isn't a GIF!`; + const buffer = await promisify(magick.freeze)(image.path, true, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + return { + file: buffer, + name: `loop.${image.type}` + }; +}; + +exports.aliases = ["unfreeze"]; +exports.category = 5; +exports.help = "Makes a GIF loop endlessly"; \ No newline at end of file diff --git a/commands/warn.js b/commands/warn.js index 03f9933..cf4bf97 100644 --- a/commands/warn.js +++ b/commands/warn.js @@ -25,7 +25,6 @@ exports.run = async (message, args) => { if (!message.channel.guild.members.get(client.user.id).permission.has("embedLinks") && !message.channel.permissionsOf(client.user.id).has("embedLinks")) return `${message.author.mention}, I don't have the \`Embed Links\` permission!`; const warnArray = []; for (const [i, value] of array.entries()) { - console.log(value); warnArray.push(`**${i + 1}: Added by ${message.channel.guild.members.get(value.creator).username}#${message.channel.guild.members.get(value.creator).discriminator}**: ${value.message} (${new Date(value.time).toUTCString()})`); } const pageSize = 15; diff --git a/events/ready.js b/events/ready.js index c844e1b..0a95272 100644 --- a/events/ready.js +++ b/events/ready.js @@ -55,7 +55,7 @@ module.exports = async () => { } else { for (const command of collections.commands.keys()) { const count = await database.query("SELECT * FROM counts WHERE command = $1", [command]); - if (!count) { + if (!count.rows[0]) { await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]); } } diff --git a/natives/crop.cc b/natives/crop.cc new file mode 100644 index 0000000..60967f3 --- /dev/null +++ b/natives/crop.cc @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; +using namespace Magick; + +class CropWorker : public Napi::AsyncWorker { + public: + CropWorker(Napi::Function& callback, string in_path, string type, int delay) + : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} + ~CropWorker() {} + + void Execute() { + list frames; + list coalesced; + list mid; + list result; + readImages(&frames, in_path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.extent(Geometry(to_string(image.columns() / image.rows() >= 1 ? image.rows() : image.columns()) + "x"), Magick::CenterGravity); + image.extent(Geometry("x" + to_string(image.columns() / image.rows() <= 1 ? image.columns() : image.rows())), Magick::CenterGravity); + image.magick(type); + mid.push_back(image); + } + + optimizeImageLayers(&result, mid.begin(), mid.end()); + if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); + writeImages(result.begin(), result.end(), &blob); + } + + void OnOK() { + Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length())}); + } + + private: + string in_path, type; + int delay, wordlength, i, n; + size_t bytes, type_size; + Blob blob; +}; + +Napi::Value Crop(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + + string in_path = info[0].As().Utf8Value(); + string type = info[1].As().Utf8Value(); + int delay = info[2].As().Int32Value(); + Napi::Function cb = info[3].As(); + + CropWorker* blurWorker = new CropWorker(cb, in_path, type, delay); + blurWorker->Queue(); + return env.Undefined(); +} \ No newline at end of file diff --git a/natives/crop.h b/natives/crop.h new file mode 100644 index 0000000..df3d504 --- /dev/null +++ b/natives/crop.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_CROP_H_ +#define ESMBOT_NATIVES_CROP_H_ + +#include + +Napi::Value Crop(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/freeze.cc b/natives/freeze.cc index d0e8b83..41d80e2 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,20 +8,18 @@ using namespace Magick; class FreezeWorker : public Napi::AsyncWorker { public: - FreezeWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} + FreezeWorker(Napi::Function& callback, string in_path, bool loop, string type, int delay) + : Napi::AsyncWorker(callback), in_path(in_path), loop(loop), type(type), delay(delay) {} ~FreezeWorker() {} void Execute() { list frames; - list result; readImages(&frames, in_path); - for_each(frames.begin(), frames.end(), animationIterationsImage(1)); + for_each(frames.begin(), frames.end(), animationIterationsImage(loop ? 0 : 1)); - optimizeImageLayers(&result, frames.begin(), frames.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); - writeImages(result.begin(), result.end(), &blob); + if (delay != 0) for_each(frames.begin(), frames.end(), animationDelayImage(delay)); + writeImages(frames.begin(), frames.end(), &blob); } void OnOK() { @@ -32,6 +31,7 @@ class FreezeWorker : public Napi::AsyncWorker { int delay, wordlength, i, n; size_t bytes, type_size; Blob blob; + bool loop; }; Napi::Value Freeze(const Napi::CallbackInfo &info) @@ -39,11 +39,12 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) Napi::Env env = info.Env(); string in_path = info[0].As().Utf8Value(); - string type = info[1].As().Utf8Value(); - int delay = info[2].As().Int32Value(); - Napi::Function cb = info[3].As(); + bool loop = info[1].As().Value(); + string type = info[2].As().Utf8Value(); + int delay = info[3].As().Int32Value(); + Napi::Function cb = info[4].As(); - FreezeWorker* blurWorker = new FreezeWorker(cb, in_path, type, delay); + FreezeWorker* blurWorker = new FreezeWorker(cb, in_path, loop, type, delay); blurWorker->Queue(); return env.Undefined(); } \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index e2ece82..39bf078 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -4,6 +4,7 @@ #include "caption.h" #include "caption2.h" #include "circle.h" +#include "crop.h" #include "explode.h" #include "flag.h" #include "flip.h" @@ -39,6 +40,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) exports.Set(Napi::String::New(env, "caption"), Napi::Function::New(env, Caption)); exports.Set(Napi::String::New(env, "captionTwo"), Napi::Function::New(env, CaptionTwo)); exports.Set(Napi::String::New(env, "circle"), Napi::Function::New(env, Circle)); + exports.Set(Napi::String::New(env, "crop"), Napi::Function::New(env, Crop)); exports.Set(Napi::String::New(env, "explode"), Napi::Function::New(env, Explode)); exports.Set(Napi::String::New(env, "flag"), Napi::Function::New(env, Flag)); exports.Set(Napi::String::New(env, "flip"), Napi::Function::New(env, Flip));