Skip to main content

Coin Flip

This command flips a coin landing on heads or tails to lose or double the bet.

caution

This command assumes that you have an existing currency system set up.

Trigger

Type: Command
Trigger: coinflip

Usage

  • -coinflip <heads/tails> <bet> - Flips the coin, betting bet on the result being heads/tails.

Configuration

  • 📌 $CHANNEL
    Channel ID where the game is played. Set to .Channel.ID to allow play in any channel.

  • $MIN_BET
    Minimum amount users can bet.

  • $MAX_BET
    Maximum amount users can bet.

  • 📌 $DB_KEY
    Database entry name where the currency is stored.

  • $COOLDOWN
    Cooldown for the command in seconds.

Code

{{/*
Coin-flip game that integrates with an existing currency system.
See <https://yagpdb-cc.github.io/fun/coinflip> for more information.

Author: DaviiD1337 <https://github.com/DaviiD1337>
Co-Author: H1nr1 <https://github.com/H1nr1>
*/}}

{{/* Configuration variables start */}}
{{ $CHANNEL := .Channel.ID }}
{{ $MIN_BET := 100 }}
{{ $MAX_BET := 500 }}
{{ $DB_KEY := "CREDITS" }}
{{ $COOLDOWN := 5 }}
{{/* Configuration variables end */}}

{{ $err := false }}
{{ $img := "https://cdn.discordapp.com/attachments/707661790443733022/782710794634264616/d74906d39a1964e7d07555e7601b06ad.gif" }}
{{ $heads := "https://cdn.discordapp.com/attachments/707661790443733022/782713935341027358/cap.png" }}
{{ $tails := "https://cdn.discordapp.com/attachments/707661790443733022/782713937979637760/pajura.png" }}
{{ $embed := sdict "author" (sdict "name" .User.Username "icon_url" (.User.AvatarURL "256")) }}

{{ if not .ExecData }}
{{ $choice := reFind `(?i)head|tail` .StrippedMsg | lower }}
{{ $bet := reFind `\d+` .StrippedMsg | toInt }}

{{ if not (eq (len .CmdArgs) 2) }}
{{ $err = printf "Syntax: **%scoinflip <heads/tails> <bet>**" .ServerPrefix }}

{{ else if not $choice }}
{{ $err = "You can only choose from **heads** or **tails**" }}

{{ else if not $bet }}
{{ $err = printf "Please provide a bet between **%d** and **%d**" $MIN_BET $MAX_BET }}

{{ else if or (lt $bet $MIN_BET) (gt $bet $MAX_BET) }}
{{ $err = printf "Bet must be minimum **%d** and maximum **%d**!" $MIN_BET $MAX_BET }}

{{ else if $cd := dbGet .User.ID "CF_cooldown" }}
{{ $err = print "You need to wait **" ($cd.ExpiresAt.Sub currentTime | humanizeDurationSeconds) "** to use this command again." }}

{{ else if lt ($userBalance := (dbGet .User.ID $DB_KEY).Value | toInt) $bet }}
{{ $err = printf "You dont have **%d** credits.\nYou only have **%d** credits." $bet $userBalance }}

{{ else if dbGet .User.ID "CF_isFlipping" }}
{{ $err = "You can use this command once your last coin has landed!" }}
{{ end }}

{{ if $err }}
{{ $embed.Set "description" $err }}
{{ $embed.Set "color" 16488706 }}
{{ $message := sendMessageRetID nil (cembed $embed) }}
{{ deleteMessage .Channel.ID $message 5 }}
{{ deleteTrigger }}
{{ return }}
{{ end }}

{{/* flip may proceed */}}
{{ dbSet .User.ID "CF_isFlipping" true }}
{{ $embed.Set "image" (sdict "url" $img) }}
{{ $embed.Set "color" 16765696 }}
{{ $embed.Set "description" "**The coin is in the air... Please wait for it to land...**" }}
{{ $m := sendMessageRetID $CHANNEL (cembed $embed) }}
{{ $randChannel := (index .Guild.Channels (randInt (len .Guild.Channels))).ID }}
{{ scheduleUniqueCC .CCID $randChannel 3 .User.ID (sdict "channel" $CHANNEL "m" $m "b" $bet "c" $choice )}}

{{ else }}
{{ $channel := .ExecData.channel }}
{{ $m := .ExecData.m }}
{{ $bet := .ExecData.b }}
{{ $choice := print .ExecData.c "s" }}

{{ $result := (dict
0 (sdict "side" "heads" "img" $heads)
1 (sdict "side" "tails" "img" $tails)
).Get (randInt 2) }}

{{ $embed.Set "image" (sdict "url" $result.img) }}

{{ if eq $choice $result.side }}
{{ $result.Set "WL" "won" }}
{{ $result.Set "diff" (mult $bet 2) }}
{{ $embed.Set "color" 40811 }}
{{ else }}
{{ $result.Set "WL" "lost" }}
{{ $result.Set "diff" $bet }}
{{ $bet = mult $bet -1 }}
{{ $embed.Set "color" 16488706 }}
{{ end }}

{{ $balance := dbIncr .User.ID $DB_KEY $bet | toInt }}
{{ $embed.Set "description" (printf "**----------**\nYou chose **%s** | The coin landed on **%s**.\nYou %s **%d** %s.\nNow you have **%d** %s.\n**----------**" $choice $result.side $result.WL $result.diff (lower $DB_KEY) $balance (lower $DB_KEY)) }}
{{ dbSetExpire .User.ID "CF_cooldown" true $COOLDOWN }}
{{ dbDel .User.ID "CF_isFlipping" }}
{{ editMessage $channel $m (cembed $embed) }}
{{ end }}

Author

This custom command was written by @DaviiD1337 with contributions from @H1nr1.