import { MessageEmbed } from 'discord.js' ; import Constants from '../../common/constants' ; import { Plugin } from '../../common/plugin' ; import { IContainer , IMessage , ChannelType } from '../../common/types' ; export default class GamblePlugin extends Plugin { public commandName : string = 'gamble' ; public name : string = 'Gamble Plugin' ; public description : string = 'Bet your Tacos on a coin flip' ; public usage : string = 'gamble 100\ngamble all' ; public override pluginAlias = [ ] ; public permission : ChannelType = ChannelType . Public ; public override pluginChannelName : string = Constants . Channels . Public . Games ; private _minBet : number = 10 ; public override commandPattern : RegExp = /(all|\d+)/ ; constructor ( public container ) { super ( ) ; } public async execute ( message , args ) { const userDoc = await this . container . pointService . getUserPointDoc ( message . author . id ) ; if ( userDoc . numPoints === 0 ) { message . reply ( 'You have no Tacos to gamble!' ) ; return ; } const pointsToGamble = args [ 0 ] . toLowerCase ( ) === 'all' ? userDoc . numPoints : parseInt ( args [ 0 ] ) ; message . reply ( { embeds : [ await this . _gamble ( message . author . id , userDoc . numPoints , pointsToGamble ) ] , } ) ; } private async _gamble ( userID , totalPoints , betAmount ) { if ( betAmount > totalPoints || betAmount < this . _minBet ) { return this . _createInvalidBetEmbed ( totalPoints , betAmount ) ; } const userWon = Math . random ( ) < 0.5 ; const newPoints = totalPoints + ( userWon ? betAmount : - betAmount ) ; await this . container . pointService . awardPoints ( userID , userWon ? betAmount : - betAmount ) ; return this . _createResultEmbed ( betAmount , userWon , newPoints ) ; } private _createResultEmbed ( betAmount , userWon , newPoints ) { const resultString = ( userWon ) : string => userWon ? ':confetti_ball: You won! :confetti_ball:' : ':no_entry_sign: You lost! :no_entry_sign:' ; return new MessageEmbed ( ) . setTitle ( resultString ( userWon ) ) . setDescription ( `You bet **${ betAmount }** and *${ userWon ? 'won' : 'lost' }!*\n` + `:taco: You now have **${ newPoints }** Tacos :taco:` ) . setColor ( userWon ? '#a3be8c' : '#bf616a' ) ; } private _createInvalidBetEmbed ( totalPoints , betAmount ) { return new MessageEmbed ( ) . setTitle ( 'That was an invalid bet amount' ) . setDescription ( `You have **${ totalPoints }** Tacos\nYou tried to bet **${ betAmount }**` ) . setFooter ( `There is a minimum bet of ${ this . _minBet }` ) . setColor ( '#bf616a' ) ; } }