Skip to content

Instantly share code, notes, and snippets.

@loopyd
Created April 28, 2020 01:38
Show Gist options
  • Save loopyd/97383dab93d8ae74048ecf6a86445979 to your computer and use it in GitHub Desktop.
Save loopyd/97383dab93d8ae74048ecf6a86445979 to your computer and use it in GitHub Desktop.
[Yagscript/yag-cc/levels] Improved rank card with cleaner embed display.
{{/*
This command manages viewing the rank of a given member.
Use by running `-rank [member]` where member is optional. Defaults to yourself.
You may also set a color for your rank card using `-rank set-color <hex>` or `-rank set-color default`.
Recommended trigger: Regex trigger with trigger `^-(rank|lvl|xp)`.
*/}}
{{/* Instantiate constants */}}
{{ $barEmpty := "░" }} {{/* Progress bar empty character */}}
{{ $barFull := "█" }} {{/* Progress bar fill character */}}
{{ $progBarLength := 17 }} {{/* Progress bar character length */}}
{{ $xp := 0 }} {{/* Xp of user */}}
{{ $color := 14232643 }} {{/* Embed color */}}
{{ $colorSet := false }}
{{ $thumbnailSize := 64 }} {{/* Embed Thumbnail size */}}
{{ $iconSize := 128 }} {{/* Embed Icon Size */}}
{{ $guildIconSize := 32 }} {{/* Guild Icon Size */}}
{{ $emojiID := 640717810145820730 }} {{/* Copy the emoji ID you'd like to use for the icon and paste it here */}}
{{ with dbGet .User.ID "xpColor" }} {{ $color = .Value }} {{ end }}
{{ $user := .User }} {{/* Target user */}}
{{ with .CmdArgs }}
{{ $temp := userArg (index . 0) }}
{{ if $temp }}
{{ $user = $temp }}
{{ else if and (eq (index . 0) "set-color") (ge (len .) 2) }}
{{ $colorSet = true }}
{{ $multipliers := cslice 1048576 65536 4096 256 16 1 }}
{{ $hex2dec := sdict "A" 10 "B" 11 "C" 12 "D" 13 "E" 14 "F" 15 }}
{{ with reFindAllSubmatches `(?:#?default|([a-fA-F\d]{1,6}))` (joinStr " " (slice . 1)) }}
{{ $hex := "D92C43" }}
{{ with index . 0 1 }}
{{ $hex = (printf "%06s" .) | upper }}
{{ end }}
{{ $dec := 0 }}
{{- range $k, $v := split $hex "" -}}
{{ $multiplier := index $multipliers $k }}
{{ $num := or ($hex2dec.Get $v) $v}}
{{ $dec = add $dec (mult $num $multiplier) }}
{{- end -}}
{{ dbSet $user.ID "xpColor" $dec }}
{{ $user.Mention }}, I set your rank card color to `#{{ $hex }}`.
{{ else }}
Please provide a valid hex to set your rank card color to.
{{ end }}
{{ end }}
{{ end }} {{/* If user was provided, use that */}}
{{ if not $colorSet }}
{{ with dbGet $user.ID "xp" }}
{{ $xp = .Value }}
{{ end }} {{/* If XP is there, use that */}}
{{ $level := roundFloor (mult 0.1 (sqrt $xp)) }} {{/* Calculate level */}}
{{ $current := sub $xp (mult 100 $level $level) }} {{/* current XP for this level */}}
{{ if lt (toInt $xp) 100 }}
{{ $current = $xp }}
{{ end }} {{/* If this level < level 1, use total XP as current */}}
{{ $nextLvl := add $level 1 }} {{/* Next level */}}
{{ $total := sub (mult 100 $nextLvl $nextLvl) (mult 100 $level $level) }} {{/* The total until next level */}}
{{ $percentage := (mult (fdiv $current $total) 100) | roundFloor }} {{/* Start with percentage calculations */}}
{{ $stopChar := (mult (fdiv $percentage 100) $progBarLength) | roundFloor | toInt }}
{{ $bar := "" }} {{/* The progress bar */}}
{{- range seq 1 (add $progBarLength 1) -}}
{{ if ge $stopChar . }} {{ $bar = joinStr "" $bar $barFull }} {{/* We join with full bar unicode if percentage > current iteration */}}
{{ else }} {{ $bar = joinStr "" $bar $barEmpty }} {{ end }} {{/* Otherwise, join with empty bar */}}
{{- end -}}
{{ $bar = joinStr "" $bar " " (toInt $percentage) "%" }}
{{/* Build URLs for images */}}
{{ $thumbnailURL := ($user.AvatarURL (toString $thumbnailSize)) }}
{{ $iconURL := printf "https://cdn.discordapp.com/emojis/%d.gif?v=1" $emojiID }}
{{ $guildIconURL := printf "https://cdn.discordapp.com/icons/%d/%s.png" .Guild.ID .Guild.Icon }}
{{ if ($iconSize) gt 0 }} {{ $iconURL = printf "%s&size=%d" $iconURL $iconSize }} {{ end }}
{{ if ($guildIconSize) gt 0 }} {{ $guildIconURL = printf "%s?size=%d" $guildIconURL $guildIconSize }} {{ end }}
{{ $embed := sdict
"title" nil
"color" $color
"description" nil
"footer" (sdict
"icon_url" $guildIconURL
"text" (printf "%s | CCscript by HeavyPaws" .Guild.Name)
)
"thumbnail" (sdict "url" $thumbnailURL)
"author" (sdict
"name" (printf "%s Rank Card" $user.Username)
"icon_url" $iconURL
)
"fields" (cslice
(sdict
"name" "Rank"
"value" "_unranked_\nInteract more to rank up!"
"inline" false
)
(sdict
"name" "Level"
"value" (printf "%d" (toInt $level) )
"inline" true
)
(sdict
"name" "XP"
"value" (printf "%d/%d" (toInt $current) (toInt $total) )
"inline" true
)
(sdict
"name" "Level Progress"
"value" $bar
"inline" false
)
)
}} {{/* Format embed */}}
{{ $rank := 0 }} {{/* Current rank of user */}}
{{ $numUsers := dbCount "xp" }} {{/* Number of Users */}}
{{- range $index, $entry := dbTopEntries "xp" 10 0 -}} {{/* Loop over top 10 entries - see if user is in top 10 */}}
{{ if eq .UserID $user.ID }} {{ $rank = add $index 1 }} {{ end }}
{{- end -}}
{{ if $rank }}
{{ if (eq (toInt $rank) 1) }}
{{ $rank = (printf ":first_place: __**%dst!**__" (toInt $rank)) }}
{{ else if (eq (toInt $rank) 2) }}
{{ $rank = (printf ":second_place: **%dnd!**" (toInt $rank)) }}
{{ else if (eq (toInt $rank) 3) }}
{{ $rank = (printf ":third_place: _%drd!_" (toInt $rank)) }}
{{ else }}
{{ $rank = (printf ":medal: %dth" (toInt $rank)) }}
{{ end }}
{{ $nf := ($embed.Get "fields") }}
{{ $nf.Set 0 (sdict
"name" "Rank"
"value" $rank
"inline" false
)}}
{{ $embed.Set "fields" $nf }}
{{ end }}
{{ sendMessage nil (cembed $embed) }}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment