Skip to content

Commit

Permalink
feat(Muse/Fun): Turn doggo/foxy & kitty commands in animal commands +…
Browse files Browse the repository at this point in the history
… additions
  • Loading branch information
jurienhamaker committed Oct 25, 2023
1 parent 44436a4 commit 94392c2
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 45 deletions.
96 changes: 96 additions & 0 deletions apps/muse/src/modules/discord/fun/commands/animal.commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Injectable, Logger } from '@nestjs/common';
import { MESSAGE_PREFIX } from '@util';
import { ChatInputCommandInteraction } from 'discord.js';
import { Context, SlashCommand, SlashCommandContext } from 'necord';
import { FunAnimalService } from '../services/animal.service';
import { AnimelType } from '../types/animal-type';
// class FunKittyRandomOptions {
// @StringOption({
// name: 'type',
// description: "The type of image you'd like",
// required: false,
// choices: KITTY_TYPE_OPTIONS,
// })
// type: string;
// }

@Injectable()
export class FunAnimalCommands {
private readonly _logger = new Logger(FunAnimalCommands.name);

constructor(private _animal: FunAnimalService) {}

@SlashCommand({
name: 'kitty',
description: 'Send a random kitty',
})
public async kitty(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'cat', 'kitty');
}

@SlashCommand({
name: 'doggo',
description: 'Send a random doggo',
})
public async doggo(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'dog', 'doggo');
}

@SlashCommand({
name: 'foxy',
description: 'Send a random foxy',
})
public async foxy(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'fox', 'foxy');
}

@SlashCommand({
name: 'fishy',
description: 'Send a random fishy',
})
public async fishy(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'fish', 'fishy');
}

@SlashCommand({
name: 'alpaca',
description: 'Send a random alpha',
})
public async alpaca(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'alpaca', 'alpaca');
}

@SlashCommand({
name: 'birb',
description: 'Send a random birb',
})
public async birb(@Context() [interaction]: SlashCommandContext) {
return this._runType(interaction, 'bird', 'birb');
}

private async _runType(
interaction: ChatInputCommandInteraction,
type: AnimelType,
friendlyName: string,
) {
this._logger.debug(`Sending a random ${type} image`);
await interaction.deferReply();
const image = await this._animal.getRandom(type);

if (!image) {
return interaction.editReply({
content: `${MESSAGE_PREFIX} Something went wrong while fetching the ${friendlyName} image. Try again later.`,
});
}

return interaction.editReply({
content: '',
files: [
{
attachment: image,
name: `${type}.png'}`,
},
],
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { MESSAGE_PREFIX } from '@util';
import { Context, SlashCommand, SlashCommandContext } from 'necord';
import { FunKittyService } from '../services/kitty.service';
import { KITTY_BASE_URL } from '../util/constants';
import { RANDOM_ANIMAL_BASE_URL } from '../util/constants';
// class FunKittyRandomOptions {
// @StringOption({
// name: 'type',
Expand Down Expand Up @@ -40,7 +40,7 @@ export class FunKittyCommands {
content: '',
files: [
{
attachment: `${KITTY_BASE_URL}${data.url}`,
attachment: `${RANDOM_ANIMAL_BASE_URL}${data.url}`,
name: `kitty.${type === 'gif' ? 'gif' : 'png'}`,
},
],
Expand Down
18 changes: 4 additions & 14 deletions apps/muse/src/modules/discord/fun/fun.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Module } from '@nestjs/common';
import { FunDoggoCommands } from './commands/doggo.commands';
import { FunFoxyCommands } from './commands/foxy.commands';
import { FunKittyCommands } from './commands/kitty.commands';
import { FunAnimalCommands } from './commands/animal.commands';
import { FunSayCommands } from './commands/say.commands';
import { FunMessageEvents } from './events/message.events';
import { FunDoggoService } from './services/doggo.service';
import { FunFoxyService } from './services/foxy.service';
import { FunKittyService } from './services/kitty.service';
import { FunAnimalService } from './services';

@Module({
imports: [],
Expand All @@ -16,14 +12,8 @@ import { FunKittyService } from './services/kitty.service';

FunSayCommands,

FunKittyService,
FunKittyCommands,

FunDoggoService,
FunDoggoCommands,

FunFoxyService,
FunFoxyCommands,
FunAnimalService,
FunAnimalCommands,
],
exports: [],
})
Expand Down
32 changes: 32 additions & 0 deletions apps/muse/src/modules/discord/fun/services/animal.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable, Logger } from '@nestjs/common';
import { AnimelType } from '../types/animal-type';
import { RANDOM_ANIMAL_BASE_URL } from '../util/constants';

export type RANDOM_KITTY_TYPES = 'normal' | 'gif' | 'cute' | 'random';

@Injectable()
export class FunAnimalService {
private readonly _logger = new Logger(FunAnimalService.name);

async getRandom(type: AnimelType) {
const data = await fetch(`${RANDOM_ANIMAL_BASE_URL}/${type}`)
.then((d) => {
if (d.status !== 200) {
return new Promise((resolve) =>
setTimeout(async () => {
const data = await this.getRandom(type);
resolve(data);
}, 300),
);
}

return d?.json();
})
.catch((err) => {
this._logger.error(err);
return null;
});

return data.message;
}
}
2 changes: 1 addition & 1 deletion apps/muse/src/modules/discord/fun/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './kitty.service';
export * from './animal.service';
1 change: 1 addition & 0 deletions apps/muse/src/modules/discord/fun/types/animal-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type AnimelType = 'cat' | 'dog' | 'fox' | 'fish' | 'alpaca' | 'bird';
30 changes: 4 additions & 26 deletions apps/muse/src/modules/discord/fun/util/constants.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
export const KITTY_BASE_URL = `https://cataas.com`;
export const DOGGO_BASE_URL = `https://random.dog`;
export const FOXY_BASE_URL = `https://randomfox.ca`;
// export const KITTY_BASE_URL = `https://cataas.com`;
// export const DOGGO_BASE_URL = `https://random.dog`;
// export const FOXY_BASE_URL = `https://randomfox.ca`;

export const KITTY_TYPE_OPTIONS = [
{
name: 'Random',
description:
'Get a random type of kitty image. This includes normal, gif and cute.',
value: 'random',
},
{
name: 'Normal',
description: 'Get a normal kitty image.',
value: 'normal',
},
{
name: 'GIF',
description: 'Get a GIF kitty image.',
value: 'gif',
},
{
name: 'Cute',
description: 'Get a cute kitty image.',
value: 'cute',
},
];
export const RANDOM_ANIMAL_BASE_URL = `https://api.sefinek.net/api/v2/random/animal`;

export const TABLE_FLIP_RESPONSES = [
`┬─┬ノ( ◕◡◕ ノ)`,
Expand Down
2 changes: 1 addition & 1 deletion apps/muse/src/muse.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { SharedModule } from './shared.module';
process.env.NODE_ENV !== 'production'
? process.env.DEVELOPMENT_SERVER_IDS!.split(',')
: false,
skipRegistration: process.env.REGISTER_COMMANDS !== 'false',
skipRegistration: process.env.REGISTER_COMMANDS === 'false',
token: process.env.DISCORD_TOKEN!,
intents,
}),
Expand Down
7 changes: 6 additions & 1 deletion apps/muse/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"target": "es2021"
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts", "../../libs/music/src/lib/music.decorator.ts"]
"include": [
"src/**/*.ts",
"../../libs/music/src/lib/music.decorator.ts",
"src/modules/discord/fun/commands/doggo.commands.ts.bak",
"src/modules/discord/fun/services/kitty.service.ts.bak"
]
}

0 comments on commit 94392c2

Please sign in to comment.