Skip to content

Commit

Permalink
feat(application commands): animal slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
jwford committed Jul 10, 2021
1 parent b2d0113 commit b4f7595
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class extends Event {

public run(interaction: APIApplicationCommandInteraction) {
switch (interaction.data.name) {
case 'animal':
this.client.emit(ApplicationCommands.Animal, interaction);
break;
case 'assign':
this.client.emit(ApplicationCommands.Assign, interaction);
break;
Expand Down
139 changes: 139 additions & 0 deletions src/events/interactions/applicationCommands/animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { ApplicationCommand } from '@lib/structures/events/ApplicationCommand';
import { ImageAssets } from '@lib/types/Enums';
import axios from 'axios';
import { APIApplicationCommandInteraction, APIApplicationCommandInteractionDataOptionWithValues, APIInteractionApplicationCommandCallbackData } from 'discord-api-types/payloads/v8';
import { MessageEmbed } from 'discord.js';

export default class extends ApplicationCommand {

public async handle(interaction: APIApplicationCommandInteraction): Promise<APIInteractionApplicationCommandCallbackData> {
const animal = (interaction.data.options![0] as APIApplicationCommandInteractionDataOptionWithValues).value as Animal;

switch (animal) {
case 'cat':
return this.cat();
case 'dog':
return this.dog();
case 'fox':
return this.fox();
case 'koala':
return this.koala();
case 'panda':
return this.panda();
case 'red_panda':
return this.redpanda();
}
}

private async cat(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<CatResponse>('https://cataas.com/cat?json=true');

embed.setImage(data.url
? `https://cataas.com${data.url}`
: ImageAssets.Cat);
} catch {
embed.setImage(ImageAssets.Cat);
}

return { embeds: [embed.toJSON()] };
}

private async dog(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<DogResponse>('https://dog.ceo/api/breeds/image/random');

embed.setImage(data.message);
} catch {
embed.setImage(ImageAssets.Dog);
}

return { embeds: [embed.toJSON()] };
}

private async fox(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<FoxResponse>('https://randomfox.ca/floof');
embed.setImage(data.image ?? ImageAssets.Fox);
} catch {
embed.setImage(ImageAssets.Fox);
}

return { embeds: [embed.toJSON()] };
}

private async koala(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<SomeRandomApiResponse>('https://some-random-api.ml/img/koala');
embed.setImage(data.link);
} catch {
embed.setDescription('Whoops! Couldn\'t find an image. Soon:tm: I\'ll have a fallback image like I do for dogs/cats/foxes, but you can bug my developers about that.');
}

return { embeds: [embed.toJSON()] };
}

private async panda(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<SomeRandomApiResponse>('https://some-random-api.ml/img/panda');

data.link
? embed.setImage(data.link)
: embed.setDescription('Whoops! Couldn\'t find an image. Soon:tm: I\'ll have a fallback image like I do for dogs/cats/foxes, but you can bug my developers about that.');
} catch {
embed.setDescription('Whoops! Couldn\'t find an image. Soon:tm: I\'ll have a fallback image like I do for dogs/cats/foxes, but you can bug my developers about that.');
}

return { embeds: [embed.toJSON()] };
}

private async redpanda(): Promise<APIInteractionApplicationCommandCallbackData> {
const embed = new MessageEmbed();

try {
const { data } = await axios.get<SomeRandomApiResponse>('https://some-random-api.ml/img/red_panda');

data.link
? embed.setImage(data.link)
: embed.setDescription('Whoops! Couldn\'t find an image. Soon:tm: I\'ll have a fallback image like I do for dogs/cats/foxes, but you can bug my developers about that.');
} catch {
embed.setDescription('Whoops! Couldn\'t find an image. Soon:tm: I\'ll have a fallback image like I do for dogs/cats/foxes, but you can bug my developers about that.');
}

return { embeds: [embed.toJSON()] };
}

}

interface CatResponse {
id: string;
created_at: string;
tags: string[];
url: string;
}

interface DogResponse {
message: string;
status: string;
}

interface FoxResponse {
image: string;
link: string;
}

interface SomeRandomApiResponse {
link: string;
}

type Animal = 'dog' | 'cat' | 'fox' | 'koala' | 'panda' | 'red_panda';
1 change: 1 addition & 0 deletions src/lib/types/Enums.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const enum ApplicationCommands {
Animal = 'animal',
Assign = 'assign',
Avatar = 'avatar',
Convert = 'convert',
Expand Down

0 comments on commit b4f7595

Please sign in to comment.