Skip to content

Commit

Permalink
merge "too big" warnings and fix untranslatable action strings
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 25, 2023
1 parent 4fa252e commit 89e7bf7
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 109 deletions.
5 changes: 5 additions & 0 deletions lang/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
},
"images": {
"too_big": "This image is too big to %ACTION%!",
"actions": {
"tile": "be tiled",
"palette": "take the palette of",
"compare": "compare"
},
"too_long": "This image is too long to animate!",
"max_size": "The maximum size for an image is 1024px².",
"max_length": "The maximum length for a tilesheet is 32 frames.",
Expand Down
5 changes: 3 additions & 2 deletions src/commands/images/compare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SlashCommand } from "@interfaces";
import { SlashCommandBuilder } from "discord.js";
import { ChatInputCommandInteraction, Message } from "@client";
import textureComparison, { comparisonTooBig } from "@functions/textureComparison";
import textureComparison from "@functions/textureComparison";
import parseTextureName from "@functions/parseTextureName";
import { textureChoiceEmbed } from "@helpers/choiceEmbed";
import { imageTooBig } from "@helpers/warnUser";

export const command: SlashCommand = {
data: new SlashCommandBuilder()
Expand Down Expand Up @@ -44,7 +45,7 @@ export const command: SlashCommand = {
const replyOptions = await textureComparison(interaction.client, results[0].id, display);
if (!replyOptions) {
await interaction.deleteReply();
return comparisonTooBig(interaction);
return await imageTooBig(interaction, "compare");
}

return interaction.editReply(replyOptions).then((message: Message) => message.deleteButton());
Expand Down
5 changes: 3 additions & 2 deletions src/commands/images/palette.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { SlashCommand } from "@interfaces";
import { SlashCommandBuilder } from "discord.js";
import { ChatInputCommandInteraction, Message } from "@client";
import { paletteToAttachment, paletteTooBig } from "@images/palette";
import { paletteToAttachment } from "@images/palette";
import getImage, { imageNotFound } from "@helpers/getImage";
import { imageTooBig } from "@helpers/warnUser";

export const command: SlashCommand = {
data: new SlashCommandBuilder()
Expand All @@ -17,7 +18,7 @@ export const command: SlashCommand = {

const [file, embed] = await paletteToAttachment(image);

if (!file || !embed) return await paletteTooBig(interaction);
if (!file || !embed) return await imageTooBig(interaction, "palette");

await interaction
.reply({
Expand Down
8 changes: 4 additions & 4 deletions src/commands/images/tile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SlashCommand } from "@interfaces";
import { SlashCommandBuilder } from "discord.js";
import { ChatInputCommandInteraction, EmbedBuilder, Message } from "@client";
import { tileToAttachment, TileShape, TileRandom, tileTooBig } from "@images/tile";
import { ChatInputCommandInteraction, Message } from "@client";
import { tileToAttachment, TileShape, TileRandom } from "@images/tile";
import getImage, { imageNotFound } from "@helpers/getImage";
import { imageButtons } from "@utility/buttons";
import { colors } from "@utility/colors";
import { imageTooBig } from "@helpers/warnUser";

export const command: SlashCommand = {
data: new SlashCommandBuilder()
Expand Down Expand Up @@ -46,7 +46,7 @@ export const command: SlashCommand = {
const file = await tileToAttachment(image, { random, shape });

if (!file) {
return await tileTooBig(interaction);
return await imageTooBig(interaction, "tile");
}
await interaction
.reply({
Expand Down
5 changes: 3 additions & 2 deletions src/components/buttons/image/compare.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { Client, Message, ButtonInteraction, EmbedBuilder } from "@client";
import textureComparison, { comparisonTooBig } from "@functions/textureComparison";
import textureComparison from "@functions/textureComparison";
import { InteractionEditReplyOptions } from "discord.js";
import { imageTooBig } from "@helpers/warnUser";

export default {
id: "compare",
Expand All @@ -17,7 +18,7 @@ export default {
if (!messageOptions) {
// stupid workaround for already having deferred the message
await interaction.deleteReply();
return comparisonTooBig(interaction);
return await imageTooBig(interaction, "compare");
}

const embed = messageOptions.embeds[0] as EmbedBuilder;
Expand Down
5 changes: 3 additions & 2 deletions src/components/buttons/image/palette.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { Client, Message, ButtonInteraction } from "@client";
import { paletteToAttachment, paletteTooBig } from "@images/palette";
import { paletteToAttachment } from "@images/palette";
import getImage, { imageNotFound } from "@helpers/getImage";
import { imageTooBig } from "@helpers/warnUser";

export default {
id: "palette",
Expand All @@ -14,7 +15,7 @@ export default {
if (!url) return imageNotFound(interaction);
const [file, embed] = await paletteToAttachment(url);

if (!file || !embed) return await paletteTooBig(interaction);
if (!file || !embed) return await imageTooBig(interaction, "palette");

return interaction
.reply({
Expand Down
5 changes: 3 additions & 2 deletions src/components/buttons/image/tile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { Client, Message, ButtonInteraction, EmbedBuilder } from "@client";
import { tileToAttachment, tileTooBig } from "@images/tile";
import { tileToAttachment } from "@images/tile";
import { imageButtons } from "@utility/buttons";
import getImage, { imageNotFound } from "@helpers/getImage";
import { imageTooBig } from "@helpers/warnUser";

export default {
id: "tile",
Expand All @@ -15,7 +16,7 @@ export default {
if (!url) return imageNotFound(interaction);
const attachment = await tileToAttachment(url);

if (!attachment) return await tileTooBig(interaction);
if (!attachment) return await imageTooBig(interaction, "tile");

return interaction
.reply({
Expand Down
5 changes: 3 additions & 2 deletions src/components/menus/compareSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Client, Message, StringSelectMenuInteraction } from "@client";
import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { MessageEditOptions, MessageInteraction } from "discord.js";
import textureComparison, { comparisonTooBig } from "@functions/textureComparison";
import textureComparison from "@functions/textureComparison";
import { imageTooBig } from "@helpers/warnUser";

export default {
id: "compareSelect",
Expand Down Expand Up @@ -32,7 +33,7 @@ export default {
if (!editOptions) {
// stupid workaround for already having deferred the message
await interaction.deleteReply();
return comparisonTooBig(interaction);
return await imageTooBig(interaction, "compare");
}

message.edit(editOptions).then((message: Message) => message.deleteButton());
Expand Down
32 changes: 1 addition & 31 deletions src/helpers/functions/textureComparison.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import stitch from "@images/stitch";
import { magnifyToAttachment } from "@images/magnify";
import { Image, loadImage } from "@napi-rs/canvas";
import {
Client,
EmbedBuilder,
ButtonInteraction,
ChatInputCommandInteraction,
StringSelectMenuInteraction,
Message,
} from "@client";
import { Client, EmbedBuilder } from "@client";
import { addPathsToEmbed } from "@functions/getTexture";
import { GalleryTexture } from "@interfaces";
import axios from "axios";
import { ActionRowBuilder, ButtonBuilder } from "discord.js";
import { template } from "@utility/buttons";
import warnUser from "@helpers/warnUser";

/**
* Get the corresponding pack IDs for a given display choice
Expand Down Expand Up @@ -96,25 +88,3 @@ export default async function textureComparison(
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(template)],
};
}

/**
* Warn the user that the image is too large
* @author Evorp
* @param interaction interaction to reply to
*/
export async function comparisonTooBig(
interaction:
| ButtonInteraction
| ChatInputCommandInteraction
| StringSelectMenuInteraction
| Message,
) {
// force english if it's a message
return warnUser(
interaction,
interaction
.strings(interaction instanceof Message)
.command.images.too_big.replace("%ACTION%", "compare"),
interaction.strings(interaction instanceof Message).command.images.max_size,
);
}
8 changes: 4 additions & 4 deletions src/helpers/getImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StringSelectMenuInteraction,
} from "@client";
import { Interaction, MessageType } from "discord.js";
import warnUser from "./warnUser";
import { warnUser } from "./warnUser";

// remove stupid discord metadata (idk why they even added it)
export const removeMetadata = (url: string) => url.split("?")[0];
Expand Down Expand Up @@ -110,7 +110,7 @@ export default async function getImage(msgOrInteraction: Message | Interaction)
}

/**
* Warn the user that the image is too large
* Warn the user that the image could not be found
* @author Evorp
* @param interaction interaction to reply to
*/
Expand All @@ -123,7 +123,7 @@ export async function imageNotFound(
) {
return warnUser(
interaction,
interaction.strings().command.images.not_found,
interaction.strings().command.images.suggestion,
interaction.strings(interaction instanceof Message).command.images.not_found,
interaction.strings(interaction instanceof Message).command.images.suggestion,
);
}
31 changes: 1 addition & 30 deletions src/helpers/images/palette.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import {
EmbedBuilder,
ButtonInteraction,
ChatInputCommandInteraction,
StringSelectMenuInteraction,
Message,
} from "@client";
import { EmbedBuilder } from "@client";
import { createCanvas, loadImage } from "@napi-rs/canvas";
import { AttachmentBuilder } from "discord.js";
import ColorManager from "@images/colors";
import { ImageSource } from "@helpers/getImage";
import warnUser from "@helpers/warnUser";

const COOLORS_URL = "https://coolors.co/";

Expand Down Expand Up @@ -192,25 +185,3 @@ export async function paletteToAttachment(
if (!image || !embed) return [null, null];
return [new AttachmentBuilder(image, { name }), embed];
}

/**
* Warn the user that the image is too large
* @author Evorp
* @param interaction interaction to reply to
*/
export async function paletteTooBig(
interaction:
| ButtonInteraction
| ChatInputCommandInteraction
| StringSelectMenuInteraction
| Message,
) {
// force english if it's a message
return warnUser(
interaction,
interaction
.strings(interaction instanceof Message)
.command.images.too_big.replace("%ACTION%", "take the palette of"),
interaction.strings(interaction instanceof Message).command.images.max_size,
);
}
23 changes: 0 additions & 23 deletions src/helpers/images/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
StringSelectMenuInteraction,
Message,
} from "@client";
import warnUser from "@helpers/warnUser";

export type TileShape = "grid" | "vertical" | "horizontal" | "hollow" | "plus";
export type TileRandom = "flip" | "rotation";
Expand Down Expand Up @@ -141,25 +140,3 @@ export async function tileToAttachment(
if (!buf) return null;
return new AttachmentBuilder(buf, { name });
}

/**
* Warn the user that the image is too large
* @author Evorp
* @param interaction interaction to reply to
*/
export async function tileTooBig(
interaction:
| ButtonInteraction
| ChatInputCommandInteraction
| StringSelectMenuInteraction
| Message,
) {
// force english if it's a message
return warnUser(
interaction,
interaction
.strings(interaction instanceof Message)
.command.images.too_big.replace("%ACTION%", "be tiled"),
interaction.strings(interaction instanceof Message).command.images.max_size,
);
}
9 changes: 5 additions & 4 deletions src/helpers/prefixCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Message } from "@client";
import { magnifyToAttachment } from "./images/magnify";
import getImage, { imageNotFound } from "./getImage";
import { tileToAttachment, tileTooBig } from "./images/tile";
import { paletteToAttachment, paletteTooBig } from "./images/palette";
import { tileToAttachment } from "./images/tile";
import { paletteToAttachment } from "./images/palette";
import { imageButtons, magnifyButtons } from "@utility/buttons";
import { imageTooBig } from "./warnUser";

export default async function prefixCommandHandler(message: Message) {
const args = message.content.split(" ");
Expand All @@ -30,13 +31,13 @@ export default async function prefixCommandHandler(message: Message) {
.then((message: Message) => message.deleteButton());
case "t":
const file = await tileToAttachment(url);
if (!file) return tileTooBig(message);
if (!file) return await imageTooBig(message, "tile");
return await message
.reply({ files: [file], components: [imageButtons] })
.then((message: Message) => message.deleteButton());
case "p":
const [attachment, embed] = await paletteToAttachment(url);
if (!attachment || !embed) return paletteTooBig(message);
if (!attachment || !embed) return await imageTooBig(message, "palette");
return await message
.reply({ files: [attachment], embeds: [embed] })
.then((message: Message) => message.deleteButton());
Expand Down
26 changes: 25 additions & 1 deletion src/helpers/warnUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
EmbedBuilder,
} from "@client";
import { colors } from "@utility/colors";
import { AllStrings } from "./strings";

export default async function warnUser(
export async function warnUser(
interaction:
| ButtonInteraction
| ChatInputCommandInteraction
Expand Down Expand Up @@ -38,5 +39,28 @@ export default async function warnUser(
reply = await (interaction as ChatInputCommandInteraction).followUp(args);
}
}

if (interaction instanceof Message) reply.deleteButton();
}

/**
* Warn a user that a provided image is too large to perform an action
* @author Evorp
* @param interaction interaction to reply to
*/
export async function imageTooBig(
interaction:
| ButtonInteraction
| ChatInputCommandInteraction
| StringSelectMenuInteraction
| Message,
action: keyof AllStrings["command"]["images"]["actions"],
) {
// force english if it's a message
const imageStrings = interaction.strings(interaction instanceof Message).command.images;
return warnUser(
interaction,
imageStrings.too_big.replace("%ACTION%", imageStrings.actions[action]),
imageStrings.max_size,
);
}

0 comments on commit 89e7bf7

Please sign in to comment.