import Constants from '../../common/constants' ; import { Plugin } from '../../common/plugin' ; import { ChannelType , IContainer , IHttpResponse , IMessage } from '../../common/types' ; import { MessageEmbed } from 'discord.js' ; import ms from 'ms' ; export default class PubSubPlugin extends Plugin { public commandName : string = 'pubsub' ; public name : string = 'Pub Sub Plugin' ; public description : string = 'Get prices and steamy pictures of the subs you need in your life.' ; public usage : string = 'pubSub <subName (optional)> | <"list" | "types" (optional)>' ; public override pluginAlias = [ ] ; public permission : ChannelType = ChannelType . Public ; public override pluginChannelName : string = Constants . Channels . Public . Food ; private _API_URL : string = 'https://api.pubsub-api.dev' ; private _VALID_KEYS : string [ ] = [ 'random' , 'list' , 'types' , '' ] ; private _DEFAULT_INPUT : string = 'chicken-tenders' ; private _SUBS : string [ ] = [ ] ; private _EMBED_LIST = new MessageEmbed ( ) ; private _SUB_UPD_THRESH : number = ms ( '1d' ) ; private _LAST_UPD_TIME : number = 0 ; constructor ( public container ) { super ( ) ; this . _updateData ( ) ; } private _updateData ( ) { this . container . httpService . get ( `${ this . _API_URL }/allsubs/` ) . then ( ( response ) => { const subs = response . data ; this . _SUBS = subs . map ( ( subData ) => subData . name ) ; } ) . catch ( ( err ) => this . container . loggerService . warn ( err ) ) ; } public async execute ( message , args ? ) { const input = ( args ?? [ ] ) . join ( '-' ) . toLowerCase ( ) || this . _DEFAULT_INPUT ; if ( input === 'list' || input === 'types' ) { await message . reply ( { embeds : [ this . _generateEmbedList ( ) ] } ) ; return ; } const subType = this . _SUBS . find ( ( sub ) => sub === input ) ?? 'random' ; await this . container . httpService . get ( `${ this . _API_URL }/subs/?name=${ subType }` ) . then ( ( response ) => { if ( Math . floor ( response . status / 100 ) !== 2 ) { message . reply ( 'The API seems to be having some issues at this time.' ) ; return ; } const [ subData ] = response . data ; const embed = this . _generateEmbedSub ( subData ) ; message . reply ( { embeds : [ embed ] } ) ; } ) . catch ( ( err ) => this . container . loggerService . warn ( err ) ) ; } private _generateEmbedList ( ) { const lastListUpdate = Date . now ( ) - this . _LAST_UPD_TIME ; if ( lastListUpdate < this . _SUB_UPD_THRESH ) { return this . _EMBED_LIST ; } this . _EMBED_LIST = new MessageEmbed ( ) ; this . _LAST_UPD_TIME = Date . now ( ) ; this . _updateData ( ) ; this . _EMBED_LIST . addField ( 'Available Subs' , this . _normalizeName ( this . _SUBS . join ( '\n' ) ) ) ; return this . _EMBED_LIST ; } private _generateEmbedSub ( subData ) { const embed = new MessageEmbed ( ) ; const pubSubEmoji = this . container . guildService . getEmoji ( 'pubsub' ) ?? '🥪' ; subData . sub_name = this . _normalizeName ( subData . sub_name ) ; subData . status = subData . status . toLowerCase ( ) === 'true' ; [ , subData . last_sale_end ] = subData . last_sale . split ( '-' ) ; let saleInfo = `it **isn't** on sale currently 😭, but who could say no to ${ subData . price }.` + ` The last time it was on sale was ${ subData . last_sale }` ; if ( subData . status ) { saleInfo = `it **is** on sale for ${ subData . price } 🥳, until ${ subData . last_sale_end }` ; } embed . setColor ( '#0099ff' ) . setTitle ( subData . sub_name + ' sub' ) ; embed . setDescription ( `Get your own *${ subData . sub_name }* sub, ${ saleInfo }.` + ' So what are you waiting for?  Come on down to Publix now to get yourself a beautiful sub.' + ' Just look at this beauty right here! ' + `😍😍😍${ pubSubEmoji }${ pubSubEmoji }${ pubSubEmoji }🤤🤤🤤` ) ; embed . setImage ( subData . image ) ; embed . setFooter ( 'pulled from: pubsub-api.dev.' ) ; return embed ; } public override validate ( message , args ? ) { const input = ( args ?? [ ] ) . join ( '-' ) . toLowerCase ( ) ; const hasKeyword = this . _VALID_KEYS . includes ( input ) ; const hasSub = this . _SUBS . includes ( input ) ; return hasSub || hasKeyword ; } private _normalizeName ( input ) { return input . replace ( /-/g , ' ' ) . replace ( /\b\w/g , ( l ) => l . toUpperCase ( ) ) ; } }