From 6b34cb9d9e6558431da1c7663515f35f16f6bd69 Mon Sep 17 00:00:00 2001 From: Essem Date: Tue, 7 Jun 2022 18:26:40 -0500 Subject: [PATCH] Remove delay parameter, change argument handling, make help command show proper flag types --- api/IMPLEMENTATION.md | 1 - classes/command.js | 3 +- classes/imageCommand.js | 5 +- commands/fun/dice.js | 2 +- commands/fun/homebrew.js | 2 +- commands/fun/sonic.js | 2 +- commands/general/base64.js | 2 +- commands/general/broadcast.js | 2 +- commands/general/emote.js | 2 +- commands/general/eval.js | 2 +- commands/general/exec.js | 2 +- commands/general/help.js | 15 ++-- commands/general/image.js | 2 +- commands/general/lengthen.js | 2 +- commands/general/reload.js | 2 +- commands/general/youtube.js | 2 +- commands/image-editing/caption.js | 2 +- commands/image-editing/caption2.js | 2 +- commands/image-editing/flag.js | 2 +- commands/image-editing/freeze.js | 2 +- commands/image-editing/jpeg.js | 2 +- commands/image-editing/meme.js | 2 +- commands/image-editing/motivate.js | 2 +- commands/image-editing/reddit.js | 4 +- commands/image-editing/reverse.js | 6 -- commands/image-editing/slow.js | 2 +- commands/image-editing/snapchat.js | 2 +- commands/image-editing/soos.js | 3 +- commands/image-editing/speed.js | 2 +- commands/image-editing/whisper.js | 2 +- commands/music/host.js | 2 +- commands/music/play.js | 5 +- commands/music/remove.js | 2 +- commands/music/seek.js | 2 +- commands/music/skip.js | 1 - natives/blur.cc | 4 -- natives/caption.cc | 3 - natives/caption2.cc | 3 - natives/circle.cc | 3 - natives/colors.cc | 3 - natives/crop.cc | 3 - natives/deepfry.cc | 8 +-- natives/flag.cc | 3 - natives/flip.cc | 4 -- natives/freeze.cc | 4 -- natives/gamexplain.cc | 3 - natives/invert.cc | 4 -- natives/jpeg.cc | 8 +-- natives/meme.cc | 3 - natives/mirror.cc | 4 -- natives/motivate.cc | 3 - natives/reddit.cc | 3 - natives/resize.cc | 3 - natives/scott.cc | 4 +- natives/snapchat.cc | 3 - natives/speed.cc | 107 +++++++++-------------------- natives/tile.cc | 4 +- natives/uncaption.cc | 3 - natives/wall.cc | 3 - natives/watermark.cc | 3 - natives/wdt.cc | 4 +- natives/whisper.cc | 3 - natives/zamn.cc | 3 - 63 files changed, 82 insertions(+), 219 deletions(-) diff --git a/api/IMPLEMENTATION.md b/api/IMPLEMENTATION.md index 0f838cf..01fcb4c 100644 --- a/api/IMPLEMENTATION.md +++ b/api/IMPLEMENTATION.md @@ -41,7 +41,6 @@ The job object is formatted like this: "url": string, // original image URL, used for message filtering "params": { // content varies depending on the command, some common parameters are listed here "type": string, // mime type of output, should usually be the same as input - "delay": integer, // for manually specifying GIF frame delay, set to 0 to use the default delay ... } } diff --git a/classes/command.js b/classes/command.js index 56729d7..e776c81 100644 --- a/classes/command.js +++ b/classes/command.js @@ -13,7 +13,7 @@ class Command { this.author = options.message.author; this.member = options.message.member; this.content = options.content; - this.specialArgs = options.specialArgs; + this.specialArgs = this.options = options.specialArgs; this.reference = { messageReference: { channelID: this.channel.id, @@ -27,6 +27,7 @@ class Command { }; } else if (options.type === "application") { this.interaction = options.interaction; + this.args = []; this.channel = options.interaction.channel; this.author = this.member = options.interaction.guildID ? options.interaction.member : options.interaction.user; if (options.interaction.data.options) { diff --git a/classes/imageCommand.js b/classes/imageCommand.js index 5d0c5c5..9191e23 100644 --- a/classes/imageCommand.js +++ b/classes/imageCommand.js @@ -68,7 +68,6 @@ class ImageCommand extends Command { magickParams.path = image.path; magickParams.params.type = image.type; magickParams.url = image.url; // technically not required but can be useful for text filtering - magickParams.params.delay = image.delay ?? 0; if (this.constructor.requiresGIF) magickParams.onlyGIF = true; } catch (e) { runningCommands.delete(this.author.id); @@ -77,7 +76,7 @@ class ImageCommand extends Command { } if (this.constructor.requiresText) { - const text = this.type === "classic" ? this.args : this.options.text; + const text = this.options.text ?? this.args; if (text.length === 0 || !await this.criteria(text)) { runningCommands.delete(this.author.id); return this.constructor.noText; @@ -86,7 +85,7 @@ class ImageCommand extends Command { switch (typeof this.params) { case "function": - Object.assign(magickParams.params, this.params(magickParams.url, magickParams.delay)); + Object.assign(magickParams.params, this.params(magickParams.url)); break; case "object": Object.assign(magickParams.params, this.params); diff --git a/commands/fun/dice.js b/commands/fun/dice.js index f4b4fb8..c085827 100644 --- a/commands/fun/dice.js +++ b/commands/fun/dice.js @@ -2,7 +2,7 @@ import Command from "../../classes/command.js"; class DiceCommand extends Command { async run() { - const max = this.type === "classic" ? parseInt(this.args[0]) : this.options.max; + const max = this.options.max ?? parseInt(this.args[0]); if (!max) { return `🎲 The dice landed on ${Math.floor(Math.random() * 6) + 1}.`; } else { diff --git a/commands/fun/homebrew.js b/commands/fun/homebrew.js index 934f69f..dc60f71 100644 --- a/commands/fun/homebrew.js +++ b/commands/fun/homebrew.js @@ -3,7 +3,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class HomebrewCommand extends ImageCommand { params() { return { - caption: (this.type === "classic" ? this.args.join(" ") : this.options.text).toLowerCase().replaceAll("\n", " ") + caption: (this.options.text ?? this.args.join(" ")).toLowerCase().replaceAll("\n", " ") }; } diff --git a/commands/fun/sonic.js b/commands/fun/sonic.js index 9631713..0cf28c8 100644 --- a/commands/fun/sonic.js +++ b/commands/fun/sonic.js @@ -3,7 +3,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class SonicCommand extends ImageCommand { params() { - const cleanedMessage = (this.type === "classic" ? this.args.join(" ") : this.options.text).replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%"); + const cleanedMessage = (this.options.text ?? this.args.join(" ")).replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%"); return { text: cleanedMessage }; diff --git a/commands/general/base64.js b/commands/general/base64.js index 9e42798..47763b1 100644 --- a/commands/general/base64.js +++ b/commands/general/base64.js @@ -6,7 +6,7 @@ class Base64Command extends Command { if (this.type === "classic" && this.args.length === 0) return "You need to provide whether you want to encode or decode the text!"; const command = this.type === "classic" ? this.args[0].toLowerCase() : this.optionsArray[0].name.toLowerCase(); if (command !== "decode" && command !== "encode") return "You need to provide whether you want to encode or decode the text!"; - const string = this.type === "classic" ? this.args.slice(1).join(" ") : this.options.text; + const string = this.options.text ?? this.args.slice(1).join(" "); if (!string || !string.trim()) return `You need to provide a string to ${command}!`; if (command === "decode") { const b64Decoded = Buffer.from(string, "base64").toString("utf8"); diff --git a/commands/general/broadcast.js b/commands/general/broadcast.js index cd7169a..991abdd 100644 --- a/commands/general/broadcast.js +++ b/commands/general/broadcast.js @@ -6,7 +6,7 @@ class BroadcastCommand extends Command { return new Promise((resolve) => { const owners = process.env.OWNER.split(","); if (!owners.includes(this.author.id)) return "Only the bot owner can broadcast messages!"; - const message = this.type === "classic" ? this.args.join(" ") : this.options.message; + const message = this.options.message ?? this.args.join(" "); if (message && message.trim()) { this.ipc.broadcast("playbroadcast", message); this.ipc.register("broadcastSuccess", () => { diff --git a/commands/general/emote.js b/commands/general/emote.js index 634d6a4..3573a45 100644 --- a/commands/general/emote.js +++ b/commands/general/emote.js @@ -3,7 +3,7 @@ import Command from "../../classes/command.js"; class EmoteCommand extends Command { async run() { - const emoji = this.type === "classic" ? this.content : this.options.emoji; + const emoji = this.options.emoji ?? this.content; if (!emoji || !emoji.trim()) return "You need to provide an emoji!"; if (emoji.split(" ")[0].match(/^$/)) { return `https://cdn.discordapp.com/emojis/${emoji.split(" ")[0].replace(/^<(a)?:.+:(\d+)>$/, "$2")}.${emoji.split(" ")[0].replace(/^<(a)?:.+:(\d+)>$/, "$1") === "a" ? "gif" : "png"}`; diff --git a/commands/general/eval.js b/commands/general/eval.js index f7cc550..9456bc1 100644 --- a/commands/general/eval.js +++ b/commands/general/eval.js @@ -6,7 +6,7 @@ class EvalCommand extends Command { const owners = process.env.OWNER.split(","); if (!owners.includes(this.author.id)) return "Only the bot owner can use eval!"; await this.acknowledge(); - const code = this.type === "classic" ? this.args.join(" ") : this.options.code; + const code = this.options.code ?? this.args.join(" "); try { const evaled = eval(code); const cleaned = await clean(evaled); diff --git a/commands/general/exec.js b/commands/general/exec.js index 85c4f5e..5203675 100644 --- a/commands/general/exec.js +++ b/commands/general/exec.js @@ -9,7 +9,7 @@ class ExecCommand extends Command { const owners = process.env.OWNER.split(","); if (!owners.includes(this.author.id)) return "Only the bot owner can use exec!"; await this.acknowledge(); - const code = this.type === "classic" ? this.args.join(" ") : this.options.cmd; + const code = this.options.cmd ?? this.args.join(" "); try { const execed = await exec(code); if (execed.stderr) return `\`ERROR\` \`\`\`xl\n${await clean(execed.stderr)}\n\`\`\``; diff --git a/commands/general/help.js b/commands/general/help.js index bdb8551..dbd79bd 100644 --- a/commands/general/help.js +++ b/commands/general/help.js @@ -1,3 +1,4 @@ +import { Constants } from "eris"; import database from "../../utils/database.js"; import * as collections from "../../utils/collections.js"; import { random } from "../../utils/misc.js"; @@ -5,6 +6,7 @@ import paginator from "../../utils/pagination/pagination.js"; import * as help from "../../utils/help.js"; import Command from "../../classes/command.js"; const tips = ["You can change the bot's prefix using the prefix command.", "Image commands also work with images previously posted in that channel.", "You can use the tags commands to save things for later use.", "You can visit https://projectlounge.pw/esmBot/help.html for a web version of this command list.", "You can view a command's aliases by putting the command name after the help command (e.g. help image).", "Parameters wrapped in [] are required, while parameters wrapped in {} are optional.", "esmBot is hosted and paid for completely out-of-pocket by the main developer. If you want to support development, please consider donating! https://patreon.com/TheEssem", "You can run commands in DMs as well, just message the bot with your command - no prefix needed!"]; +const argTypes = Object.keys(Constants.ApplicationCommandOptionTypes); class HelpCommand extends Command { async run() { @@ -40,12 +42,15 @@ class HelpCommand extends Command { if (info.flags.length !== 0) { const flagInfo = []; for (const flag of info.flags) { - flagInfo.push(`\`--${flag.name}${flag.type ? `=[${flag.type}]` : ""}\` - ${flag.description}`); + if (flag.type === 1) continue; + flagInfo.push(`\`--${flag.name}${flag.type ? `=[${argTypes[flag.type - 1]}]` : ""}\` - ${flag.description}`); + } + if (flagInfo.length !== 0) { + embed.embeds[0].fields.push({ + "name": "Flags", + "value": flagInfo.join("\n") + }); } - embed.embeds[0].fields.push({ - "name": "Flags", - "value": flagInfo.join("\n") - }); } return embed; } else { diff --git a/commands/general/image.js b/commands/general/image.js index 42bb546..e8cb4e9 100644 --- a/commands/general/image.js +++ b/commands/general/image.js @@ -8,7 +8,7 @@ import Command from "../../classes/command.js"; class ImageSearchCommand extends Command { async run() { if (this.channel.guild && !this.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!"; - const query = this.type === "classic" ? this.args.join(" ") : this.options.query; + const query = this.options.query ?? this.args.join(" "); if (!query || !query.trim()) return "You need to provide something to search for!"; await this.acknowledge(); const embeds = []; diff --git a/commands/general/lengthen.js b/commands/general/lengthen.js index a19a920..746b81e 100644 --- a/commands/general/lengthen.js +++ b/commands/general/lengthen.js @@ -5,7 +5,7 @@ import Command from "../../classes/command.js"; class LengthenCommand extends Command { async run() { await this.acknowledge(); - const input = this.type === "classic" ? this.args.join(" ") : this.options.url; + const input = this.options.url ?? this.args.join(" "); if (!input || !input.trim() || !urlCheck(input)) return "You need to provide a short URL to lengthen!"; if (urlCheck(input)) { const url = await fetch(encodeURI(input), { redirect: "manual" }); diff --git a/commands/general/reload.js b/commands/general/reload.js index 3c4afd2..85c92b8 100644 --- a/commands/general/reload.js +++ b/commands/general/reload.js @@ -6,7 +6,7 @@ class ReloadCommand extends Command { return new Promise((resolve) => { const owners = process.env.OWNER.split(","); if (!owners.includes(this.author.id)) return resolve("Only the bot owner can reload commands!"); - const commandName = this.type === "classic" ? this.args.join(" ") : this.options.cmd; + const commandName = this.options.cmd ?? this.args.join(" "); if (!commandName || !commandName.trim()) return resolve("You need to provide a command to reload!"); this.acknowledge().then(() => { this.ipc.broadcast("reload", commandName); diff --git a/commands/general/youtube.js b/commands/general/youtube.js index 79d754c..5967564 100644 --- a/commands/general/youtube.js +++ b/commands/general/youtube.js @@ -7,7 +7,7 @@ import Command from "../../classes/command.js"; class YouTubeCommand extends Command { async run() { - const query = this.type === "classic" ? this.args.join(" ") : this.options.query; + const query = this.options.query ?? this.args.join(" "); if (!query || !query.trim()) return "You need to provide something to search for!"; await this.acknowledge(); const messages = []; diff --git a/commands/image-editing/caption.js b/commands/image-editing/caption.js index 48643be..4b7fff5 100644 --- a/commands/image-editing/caption.js +++ b/commands/image-editing/caption.js @@ -3,7 +3,7 @@ const allowedFonts = ["futura", "impact", "helvetica", "arial", "roboto", "noto" class CaptionCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); let newCaption = newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"); if (process.env.NODE_ENV === "development" && newCaption.toLowerCase() === "get real" && !this.specialArgs.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 { diff --git a/commands/image-editing/caption2.js b/commands/image-editing/caption2.js index eae8039..cdfdc96 100644 --- a/commands/image-editing/caption2.js +++ b/commands/image-editing/caption2.js @@ -4,7 +4,7 @@ const allowedFonts = ["futura", "impact", "helvetica", "arial", "roboto", "noto" class CaptionTwoCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); return { caption: newArgs && newArgs.trim() ? newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("%", "%").replaceAll("\\n", "\n") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" "), top: !!this.specialArgs.top, diff --git a/commands/image-editing/flag.js b/commands/image-editing/flag.js index 1032930..830d446 100644 --- a/commands/image-editing/flag.js +++ b/commands/image-editing/flag.js @@ -7,7 +7,7 @@ class FlagCommand extends ImageCommand { flagPath = ""; async criteria() { - const text = this.type === "classic" ? this.args[0] : this.options.text; + const text = this.options.text ?? this.args[0]; if (!text.match(emojiRegex)) return false; const flag = emoji.unemojify(text).replaceAll(":", "").replace("flag-", ""); let path = `assets/images/region-flags/png/${flag.toUpperCase()}.png`; diff --git a/commands/image-editing/freeze.js b/commands/image-editing/freeze.js index 546e687..124a195 100644 --- a/commands/image-editing/freeze.js +++ b/commands/image-editing/freeze.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class FreezeCommand extends ImageCommand { params() { - const frameCount = parseInt(this.type === "classic" ? this.args[0] : this.options.endframe); + const frameCount = parseInt(this.options.endframe ?? this.args[0]); return { loop: false, frame: isNaN(frameCount) ? -1 : frameCount diff --git a/commands/image-editing/jpeg.js b/commands/image-editing/jpeg.js index 7125626..8c814b7 100644 --- a/commands/image-editing/jpeg.js +++ b/commands/image-editing/jpeg.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class JPEGCommand extends ImageCommand { params() { - const quality = parseInt(this.type === "classic" ? this.args[0] : this.options.quality); + const quality = parseInt(this.options.quality ?? this.args[0]); return { quality: isNaN(quality) ? 1 : Math.max(1, Math.min(quality, 100)) }; diff --git a/commands/image-editing/meme.js b/commands/image-editing/meme.js index bec9398..b0dfe43 100644 --- a/commands/image-editing/meme.js +++ b/commands/image-editing/meme.js @@ -3,7 +3,7 @@ const allowedFonts = ["futura", "impact", "helvetica", "arial", "roboto", "noto" class MemeCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); const [topText, bottomText] = newArgs.split(/(? elem.trim()); return { top: (this.specialArgs.case ? topText : topText.toUpperCase()).replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n"), diff --git a/commands/image-editing/motivate.js b/commands/image-editing/motivate.js index 8af4722..1aeb9b1 100644 --- a/commands/image-editing/motivate.js +++ b/commands/image-editing/motivate.js @@ -3,7 +3,7 @@ const allowedFonts = ["futura", "impact", "helvetica", "arial", "roboto", "noto" class MotivateCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); const [topText, bottomText] = newArgs.split(/(? elem.trim()); return { top: topText.replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%"), diff --git a/commands/image-editing/reddit.js b/commands/image-editing/reddit.js index dc8a996..ba95f56 100644 --- a/commands/image-editing/reddit.js +++ b/commands/image-editing/reddit.js @@ -1,10 +1,10 @@ import ImageCommand from "../../classes/imageCommand.js"; import { random } from "../../utils/misc.js"; -const names = ["esmBot", "me_irl", "dankmemes", "hmmm", "gaming", "wholesome", "chonkers", "memes", "funny", "pcmasterrace", "thomastheplankengine"]; +const names = ["esmBot", "me_irl", "dankmemes", "hmmm", "gaming", "wholesome", "chonkers", "memes", "funny", "lies"]; class RedditCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); return { caption: newArgs && newArgs.trim() ? newArgs.replaceAll("\n", "").replaceAll(" ", "") : random(names) }; diff --git a/commands/image-editing/reverse.js b/commands/image-editing/reverse.js index c3e767f..83632b5 100644 --- a/commands/image-editing/reverse.js +++ b/commands/image-editing/reverse.js @@ -1,12 +1,6 @@ import ImageCommand from "../../classes/imageCommand.js"; class ReverseCommand extends ImageCommand { - params(url, delay) { - return { - delay: delay ? (100 / delay.split("/")[0]) * delay.split("/")[1] : 0 - }; - } - static description = "Reverses an image sequence"; static aliases = ["backwards"]; diff --git a/commands/image-editing/slow.js b/commands/image-editing/slow.js index 43a7e6c..320699b 100644 --- a/commands/image-editing/slow.js +++ b/commands/image-editing/slow.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class SlowCommand extends ImageCommand { params() { - const speed = parseInt(this.type === "classic" ? this.args[0] : this.options.multiplier); + const speed = parseInt(this.options.multiplier ?? this.args[0]); return { slow: true, speed: isNaN(speed) ? 2 : speed diff --git a/commands/image-editing/snapchat.js b/commands/image-editing/snapchat.js index 79ebd1f..2dd1fd4 100644 --- a/commands/image-editing/snapchat.js +++ b/commands/image-editing/snapchat.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class SnapchatCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); const position = parseFloat(this.specialArgs.position); return { caption: newArgs.replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%"), diff --git a/commands/image-editing/soos.js b/commands/image-editing/soos.js index b01c3c1..393044c 100644 --- a/commands/image-editing/soos.js +++ b/commands/image-editing/soos.js @@ -1,9 +1,8 @@ import ImageCommand from "../../classes/imageCommand.js"; class SooSCommand extends ImageCommand { - params(url, delay) { + params() { return { - delay: delay ? (100 / delay.split("/")[0]) * delay.split("/")[1] : 0, soos: true }; } diff --git a/commands/image-editing/speed.js b/commands/image-editing/speed.js index c2b1030..dc91e94 100644 --- a/commands/image-editing/speed.js +++ b/commands/image-editing/speed.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class SpeedCommand extends ImageCommand { params() { - const speed = parseInt(this.type === "classic" ? this.args[0] : this.options.multiplier); + const speed = parseInt(this.options.multiplier ?? this.args[0]); return { speed: isNaN(speed) || speed < 1 ? 2 : speed }; diff --git a/commands/image-editing/whisper.js b/commands/image-editing/whisper.js index 01f64e8..2a52dd7 100644 --- a/commands/image-editing/whisper.js +++ b/commands/image-editing/whisper.js @@ -2,7 +2,7 @@ import ImageCommand from "../../classes/imageCommand.js"; class WhisperCommand extends ImageCommand { params(url) { - const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text; + const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" "); return { caption: newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n") }; diff --git a/commands/music/host.js b/commands/music/host.js index 4df7be3..c638ac1 100644 --- a/commands/music/host.js +++ b/commands/music/host.js @@ -7,7 +7,7 @@ class HostCommand extends MusicCommand { if (!this.member.voiceState.channelID) return "You need to be in a voice channel first!"; if (!this.channel.guild.members.get(this.client.user.id).voiceState.channelID) return "I'm not in a voice channel!"; if (this.connection.host !== this.author.id && this.author.id !== process.env.OWNER) return "Only the current voice session host can choose another host!"; - const input = this.type === "classic" ? this.args.join(" ") : this.options.user; + const input = this.options.user ?? this.args.join(" "); if (!input || !input.trim()) return "You need to provide who you want the host to be!"; let user; if (this.type === "classic") { diff --git a/commands/music/play.js b/commands/music/play.js index 1ff9c58..eb3d56e 100644 --- a/commands/music/play.js +++ b/commands/music/play.js @@ -1,9 +1,10 @@ import { play } from "../../utils/soundplayer.js"; import MusicCommand from "../../classes/musicCommand.js"; +const prefixes = ["ytsearch:", "ytmsearch:", "scsearch:", "spsearch:", "amsearch:"]; class PlayCommand extends MusicCommand { async run() { - const input = this.type === "classic" ? this.args.join(" ") : this.options.query; + const input = this.options.query ?? this.args.join(" "); if (!input && (this.type === "classic" ? (!this.message || this.message.attachments.length <= 0) : !this.options.file)) return "You need to provide what you want to play!"; let query = input ? input.trim() : ""; const attachment = this.type === "classic" ? this.message.attachments[0] : (this.options.file ? this.interaction.data.resolved.attachments[this.options.file] : null); @@ -17,7 +18,7 @@ class PlayCommand extends MusicCommand { const url = new URL(query); return await play(this.client, url, { channel: this.channel, member: this.member, type: this.type, interaction: this.interaction }, true); } catch { - const search = query.startsWith("ytsearch:") ? query : !query && attachment ? attachment.url : `ytsearch:${query}`; + const search = prefixes.some(v => query.startsWith(v)) ? query : !query && attachment ? attachment.url : `ytsearch:${query}`; return await play(this.client, search, { channel: this.channel, member: this.member, type: this.type, interaction: this.interaction }, true); } } diff --git a/commands/music/remove.js b/commands/music/remove.js index df24fb1..4b4c0c8 100644 --- a/commands/music/remove.js +++ b/commands/music/remove.js @@ -8,7 +8,7 @@ class RemoveCommand extends MusicCommand { if (!this.member.voiceState.channelID) return "You need to be in a voice channel first!"; if (!this.channel.guild.members.get(this.client.user.id).voiceState.channelID) return "I'm not in a voice channel!"; if (this.connection.host !== this.author.id) return "Only the current voice session host can remove songs from the queue!"; - const pos = parseInt(this.type === "classic" ? this.args[0] : this.options.position); + const pos = parseInt(this.options.position ?? this.args[0]); if (isNaN(pos) || pos > this.queue.length || pos < 1) return "That's not a valid position!"; const removed = this.queue.splice(pos, 1); const track = await Rest.decode(this.connection.player.node, removed[0]); diff --git a/commands/music/seek.js b/commands/music/seek.js index 0ba7ff0..02a7cb5 100644 --- a/commands/music/seek.js +++ b/commands/music/seek.js @@ -10,7 +10,7 @@ class SeekCommand extends MusicCommand { const player = this.connection.player; const track = await Rest.decode(player.node, player.track); if (!track.isSeekable) return "This track isn't seekable!"; - const seconds = parseFloat(this.type === "classic" ? this.args[0] : this.options.position); + const seconds = parseFloat(this.options.position ?? this.args[0]); if (isNaN(seconds) || (seconds * 1000) > track.length || (seconds * 1000) < 0) return "That's not a valid position!"; await player.seek(seconds * 1000); return `🔊 Seeked track to ${seconds} second(s).`; diff --git a/commands/music/skip.js b/commands/music/skip.js index 91e743e..c7dda7e 100644 --- a/commands/music/skip.js +++ b/commands/music/skip.js @@ -26,7 +26,6 @@ class SkipCommand extends MusicCommand { } else { await player.player.stop(this.channel.guild.id); if (this.type === "application") return "🔊 The current song has been skipped."; - return; } } diff --git a/natives/blur.cc b/natives/blur.cc index 23bc4e8..6856b06 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -15,8 +15,6 @@ Napi::Value Blur(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); bool sharp = obj.Get("sharp").As().Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -31,8 +29,6 @@ Napi::Value Blur(const Napi::CallbackInfo &info) { VImage out = sharp ? in.sharpen(VImage::option()->set("sigma", 3)) : in.gaussblur(15); - if (delay) out.set("delay", delay); - void *buf; size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); diff --git a/natives/caption.cc b/natives/caption.cc index bd4dada..0521c31 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -13,8 +13,6 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { string caption = obj.Get("caption").As().Utf8Value(); string font = obj.Get("font").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -59,7 +57,6 @@ Napi::Value Caption(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height + captionImage.height()); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/caption2.cc b/natives/caption2.cc index 6fb4085..4f6b83b 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -15,8 +15,6 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { bool top = obj.Get("top").As().Value(); string font = obj.Get("font").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -63,7 +61,6 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height + captionImage.height()); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/circle.cc b/natives/circle.cc index 9c4ed34..02e2c43 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -14,8 +14,6 @@ Napi::Value Circle(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; @@ -43,7 +41,6 @@ Napi::Value Circle(const Napi::CallbackInfo &info) { for (Image &image : blurred) { image.quantizeDitherMethod(FloydSteinbergDitherMethod); image.quantize(); - if (delay != 0) image.animationDelay(delay); } } diff --git a/natives/colors.cc b/natives/colors.cc index dc91a4f..3ed02c1 100644 --- a/natives/colors.cc +++ b/natives/colors.cc @@ -16,8 +16,6 @@ Napi::Value Colors(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string color = obj.Get("color").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -35,7 +33,6 @@ Napi::Value Colors(const Napi::CallbackInfo &info) { } else if (color == "sepia") { out = in.flatten().recomb(sepia); } - if (delay) out.set("delay", delay); void *buf; size_t length; diff --git a/natives/crop.cc b/natives/crop.cc index e62996d..0913969 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -12,8 +12,6 @@ Napi::Value Crop(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -45,7 +43,6 @@ Napi::Value Crop(const Napi::CallbackInfo &info) { VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, finalHeight); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/deepfry.cc b/natives/deepfry.cc index 5019e40..847279b 100644 --- a/natives/deepfry.cc +++ b/natives/deepfry.cc @@ -12,8 +12,6 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Napi::Object result = Napi::Object::New(env); @@ -34,11 +32,7 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) { VImage::option()->set("Q", 1)->set("strip", true)); VImage final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) { - final.set("delay", delay); - } else if (type == "gif") { - final.set("delay", fried.get_array_int("delay")); - } + final.set("delay", fried.get_array_int("delay")); void *buf; size_t length; diff --git a/natives/flag.cc b/natives/flag.cc index a53b754..ddb2364 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -14,8 +14,6 @@ Napi::Value Flag(const Napi::CallbackInfo &info) { string overlay = obj.Get("overlay").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -53,7 +51,6 @@ Napi::Value Flag(const Napi::CallbackInfo &info) { VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/flip.cc b/natives/flip.cc index e469033..d572b47 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -14,8 +14,6 @@ Napi::Value Flip(const Napi::CallbackInfo &info) { bool flop = obj.Has("flop") ? obj.Get("flop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -44,8 +42,6 @@ Napi::Value Flip(const Napi::CallbackInfo &info) { out = in.flip(VIPS_DIRECTION_VERTICAL); } - if (delay) out.set("delay", delay); - void *buf; size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); diff --git a/natives/freeze.cc b/natives/freeze.cc index 5d80c30..f7fe696 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -14,8 +14,6 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { bool loop = obj.Has("loop") ? obj.Get("loop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; int frame = obj.Has("frame") ? obj.Get("frame").As().Int32Value() : -1; @@ -67,8 +65,6 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) { out.set(VIPS_META_PAGE_HEIGHT, page_height); out.set("loop", loop ? 0 : 1); - if (delay) out.set("delay", delay); - void *buf; size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 5bdeb65..b1a2671 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -13,8 +13,6 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -46,7 +44,6 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, 675); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/invert.cc b/natives/invert.cc index 15eda5f..708116f 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -12,8 +12,6 @@ Napi::Value Invert(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -28,8 +26,6 @@ Napi::Value Invert(const Napi::CallbackInfo &info) { VImage inverted = noAlpha.invert(); VImage out = inverted.bandjoin(in.extract_band(3)); - if (delay) out.set("delay", delay); - void *buf; size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); diff --git a/natives/jpeg.cc b/natives/jpeg.cc index 8874e46..b3aa5a0 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -15,8 +15,6 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { ? obj.Get("quality").As().Int32Value() : 0; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Napi::Object result = Napi::Object::New(env); @@ -37,11 +35,7 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) { VImage::option()->set("Q", quality)->set("strip", true)); VImage final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) { - final.set("delay", delay); - } else if (type == "gif") { - final.set("delay", in.get_array_int("delay")); - } + final.set("delay", in.get_array_int("delay")); void *buf; size_t length; diff --git a/natives/meme.cc b/natives/meme.cc index f55f7d0..409f599 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -15,8 +15,6 @@ Napi::Value Meme(const Napi::CallbackInfo &info) { string bottom = obj.Get("bottom").As().Utf8Value(); string font = obj.Get("font").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -125,7 +123,6 @@ Napi::Value Meme(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/mirror.cc b/natives/mirror.cc index 5ff2321..0a4f896 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -17,8 +17,6 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) { bool first = obj.Has("first") ? obj.Get("first").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -67,8 +65,6 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) { } } - if (delay) out.set("delay", delay); - void *buf; size_t length; out.write_to_buffer(("." + type).c_str(), &buf, &length); diff --git a/natives/motivate.cc b/natives/motivate.cc index 7d60104..874ec72 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -15,8 +15,6 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) { string bottom_text = obj.Get("bottom").As().Utf8Value(); string font = obj.Get("font").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -103,7 +101,6 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) { VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)) .extract_band(0, VImage::option()->set("n", 3)); final.set(VIPS_META_PAGE_HEIGHT, height); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/reddit.cc b/natives/reddit.cc index 7c3d469..054c22a 100644 --- a/natives/reddit.cc +++ b/natives/reddit.cc @@ -13,8 +13,6 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) { string text = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -56,7 +54,6 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height + watermark.height()); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/resize.cc b/natives/resize.cc index a18c336..0e8f5ec 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -17,8 +17,6 @@ Napi::Value Resize(const Napi::CallbackInfo &info) { bool wide = obj.Has("wide") ? obj.Get("wide").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -47,7 +45,6 @@ Napi::Value Resize(const Napi::CallbackInfo &info) { finalHeight = page_height; } out.set(VIPS_META_PAGE_HEIGHT, finalHeight); - if (delay) out.set("delay", delay); void *buf; size_t length; diff --git a/natives/scott.cc b/natives/scott.cc index 59b5031..94ba1c6 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -15,8 +15,6 @@ Napi::Value Scott(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; @@ -46,7 +44,7 @@ Napi::Value Scott(const Napi::CallbackInfo &info) { watermark_new.composite(image, Geometry("-110+83"), Magick::OverCompositeOp); watermark_new.magick(type); - watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); + watermark_new.animationDelay(image.animationDelay()); mid.push_back(watermark_new); } diff --git a/natives/snapchat.cc b/natives/snapchat.cc index dd0c81f..62c997c 100644 --- a/natives/snapchat.cc +++ b/natives/snapchat.cc @@ -15,8 +15,6 @@ Napi::Value Snapchat(const Napi::CallbackInfo &info) { float pos = obj.Has("pos") ? obj.Get("pos").As().FloatValue() : 0.5; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -64,7 +62,6 @@ Napi::Value Snapchat(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/speed.cc b/natives/speed.cc index c59f0ce..06d5f52 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -51,8 +51,6 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { bool slow = obj.Has("slow") ? obj.Get("slow").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; int speed = obj.Has("speed") ? obj.Get("speed").As().Int32Value() : 2; @@ -62,94 +60,51 @@ Napi::Value Speed(const Napi::CallbackInfo &info) { char *match = (char *)"\x00\x21\xF9\x04"; - // if passed a delay, use that. otherwise iterate over every frame. - if (delay == 0) { - vector old_delays; - bool removeFrames = false; - char *lastPos; + vector old_delays; + bool removeFrames = false; + char *lastPos; - int amount = 0; + int amount = 0; - lastPos = (char *)memchr(fileData, '\x00', data.Length()); - while (lastPos != NULL) { - if (memcmp(lastPos, match, 4) != 0) { - lastPos = (char *)memchr(lastPos + 1, '\x00', - (data.Length() - (lastPos - fileData)) - 1); - continue; - } - ++amount; - uint16_t old_delay; - memcpy(&old_delay, lastPos + 5, 2); - old_delays.push_back(old_delay); + lastPos = (char *)memchr(fileData, '\x00', data.Length()); + while (lastPos != NULL) { + if (memcmp(lastPos, match, 4) != 0) { lastPos = (char *)memchr(lastPos + 1, '\x00', (data.Length() - (lastPos - fileData)) - 1); + continue; } + ++amount; + uint16_t old_delay; + memcpy(&old_delay, lastPos + 5, 2); + old_delays.push_back(old_delay); + lastPos = (char *)memchr(lastPos + 1, '\x00', + (data.Length() - (lastPos - fileData)) - 1); + } - int currentFrame = 0; - lastPos = (char *)memchr(fileData, '\x00', data.Length()); - while (lastPos != NULL) { - if (memcmp(lastPos, match, 4) != 0) { - lastPos = (char *)memchr(lastPos + 1, '\x00', - (data.Length() - (lastPos - fileData)) - 1); - continue; - } - uint16_t new_delay = slow ? old_delays[currentFrame] * speed - : old_delays[currentFrame] / speed; - if (!slow && new_delay <= 1) { - removeFrames = true; - break; - } - memset16(lastPos + 5, new_delay, 1); + int currentFrame = 0; + lastPos = (char *)memchr(fileData, '\x00', data.Length()); + while (lastPos != NULL) { + if (memcmp(lastPos, match, 4) != 0) { lastPos = (char *)memchr(lastPos + 1, '\x00', (data.Length() - (lastPos - fileData)) - 1); - ++currentFrame; + continue; } - - result.Set("data", - Napi::Buffer::Copy(env, fileData, data.Length())); - - if (removeFrames) vipsRemove(&env, &result, data, speed); - } else { - char *lastPos; - - bool removeFrames = false; - - lastPos = (char *)memchr(fileData, '\x00', data.Length()); - while (lastPos != NULL) { - if (memcmp(lastPos, match, 4) != 0) { - lastPos = (char *)memchr(lastPos + 1, '\x00', - (data.Length() - (lastPos - fileData)) - 1); - continue; - } - uint16_t old_delay; - memcpy(&old_delay, lastPos + 5, 2); - int new_delay = slow ? delay * speed : delay / speed; - if (!slow && new_delay <= 1) { - removeFrames = true; - } + uint16_t new_delay = slow ? old_delays[currentFrame] * speed + : old_delays[currentFrame] / speed; + if (!slow && new_delay <= 1) { + removeFrames = true; break; } - - if (removeFrames) { - vipsRemove(&env, &result, data, speed); - } else { - while (lastPos != NULL) { - if (memcmp(lastPos, match, 4) != 0) { - lastPos = - (char *)memchr(lastPos + 1, '\x00', + memset16(lastPos + 5, new_delay, 1); + lastPos = (char *)memchr(lastPos + 1, '\x00', (data.Length() - (lastPos - fileData)) - 1); - continue; - } - uint16_t old_delay; - memcpy(&old_delay, lastPos + 5, 2); - int new_delay = slow ? delay * speed : delay / speed; - memset16(lastPos + 5, new_delay, 1); - lastPos = (char *)memchr(lastPos + 1, '\x00', - (data.Length() - (lastPos - fileData)) - 1); - } - } + ++currentFrame; } + result.Set("data", Napi::Buffer::Copy(env, fileData, data.Length())); + + if (removeFrames) vipsRemove(&env, &result, data, speed); + result.Set("type", type); return result; } catch (std::exception const &err) { diff --git a/natives/tile.cc b/natives/tile.cc index 3d45a80..0932b0e 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -14,8 +14,6 @@ Napi::Value Tile(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; @@ -48,7 +46,7 @@ Napi::Value Tile(const Napi::CallbackInfo &info) { appendImages(&frame, montage.begin(), montage.end(), true); frame.repage(); frame.scale(Geometry("800x800>")); - frame.animationDelay(delay == 0 ? image.animationDelay() : delay); + frame.animationDelay(image.animationDelay()); mid.push_back(frame); } diff --git a/natives/uncaption.cc b/natives/uncaption.cc index 726103b..74e4cd9 100644 --- a/natives/uncaption.cc +++ b/natives/uncaption.cc @@ -15,8 +15,6 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) { ? obj.Get("tolerance").As().FloatValue() : 0.5; string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option(); @@ -44,7 +42,6 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height - top); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/wall.cc b/natives/wall.cc index ae693cd..2686fbb 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -14,8 +14,6 @@ Napi::Value Wall(const Napi::CallbackInfo &info) { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; @@ -51,7 +49,6 @@ Napi::Value Wall(const Napi::CallbackInfo &info) { for (Image &image : mid) { image.quantizeDitherMethod(FloydSteinbergDitherMethod); image.quantize(); - if (delay != 0) image.animationDelay(delay); } } diff --git a/natives/watermark.cc b/natives/watermark.cc index ce62109..f4f0e88 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -27,8 +27,6 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) { bool mc = obj.Has("mc") ? obj.Get("mc").As().Value() : false; string basePath = obj.Get("basePath").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -145,7 +143,6 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height + addedHeight); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/wdt.cc b/natives/wdt.cc index 699c6e7..0a09dff 100644 --- a/natives/wdt.cc +++ b/natives/wdt.cc @@ -15,8 +15,6 @@ Napi::Value Wdt(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; @@ -40,7 +38,7 @@ Napi::Value Wdt(const Napi::CallbackInfo &info) { watermark_new.composite(image, Magick::CenterGravity, Magick::OverCompositeOp); watermark_new.magick(type); - watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); + watermark_new.animationDelay(image.animationDelay()); mid.push_back(watermark_new); } diff --git a/natives/whisper.cc b/natives/whisper.cc index eea3b34..676cffb 100644 --- a/natives/whisper.cc +++ b/natives/whisper.cc @@ -13,8 +13,6 @@ Napi::Value Whisper(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -78,7 +76,6 @@ Napi::Value Whisper(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, page_height); - if (delay) final.set("delay", delay); void *buf; size_t length; diff --git a/natives/zamn.cc b/natives/zamn.cc index a6ccf4f..46aac43 100644 --- a/natives/zamn.cc +++ b/natives/zamn.cc @@ -13,8 +13,6 @@ Napi::Value Zamn(const Napi::CallbackInfo &info) { Napi::Buffer data = obj.Get("data").As>(); string type = obj.Get("type").As().Utf8Value(); string basePath = obj.Get("basePath").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; VOption *options = VImage::option()->set("access", "sequential"); @@ -44,7 +42,6 @@ Napi::Value Zamn(const Napi::CallbackInfo &info) { } VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); final.set(VIPS_META_PAGE_HEIGHT, 516); - if (delay) final.set("delay", delay); void *buf; size_t length;