Added crop and loop, fixed database not properly adding new commands

This commit is contained in:
TheEssem 2020-07-23 17:43:39 -05:00
parent 10e934e722
commit 0ebd0a0cee
9 changed files with 115 additions and 14 deletions

16
commands/crop.js Normal file
View file

@ -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";

View file

@ -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}`

18
commands/loop.js Normal file
View file

@ -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";

View file

@ -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;

View file

@ -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]);
}
}

57
natives/crop.cc Normal file
View file

@ -0,0 +1,57 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<char>::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<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
CropWorker* blurWorker = new CropWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/crop.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_CROP_H_
#define ESMBOT_NATIVES_CROP_H_
#include <napi.h>
Napi::Value Crop(const Napi::CallbackInfo& info);
#endif

View file

@ -1,4 +1,5 @@
#include <napi.h>
#include <iostream>
#include <list>
#include <Magick++.h>
@ -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<Image> frames;
list<Image> 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<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
bool loop = info[1].As<Napi::Boolean>().Value();
string type = info[2].As<Napi::String>().Utf8Value();
int delay = info[3].As<Napi::Number>().Int32Value();
Napi::Function cb = info[4].As<Napi::Function>();
FreezeWorker* blurWorker = new FreezeWorker(cb, in_path, type, delay);
FreezeWorker* blurWorker = new FreezeWorker(cb, in_path, loop, type, delay);
blurWorker->Queue();
return env.Undefined();
}

View file

@ -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));