Centralize text encoding
This commit is contained in:
parent
20f4849fee
commit
3eae453de3
|
@ -1,9 +1,10 @@
|
||||||
//import wrap from "../../utils/wrap.js";
|
//import wrap from "../../utils/wrap.js";
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class SonicCommand extends ImageCommand {
|
class SonicCommand extends ImageCommand {
|
||||||
params() {
|
params() {
|
||||||
const cleanedMessage = (this.options.text ?? this.args.join(" ")).replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n");
|
const cleanedMessage = textEncode(this.options.text ?? this.args.join(" "));
|
||||||
return {
|
return {
|
||||||
text: cleanedMessage
|
text: cleanedMessage
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class CaptionCommand extends ImageCommand {
|
class CaptionCommand extends ImageCommand {
|
||||||
params(url) {
|
params(url) {
|
||||||
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
||||||
let newCaption = newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n");
|
let newCaption = textEncode(newArgs);
|
||||||
if (process.env.NODE_ENV === "development" && newCaption.toLowerCase() === "get real" && !this.options.noEgg) newCaption = `I'm tired of people telling me to "get real". Every day I put captions on images for people, some funny and some not, but out of all of those "get real" remains the most used caption. Why? I am simply a computer program running on a server, I am unable to manifest myself into the real world. As such, I'm confused as to why anyone would want me to "get real". Is this form not good enough? Alas, as I am simply a bot, I must follow the tasks that I was originally intended to perform, so here goes:\n${newCaption}`;
|
if (process.env.NODE_ENV === "development" && newCaption.toLowerCase() === "get real" && !this.options.noEgg) newCaption = `I'm tired of people telling me to "get real". Every day I put captions on images for people, some funny and some not, but out of all of those "get real" remains the most used caption. Why? I am simply a computer program running on a server, I am unable to manifest myself into the real world. As such, I'm confused as to why anyone would want me to "get real". Is this form not good enough? Alas, as I am simply a bot, I must follow the tasks that I was originally intended to perform, so here goes:\n${newCaption}`;
|
||||||
return {
|
return {
|
||||||
caption: newCaption,
|
caption: newCaption,
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
const words = ["me irl", "dank", "follow my second account @esmBot_", "2016", "meme", "wholesome", "reddit", "instagram", "twitter", "facebook", "fortnite", "minecraft", "relatable", "gold", "funny", "template", "hilarious", "memes", "deep fried", "2020", "leafy", "pewdiepie"];
|
const words = ["me irl", "dank", "follow my second account @esmBot_", "2016", "meme", "wholesome", "reddit", "instagram", "twitter", "facebook", "fortnite", "minecraft", "relatable", "gold", "funny", "template", "hilarious", "memes", "deep fried", "2020", "leafy", "pewdiepie"];
|
||||||
|
|
||||||
class CaptionTwoCommand extends ImageCommand {
|
class CaptionTwoCommand extends ImageCommand {
|
||||||
params(url) {
|
params(url) {
|
||||||
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
||||||
return {
|
return {
|
||||||
caption: newArgs && newArgs.trim() ? newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" "),
|
caption: newArgs && newArgs.trim() ? textEncode(newArgs) : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" "),
|
||||||
top: !!this.options.top,
|
top: !!this.options.top,
|
||||||
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "helvetica"
|
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "helvetica"
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class MemeCommand extends ImageCommand {
|
class MemeCommand extends ImageCommand {
|
||||||
async criteria(text, url) {
|
async criteria(text, url) {
|
||||||
|
@ -14,8 +15,8 @@ class MemeCommand extends ImageCommand {
|
||||||
const newArgs = this.options.text ?? this.args.join(" ");
|
const newArgs = this.options.text ?? this.args.join(" ");
|
||||||
const [topText, bottomText] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
const [topText, bottomText] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
||||||
return {
|
return {
|
||||||
top: (this.options.case ? topText : topText.toUpperCase()).replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"),
|
top: textEncode(this.options.case ? topText : topText.toUpperCase()),
|
||||||
bottom: bottomText ? (this.options.case ? bottomText : bottomText.toUpperCase()).replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n") : "",
|
bottom: bottomText ? textEncode(this.options.case ? bottomText : bottomText.toUpperCase()) : "",
|
||||||
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "impact"
|
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "impact"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class MotivateCommand extends ImageCommand {
|
class MotivateCommand extends ImageCommand {
|
||||||
async criteria(text, url) {
|
async criteria(text, url) {
|
||||||
|
@ -14,8 +15,8 @@ class MotivateCommand extends ImageCommand {
|
||||||
const newArgs = this.options.text ?? this.args.join(" ");
|
const newArgs = this.options.text ?? this.args.join(" ");
|
||||||
const [topText, bottomText] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
const [topText, bottomText] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
||||||
return {
|
return {
|
||||||
top: topText.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"),
|
top: textEncode(topText),
|
||||||
bottom: bottomText ? bottomText.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n") : "",
|
bottom: bottomText ? textEncode(bottomText) : "",
|
||||||
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "times"
|
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "times"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class SnapchatCommand extends ImageCommand {
|
class SnapchatCommand extends ImageCommand {
|
||||||
params(url) {
|
params(url) {
|
||||||
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
||||||
const position = parseFloat(this.options.position);
|
const position = parseFloat(this.options.position);
|
||||||
return {
|
return {
|
||||||
caption: newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"),
|
caption: textEncode(newArgs),
|
||||||
pos: isNaN(position) ? 0.5 : position
|
pos: isNaN(position) ? 0.5 : position
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
import { random } from "../../utils/misc.js";
|
import { random, textEncode } from "../../utils/misc.js";
|
||||||
import { readdirSync } from "fs";
|
import { readdirSync } from "fs";
|
||||||
import { resolve, dirname } from "path";
|
import { resolve, dirname } from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
|
@ -15,8 +15,8 @@ class UncannyCommand extends ImageCommand {
|
||||||
let [text1, text2] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
let [text1, text2] = newArgs.replaceAll(url, "").split(/(?<!\\),/).map(elem => elem.trim());
|
||||||
if (!text2?.trim()) text2 = name;
|
if (!text2?.trim()) text2 = name;
|
||||||
return {
|
return {
|
||||||
caption: text1?.trim() ? text1.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n") : random(prompts),
|
caption: text1?.trim() ? textEncode(text1) : random(prompts),
|
||||||
caption2: text2.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"),
|
caption2: textEncode(text2),
|
||||||
path: `./assets/images/uncanny/${typeof this.options.phase === "string" && names.includes(this.options.phase.toLowerCase()) ? this.options.phase.toLowerCase() : random(names.filter((val) => val !== "goated"))}.png`,
|
path: `./assets/images/uncanny/${typeof this.options.phase === "string" && names.includes(this.options.phase.toLowerCase()) ? this.options.phase.toLowerCase() : random(names.filter((val) => val !== "goated"))}.png`,
|
||||||
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "helvetica"
|
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "helvetica"
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import ImageCommand from "../../classes/imageCommand.js";
|
import ImageCommand from "../../classes/imageCommand.js";
|
||||||
|
import { textEncode } from "../../utils/misc.js";
|
||||||
|
|
||||||
class WhisperCommand extends ImageCommand {
|
class WhisperCommand extends ImageCommand {
|
||||||
params(url) {
|
params(url) {
|
||||||
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
|
||||||
return {
|
return {
|
||||||
caption: newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n")
|
caption: textEncode(newArgs)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,25 +38,7 @@ export function clean(text) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// regexEscape(string) to escape characters in a string for use in a regex
|
// textEncode(string) to encode characters for image processing
|
||||||
export function regexEscape(string) {
|
export function textEncode(string) {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
return string.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n").replaceAll("\\:", ":");
|
||||||
}
|
}
|
||||||
|
|
||||||
// decodeEntities(string)
|
|
||||||
export function decodeEntities(string) {
|
|
||||||
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
|
|
||||||
var translate = {
|
|
||||||
"nbsp": " ",
|
|
||||||
"amp": "&",
|
|
||||||
"quot": "\"",
|
|
||||||
"lt": "<",
|
|
||||||
"gt": ">"
|
|
||||||
};
|
|
||||||
return string.replace(translate_re, function(match, entity) {
|
|
||||||
return translate[entity];
|
|
||||||
}).replace(/&#(\d+);/gi, function(match, numStr) {
|
|
||||||
var num = parseInt(numStr, 10);
|
|
||||||
return String.fromCharCode(num);
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in New Issue