import Constants from '../../common/constants' ; import { Plugin } from '../../common/plugin' ; import { ChannelType , IContainer , IHttpResponse , IMessage , Maybe } from '../../common/types' ; import { MessageEmbed , TextChannel } from 'discord.js' ; class Breed { public name : string = '' ; public id : string = '' ; } export default class CatPlugin extends Plugin { public commandName : string = 'cat' ; public name : string = 'Cat Plugin' ; public description : string = 'Generates pictures of cats.' ; public usage : string = 'cat <breed (optional)>' ; public override pluginAlias = [ 'cats' ] ; public permission : ChannelType = ChannelType . Public ; public override pluginChannelName : string = Constants . Channels . Public . Pets ; private _API_URL : string = 'https://api.thecatapi.com/v1/' ; private _breeds : Breed [ ] = [ ] ; private _embedBreeds : Maybe < MessageEmbed > ; constructor ( public container ) { super ( ) ; this . container . httpService . get ( `${ this . _API_URL }breeds` ) . then ( ( response ) => { const breeds = response . data ; this . _breeds = breeds . map ( ( breedData ) => { return { name : breedData . name . toLowerCase ( ) , id : breedData . id . toLowerCase ( ) , } ; } ) ; } ) . catch ( ( err ) => this . container . loggerService . warn ( err ) ) ; } public async execute ( message , args ? ) { if ( args === undefined || args . length === 0 ) { args = [ '' ] ; } if ( args [ 0 ] . includes ( 'breed' ) ) { await this . container . messageService . sendStringOrEmbed ( message . channel as TextChannel , this . _getListEmbed ( ) || 'Failed to load breeds.' ) ; return ; } const breedIn = this . _parseCommand ( args ) ; let searchCom = '' ; const breedEntry = this . _breeds . find ( ( breed ) => breed . name === breedIn ) ; if ( breedEntry !== undefined ) { searchCom = '&breed_ids=' + breedEntry . id ; } else if ( breedIn !== 'random' && breedIn !== '' ) { message . reply ( 'Breed not found.' ) ; return ; } this . container . loggerService . debug ( searchCom ) ; await this . container . httpService . get ( `${ this . _API_URL }images/search?limit=1${ searchCom }` ) . then ( ( response ) => { message . reply ( { files : [ response . data [ 0 ] . url ] , } ) ; } ) . catch ( ( err ) => this . container . loggerService . warn ( err ) ) ; } private _getListEmbed ( ) { if ( this . _embedBreeds ) { return this . _embedBreeds ; } const breedsAsArray = this . _breeds . map ( ( breedData ) => { return breedData . name ; } ) ; this . _embedBreeds = this . container . messageService . generateEmbedList ( breedsAsArray ) ; this . _embedBreeds . setColor ( '#0099ff' ) . setTitle ( 'Breeds' ) ; return this . _embedBreeds ; } private _parseCommand ( args ) { return args . map ( ( str ) => str . toLowerCase ( ) ) . join ( ' ' ) ; } }