Skip to content

Commit

Permalink
improve getTexture code + new translations
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 25, 2023
1 parent 36b12b7 commit 6e04c2c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
4 changes: 4 additions & 0 deletions lang/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"no_results": {
"title": "No results found!",
"description": "No results were found for %TEXTURENAME%. Have you made a typo?"
},
"no_image": {
"title": "Image not found!",
"description": "`%TEXTURENAME%` hasn't been made for %PACK% yet or is blacklisted!"
}
},
"info": {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/faithful/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export const command: SlashCommand = {

// only one result
if (results.length === 1) {
const replyOptions = await getTexture({
texture: results[0],
pack: interaction.options.getString("pack", true),
guild: interaction.guild,
});
const replyOptions = await getTexture(
interaction,
results[0],
interaction.options.getString("pack", true),
);

// no results found
if (!replyOptions.files) return interaction.ephemeralReply(replyOptions);
Expand Down
10 changes: 5 additions & 5 deletions src/components/menus/textureSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export default {
interaction.deferUpdate();

const [id, pack] = interaction.values[0].split("__");
const editOptions = await getTexture({
texture: (await axios.get(`${interaction.client.tokens.apiUrl}textures/${id}/all`)).data,
pack: pack,
guild: interaction.guild,
});
const editOptions = await getTexture(
interaction,
(await axios.get(`${interaction.client.tokens.apiUrl}textures/${id}/all`)).data,
pack,
);

if (!editOptions.files) return await interaction.ephemeralReply(editOptions);

Expand Down
37 changes: 20 additions & 17 deletions src/helpers/functions/getTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EmbedBuilder } from "@client";
import TokenJson from "@json/tokens.json";
import { Contributor, GalleryTexture, Tokens } from "@interfaces";
import axios from "axios";
import { APIEmbedField, AttachmentBuilder, Guild } from "discord.js";
import { APIEmbedField, AttachmentBuilder, Interaction } from "discord.js";
import { magnify, magnifyToAttachment } from "@images/magnify";
import { colors } from "@utility/colors";
import { Texture, Contribution } from "@interfaces";
Expand All @@ -15,23 +15,21 @@ import { Image, loadImage } from "@napi-rs/canvas";
/**
* Create a full texture embed with provided information
* @author Juknum, Evorp, RobertR11
* @param options what textures to load
* @returns reply options
*/
export const getTexture = async (options: {
texture: Texture;
pack: string;
guild: Guild;
}): Promise<any> => {
export async function getTexture(
interaction: Interaction,
texture: Texture,
pack: string,
): Promise<any> {
const tokens: Tokens = TokenJson;
const { texture, pack, guild } = options;
const { paths, contributions: allContributions } = texture;
const animated = paths.filter((p) => p.mcmeta === true).length !== 0;
const isAnimated = paths.filter((p) => p.mcmeta === true).length !== 0;
const contributionJSON: Contributor[] = (await axios.get(`${tokens.apiUrl}contributions/authors`))
.data;

let mcmeta: any = {};
if (animated) {
if (isAnimated) {
const animatedPath = paths.filter((p) => p.mcmeta === true)[0];
const raw = (await axios.get(`${tokens.apiUrl}settings/repositories.raw`)).data;

Expand All @@ -52,7 +50,7 @@ export const getTexture = async (options: {

const files: AttachmentBuilder[] = [];
const embed = new EmbedBuilder().setTitle(`[#${texture.id}] ${texture.name}`).setFooter({
text: `${strPack}`,
text: strPack,
iconURL: strIconURL,
});

Expand All @@ -70,8 +68,13 @@ export const getTexture = async (options: {
image = await loadImage(textureURL);
} catch (err) {
const errorEmbed = new EmbedBuilder()
.setTitle("Image not found!")
.setDescription(`\`${texture.name}\` hasn't been made for ${strPack} yet or is blacklisted!`)
.setTitle(interaction.strings().command.texture.no_image.title)
.setDescription(
interaction
.strings()
.command.texture.no_image.description.replace("%TEXTURENAME%", texture.name)
.replace("%PACK%", strPack),
)
.setColor(colors.red);
// missing texture so we break early
return { embeds: [errorEmbed], components: [] };
Expand All @@ -81,7 +84,7 @@ export const getTexture = async (options: {
.setURL(`https://webapp.faithfulpack.net/#/gallery/java/32x/latest/all/?show=${texture.id}`)
.addFields({ name: "Resolution", value: `${image.width}×${image.height}` })
.setThumbnail(textureURL)
.setImage(`attachment://${animated ? "animated.gif" : "magnified.png"}`);
.setImage(`attachment://${isAnimated ? "animated.gif" : "magnified.png"}`);

let mainContribution: Contribution;
if (allContributions.length) {
Expand All @@ -92,7 +95,7 @@ export const getTexture = async (options: {

if (mainContribution) {
const authors = mainContribution.authors.map((authorId) => {
if (guild.members.cache.get(authorId)) return `<@!${authorId}>`;
if (interaction.guild.members.cache.get(authorId)) return `<@!${authorId}>`;

// fetch username if not in server
return contributionJSON.find((user) => user.id == authorId)?.username ?? "Anonymous";
Expand All @@ -113,7 +116,7 @@ export const getTexture = async (options: {
embed.addFields(addPathsToEmbed(texture));

// magnifying the texture in thumbnail
if (animated) {
if (isAnimated) {
if (Object.keys(mcmeta?.animation ?? {}).length)
embed.addFields({
name: "MCMETA",
Expand All @@ -125,7 +128,7 @@ export const getTexture = async (options: {
} else files.push(await magnifyToAttachment(textureURL));

return { embeds: [embed], files: files, components: [textureButtons], ephemeral: false };
};
}

/**
* Generate embed fields for a given texture's paths
Expand Down

0 comments on commit 6e04c2c

Please sign in to comment.