Skip to main content

Reaction Handler

This command handles reactions for the leveling system.

Trigger

Type: Reaction
Additional options: Added reactions only

Code

{{/*
Handles reactions for the leveling system.
See <https://yagpdb-cc.github.io/leveling/reaction-handler> for more information.

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

{{ $action := .Reaction.Emoji.Name }} {{/* The action being ran */}}
{{ $validEmojis := cslice "▶️" "◀️" }} {{/* Valid emojis */}}
{{ $isValid := false }} {{/* Whether this is actually a valid embed / leaderboard embed */}}
{{ $page := 0 }} {{/* The current page */}}
{{ with and (eq .ReactionMessage.Author.ID 204255221017214977) .ReactionMessage.Embeds }} {{/* Checks for validity */}}
{{ $embed := index . 0 }} {{/* The first embed */}}
{{ if and (eq $embed.Title "❯ Leaderboard") $embed.Footer }} {{/* More checks */}}
{{ $page = reFind `\d+` $embed.Footer.Text }} {{/* We presume that this is valid, and get the page num */}}
{{ $isValid = true }} {{/* Yay, it is valid */}}
{{ end }}
{{ end }}
{{ if and (in $validEmojis $action) $isValid $page }} {{/* Even more checks for validity... */}}
{{ deleteMessageReaction nil .ReactionMessage.ID .User.ID $action }}
{{ if eq $action "▶️" }} {{ $page = add $page 1 }} {{/* Update page according to emoji */}}
{{ else }} {{ $page = sub $page 1 }} {{ end }}
{{ if ge $page 1 }} {{/* Otherwise, dbTopEntries throws error due to negative skip */}}
{{ $skip := mult (sub $page 1) 10 }} {{/* Get skip */}}
{{ $users := dbTopEntries "xp" 10 $skip }} {{/* Fetch entries */}}
{{ if (len $users) }} {{/* If there are users on this page, proceed */}}
{{ $rank := $skip }}
{{ $display := "" }} {{/* Display for leaderboard embed */}}
{{ range $users }} {{- /* Loop over users and format */}}
{{- $xp := toInt .Value }} {{- /* The user XP */}}
{{- $rank = add $rank 1 }} {{- /* The user rank */}}
{{- $display = printf "%s\n• **%d.** [%s](https://yagpdb.xyz) :: Level %d (%d XP)"
$display $rank .User.String (toInt (roundFloor (mult 0.1 (sqrt $xp)))) $xp
}} {{- /* Format display */ -}}
{{ end }}
{{ editMessage nil .ReactionMessage.ID (cembed
"title" "❯ Leaderboard"
"thumbnail" (sdict "url" "https://i.imgur.com/mJ7zu6k.png")
"color" 14232643
"description" $display
"footer" (sdict "text" (joinStr "" "Page " $page))
) }} {{/* Edit embed */}}
{{ end }}
{{ end }}
{{ end }}

Author

This custom command was written by @jo3-l.