-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Muse/Fun): Turn doggo/foxy & kitty commands in animal commands +…
… additions
- Loading branch information
1 parent
44436a4
commit 94392c2
Showing
14 changed files
with
147 additions
and
45 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
apps/muse/src/modules/discord/fun/commands/animal.commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}`, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
apps/muse/src/modules/discord/fun/services/animal.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './kitty.service'; | ||
export * from './animal.service'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type AnimelType = 'cat' | 'dog' | 'fox' | 'fish' | 'alpaca' | 'bird'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters