Skip to content

Commit

Permalink
/art: switch to Master Duel video game illustrations
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlul committed Mar 2, 2024
1 parent 253f88f commit 283db58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export function ygoprodeckCard(term: string | number): string {
return `https://ygoprodeck.com/card/?search=${encodeURIComponent(term)}&utm_source=bastion`;
}

function masterDuelIllustrationURL(card: Static<typeof CardSchema>): string {
export function masterDuelIllustrationURL(card: Static<typeof CardSchema>): string {
// Filter card name down to alphanumeric characters
const probableBasename = (card.name.en ?? "").replaceAll(/\W/g, "");
return `https://yugipedia.com/wiki/Special:Redirect/file/${probableBasename}-MADU-EN-VG-artwork.png`;
Expand Down
26 changes: 19 additions & 7 deletions src/commands/art.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Got } from "got";
import { inject, injectable } from "tsyringe";
import { c, t, useLocale } from "ttag";
import { Command } from "../Command";
import { getCard, getCardSearchOptions, getRubylessCardName } from "../card";
import { getCard, getCardSearchOptions, getRubylessCardName, masterDuelIllustrationURL } from "../card";
import { CardSchema } from "../definitions";
import {
LocaleProvider,
Expand All @@ -17,7 +17,7 @@ import {
} from "../locale";
import { Logger, getLogger } from "../logger";
import { Metrics } from "../metrics";
import { replyLatency } from "../utils";
import { replyLatency, serialiseInteraction } from "../utils";

@injectable()
export class ArtCommand extends Command {
Expand Down Expand Up @@ -54,10 +54,22 @@ export class ArtCommand extends Command {
return this.#logger;
}

async getArt(card: Static<typeof CardSchema>): Promise<string | undefined> {
const artUrl = `${process.env.IMAGE_HOST}/${card.password}.png`;
const response = await this.got.head(artUrl);
return response.statusCode === 200 ? artUrl : undefined;
async getArt(
card: Static<typeof CardSchema>,
interaction: ChatInputCommandInteraction
): Promise<string | undefined> {
const artUrl = masterDuelIllustrationURL(card);
try {
const response = await this.got.head(artUrl, { followRedirect: false });
if (response.statusCode === 302) {
// MediaWiki Special:Redirect/file should only use 302s
return artUrl;
} else if (response.statusCode !== 404) {
this.#logger.warn(serialiseInteraction(interaction, { redirectStatusCode: response.statusCode }));
}
} catch (error) {
this.#logger.warn(serialiseInteraction(interaction), error);
}
}

protected override async execute(interaction: ChatInputCommandInteraction): Promise<number> {
Expand All @@ -72,7 +84,7 @@ export class ArtCommand extends Command {
return replyLatency(reply, interaction);
} else {
await interaction.deferReply();
const artUrl = await this.getArt(card);
const artUrl = await this.getArt(card, interaction);
const end = Date.now();
if (artUrl) {
// expected embedding of image from URL
Expand Down

0 comments on commit 283db58

Please sign in to comment.