Added snapchat, prevent image queue packet from being sent twice

This commit is contained in:
TheEssem 2021-06-14 22:03:48 -05:00
parent 0ed7d1f57a
commit 52d91b752d
No known key found for this signature in database
GPG key ID: A3F9F02129092FCA
5 changed files with 119 additions and 14 deletions

View file

@ -0,0 +1,21 @@
const ImageCommand = require("../../classes/imageCommand.js");
class SnapchatCommand extends ImageCommand {
params(args, url) {
const newArgs = args.filter(item => !item.includes(url));
return {
caption: newArgs.join(" ").replaceAll("&", "\\&amp;").replaceAll(">", "\\&gt;").replaceAll("<", "\\&lt;").replaceAll("\"", "\\&quot;").replaceAll("'", "\\&apos;").replaceAll("%", "\\%")
};
}
static description = "Adds a Snapchat style caption to an image";
static aliases = ["snap", "caption3"];
static arguments = ["[text]"];
static requiresText = true;
static noText = "You need to provide some text to add a caption!";
static noImage = "You need to provide an image to add a caption!";
static command = "snapchat";
}
module.exports = SnapchatCommand;

View file

@ -26,6 +26,7 @@
#include "retro.h"
#include "reverse.h"
#include "scott.h"
#include "snapchat.h"
#include "speed.h"
#include "sonic.h"
#include "spin.h"
@ -63,6 +64,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "retro"), Napi::Function::New(env, Retro));
exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse));
exports.Set(Napi::String::New(env, "scott"), Napi::Function::New(env, Scott));
exports.Set(Napi::String::New(env, "snapchat"), Napi::Function::New(env, Snapchat));
exports.Set(Napi::String::New(env, "speed"), Napi::Function::New(env, Speed));
exports.Set(Napi::String::New(env, "sonic"), Napi::Function::New(env, Sonic));
exports.Set(Napi::String::New(env, "spin"), Napi::Function::New(env, Spin));

73
natives/snapchat.cc Normal file
View file

@ -0,0 +1,73 @@
#include <Magick++.h>
#include <napi.h>
#include <list>
using namespace std;
using namespace Magick;
Napi::Value Snapchat(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
Napi::Object obj = info[0].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay =
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
Blob blob;
list<Image> frames;
list<Image> coalesced;
list<Image> captioned;
Blob caption_blob;
readImages(&frames, Blob(data.Data(), data.Length()));
size_t width = frames.front().baseColumns();
string query(to_string(width - ((width / 25) * 2)) + "x");
Image caption_image(Geometry(query), Color("none"));
caption_image.backgroundColor(Color("none"));
caption_image.fillColor("white");
caption_image.font("Helvetica Neue");
caption_image.fontPointsize(width / 25);
caption_image.textGravity(Magick::CenterGravity);
caption_image.read("pango:" + caption);
caption_image.backgroundColor(Color("rgba(0, 0, 0, 0.7)"));
caption_image.extent(Geometry(width, caption_image.rows() + (width / 25)),
Magick::CenterGravity);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
list<Image> images;
image.composite(caption_image, Magick::CenterGravity,
Magick::OverCompositeOp);
image.magick(type);
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
captioned.push_back(image);
}
optimizeTransparency(captioned.begin(), captioned.end());
if (type == "gif") {
for (Image &image : captioned) {
image.quantizeDither(false);
image.quantize();
}
}
writeImages(captioned.begin(), captioned.end(), &blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", type);
return result;
} catch (std::exception const &err) {
throw Napi::Error::New(env, err.what());
} catch (...) {
throw Napi::Error::New(env, "Unknown error");
}
}

8
natives/snapchat.h Normal file
View file

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

View file

@ -182,22 +182,23 @@ const start = (object, num) => {
const res = start(object, num);
reject(res); // this is done to differentiate the result from a step
});
}
currentServer.write(data, (err) => {
if (err) {
if (err.code === "EPIPE") {
logger.log(`Lost connection to ${currentServer.remoteAddress}, attempting to reconnect...`);
currentServer.connect(8080, currentServer.remoteAddress, () => {
const res = start(object, num);
reject(res); // this is done to differentiate the result from a step
});
} else {
currentServer.write(data, (err) => {
if (err) {
if (err.code === "EPIPE") {
logger.log(`Lost connection to ${currentServer.remoteAddress}, attempting to reconnect...`);
currentServer.connect(8080, currentServer.remoteAddress, () => {
const res = start(object, num);
reject(res); // this is done to differentiate the result from a step
});
} else {
reject(err);
}
} else {
reject(err);
resolve(currentServer.remoteAddress);
}
} else {
resolve(currentServer.remoteAddress);
}
});
});
}
});
}).catch((result) => {
throw result;