From a45a7e35808cee6f3fc19cde003cc89d7587391e Mon Sep 17 00:00:00 2001 From: Chew Date: Thu, 22 Dec 2022 22:53:12 -0600 Subject: [PATCH] feat: rory command!!! --- src/_reupload.ts | 3 +++ src/commands/rory.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 +++ 3 files changed, 57 insertions(+) create mode 100644 src/commands/rory.ts diff --git a/src/_reupload.ts b/src/_reupload.ts index 3364847..bb8cf8a 100644 --- a/src/_reupload.ts +++ b/src/_reupload.ts @@ -49,6 +49,9 @@ export const reuploadCommands = async () => { .setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers) .setDMPermission(false), new SlashCommandBuilder().setName('joke').setDescription("it's a joke"), + new SlashCommandBuilder().setName('rory').setDescription("Gets a Rory photo!") + .addStringOption((option) => + option.setName("id").setDescription("specify a Rory ID").setRequired(false)), ].map((command) => command.toJSON()); const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN!); diff --git a/src/commands/rory.ts b/src/commands/rory.ts new file mode 100644 index 0000000..0a1026e --- /dev/null +++ b/src/commands/rory.ts @@ -0,0 +1,51 @@ +import type { CacheType, ChatInputCommandInteraction } from 'discord.js'; +import { EmbedBuilder } from 'discord.js'; + +export interface RoryResponse { + /** + * The ID of this Rory + */ + id: number; + /** + * The URL to the image of this Rory + */ + url: string; + /** + * When error :( + */ + error: string | undefined; +} + +export const roryCommand = async ( + i: ChatInputCommandInteraction +) => { + await i.deferReply(); + + const { value: id } = i.options.get('id') ?? { value: '' }; + + const rory: RoryResponse = await fetch(`https://rory.cat/purr/${id}`, { + headers: { Accept: 'application/json' }, + }).then((r) => r.json()); + + if (rory.error) { + await i.editReply({ + embeds: [ + new EmbedBuilder().setTitle('Error!').setDescription(rory.error), + ], + }); + + return; + } + + await i.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle('Rory :3') + .setURL(`https://rory.cat/id/${rory.id}`) + .setImage(rory.url) + .setFooter({ + text: `ID ${rory.id}`, + }), + ], + }); +}; diff --git a/src/index.ts b/src/index.ts index b9f0808..75c6797 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { starsCommand } from './commands/stars'; import { modrinthCommand } from './commands/modrinth'; import { tagsCommand } from './commands/tags'; import { jokeCommand } from './commands/joke'; +import { roryCommand } from "./commands/rory"; import random from 'just-random'; import { green, bold, yellow, cyan } from 'kleur/colors'; @@ -120,6 +121,8 @@ client.on('interactionCreate', async (interaction) => { await tagsCommand(interaction); } else if (commandName === 'joke') { await jokeCommand(interaction); + } else if (commandName === "rory") { + await roryCommand(interaction); } } });