Skip to main content

View Rank

This command displays the rank of a member, defaulting to the triggering member. There are also overloads to set your rank card background (if enabled) and rank embed colour.

Trigger

Type: Regex
Trigger: \A(-|<@!?204255221017214977>\s*)(rank|lvl|xp)(\s+|\z)

Usage

  • -rank - Views your own rank.
  • -rank <user> - Views the rank of the user provided.
  • -rank set-color <hex> - Sets your rank embed colour to the hex provided.
  • -rank set-color default - Sets your rank embed colour to the default value.
  • -rank set-background <link> - Sets your rank card background.
  • -rank set-background del - Deletes your rank card background.

Configuration

  • $rankcard
    Whether or not a rank card should be displayed instead of an embed.

  • $background
    The default rank card background. Only applies if $rankcard is true.

Code

{{/*
Views the rank of a given member, or the triggering user.
See <https://yagpdb-cc.github.io/leveling/view-rank> for more information.

Author: jo3-l <https://github.com/jo3-l>
*/}}

{{/* Configurable values */}}
{{ $rankcard := false }}
{{ $background := "https://xbox-store-checker.com/assets/upload/game/2020/06/optimize/9n5qmw0x81jm-background.jpg" }}
{{/* End of configurable values */}}

{{/* Instantiate constants */}}
{{ $barEmpty := "□" }}
{{ $barFull := "■" }}
{{ $xp := 0 }} {{/* Xp of user */}}
{{ $color := 14232643 }} {{/* Embed color */}}
{{ $user := .User }} {{/* Target user */}}
{{ $colorSet := false }}

{{ with .CmdArgs }}
{{ $temp := userArg (index . 0) }}
{{ if $temp }}
{{ $user = $temp }}
{{ with dbGet $user.ID "xpColor" }} {{ $color = .Value }} {{ end }}
{{ 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 `\A(?:#?default|([a-fA-F\d]{1,6}))\z` (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 }}
{{ dbSet $user.ID "hex" $hex }}
{{ $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 not $colorSet }}
{{/* Get the color for this user */}}
{{ with dbGet $user.ID "xpColor" }} {{ $color = .Value }} {{ end }}
{{ 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 */}}
{{ $percentage = div $percentage 10 | toInt }} {{/* Percentage out of 10 */}}

{{ $bar := "" }} {{/* The progress bar */}}
{{ range seq 1 11 }}
{{- if ge $percentage . }} {{- $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 }}

{{ $embed := sdict
"title" (printf "❯ %s's Rank" $user.String)
"color" $color
"description" (printf
"❯ **%d / %d** XP\n❯ **Total:** %d\n❯ **Level:** %d\n❯ **Progress bar:**\n[**%s**](https://yagpdb.xyz)"
(toInt $current) (toInt $total) (toInt $xp) (toInt $level) $bar)
}} {{/* Format embed */}}

{{ $rank := 0 }} {{/* Current rank of user */}}
{{ range $index, $entry := dbTopEntries "xp" 100 0 }} {{- /* Loop over top 100 entries - see if user is in top 100 */}}
{{- if eq .UserID $user.ID }} {{- $rank = add $index 1 }} {{- end -}}
{{ end }}

{{ if $rank }}
{{ $embed.Set "description" (printf
"🏆 *Member of Top 100*\n\n❯ **Rank:** %d\n%s"
$rank
$embed.description
) }} {{/* If user in top 100, update the description */}}
{{ end }}
{{ $bg := true }}
{{ with .CmdArgs }}
{{ $bg = false }}
{{ if and (eq (index . 0) "set-background") (ge (len .) 2) }}
{{ if eq (index . 1) "del" }} {{ dbDel $.User.ID "background" }} deleted
{{ else if (reFindAllSubmatches `https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,63}\b(?:(?:(?:,?[-a-zA-Z0-9@:%_\+.~#?!&/=*]*)|(?:,?\([-a-zA-Z0-9@:%_\+.~#?!&/=*]*\))*|\[\])*)` (index . 1)) }}
{{ dbSet $.User.ID "background" (index . 1) }} doneso
{{ else }} thats not a valid link!
{{ end }}
{{ end }}
{{ end }}
{{ if ($rankcard) }}
{{ $rank = or $rank "100" }}
{{ $username := urlescape ($user.Username) }}
{{ with dbGet $user.ID "background" }}
{{ $background = .Value }} {{end}}
{{$pfp := "https://discord.com/assets/322c936a8c8be1b803cd94861bdfa868.png"}}
{{if $user.Avatar}} {{$pfp = ($user.AvatarURL "256")}} {{end}}

{{ sendMessage nil (cembed "color" 4645612 "image" (sdict "url" (print "https://vacefron.nl/api/rankcard?username=" $username "&avatar=" $pfp "&level=" $level "&rank=" $rank "&currentxp=" $current "&nextlevelxp=" $total "&previouslevelxp=0&custombg=" $background "&xpcolor=" ((dbGet $user.ID "hex").Value)))) }}
{{ else }}
{{ sendMessage nil (cembed $embed) }}
{{ end }}
{{ end }}

Author

This custom command was written by @jo3-l.