Skip to content

Commit

Permalink
/yugipedia: hyperlink search page on unhappy path, add explanation fo…
Browse files Browse the repository at this point in the history
…r common timeouts
  • Loading branch information
kevinlul committed Feb 3, 2025
1 parent 4a147e3 commit d5ba4d5
Show file tree
Hide file tree
Showing 10 changed files with 737 additions and 732 deletions.
11 changes: 8 additions & 3 deletions src/commands/yugipedia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommandStringOption } from "@discordjs/builders";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types/v10";
import { AutocompleteInteraction, ChatInputCommandInteraction } from "discord.js";
import { Got } from "got";
import { Got, TimeoutError } from "got";
import { LRUMap } from "mnemonist";
import { inject, injectable } from "tsyringe";
import { c, t, useLocale } from "ttag";
Expand Down Expand Up @@ -100,18 +100,23 @@ export class YugiCommand extends AutocompletableCommand {
content = cached;
this.#logger.info(serialiseInteraction(interaction, { page, cached }));
} else {
const searchURL = `<https://yugipedia.com/index.php?search=${encodeURIComponent(page)}>`;
try {
const start = Date.now();
const response = await this.search(page);
const latency = Date.now() - start;
this.#logger.info(serialiseInteraction(interaction, { page, latency, response }));
useLocale(lang);
const link = response[3][0];
content = link || t`Could not find a Yugipedia page named \`${page}\`.`;
content = link || t`Could not find a [Yugipedia page named \`${page}\`](${searchURL}).`;
} catch (error) {
this.#logger.warn(serialiseInteraction(interaction, { page }), error);
useLocale(lang);
content = t`Something went wrong searching Yugipedia for \`${page}\`.`;
if (error instanceof TimeoutError) {
content = t`Took too long [searching Yugipedia for \`${page}\`](${searchURL}). Is the site up?`;
} else {
content = t`Something went wrong [searching Yugipedia for \`${page}\`](${searchURL}). Is the site up?`;
}
}
}
const reply = await interaction.reply({ content, fetchReply: true });
Expand Down
Loading

0 comments on commit d5ba4d5

Please sign in to comment.