Skip to content

Instantly share code, notes, and snippets.

@loopyd
Created April 28, 2020 16:03
Show Gist options
  • Save loopyd/3066292bbc9b02b7236638ab396c0d58 to your computer and use it in GitHub Desktop.
Save loopyd/3066292bbc9b02b7236638ab396c0d58 to your computer and use it in GitHub Desktop.
[Yagscript/yagg-cc/levels] Improved reactionListener to go along with leaderboard.go update. Includes a few bugfixes / patch to get things rolling again.
{{/*
WARNING: this command may be extremely buggy as I was unable to test it with limited users.
This command manages the pagination of the leaderboard command.
Recommended trigger: Reaction trigger on Reaction Added only.
*/}}
{{/* Configuration variables */}}
{{ $guildIconSize := 64 }} {{/* Size of the guild icon */}}
{{ $iconSize := 128 }} {{/* Size of the embed icon */}}
{{ $thumbnailURL := "https://i.imgur.com/mJ7zu6k.png" }} {{/* Use a self-hosted image */}}
{{ $emojiID := 704136047973630002 }} {{/* Copy the emoji ID you'd like to use for the icon and paste it here */}}
{{/* Instantiate global variables */}}
{{ $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 */}}
{{ $pageCount := toInt (roundCeil (fdiv (dbCount "xp") 10)) }} {{/* Number of pages */}}
{{ $guildIconURL := printf "https://cdn.discordapp.com/icons/%d/%s.png" .Guild.ID .Guild.Icon }}
{{ $iconURL := printf "https://cdn.discordapp.com/emojis/%d.png?v=1" $emojiID }}
{{ if ($iconSize) gt 0 }} {{ $iconURL = printf "%s&size=%d" $iconURL $iconSize }} {{ end }}
{{ if ($guildIconSize) gt 0 }} {{ $guildIconURL = printf "%s?size=%d" $guildIconURL $guildIconSize }} {{ end }}
{{ with and (eq .ReactionMessage.Author.ID 204255221017214977) .ReactionMessage.Embeds }} {{/* Checks for validity */}}
{{ $embed := index . 0 }} {{/* The first embed */}}
{{ if and (eq $embed.Author.Name "Leaderboard") $embed.Footer }} {{/* More checks */}}
{{ $page = toInt (index (reFindAll `\d+` $embed.Footer.Text) 0) }} {{/* We presume that this is valid, and get the page num, this was modified to get the first match of a number to fit the new style */}}
{{ $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 }}
{{ $displayRanks := "" }}
{{ $displayNames := "" }}
{{ $displayLevels := "" }}
{{- range $users -}} {{/* Loop over users and format */}}
{{ $xp := toInt .Value }} {{/* The user XP */}}
{{ $rank = add $rank 1 }} {{/* The user rank */}}
{{/* Initialize Ordinal Formatter */}}
{{ $medal := "" }}
{{ $markdownBefore := "" }}
{{ $markdownAfter := "" }}
{{ $suff := "" }}
{{/* Get Medal and Formatting Chars */}}
{{ if (eq (toInt $rank) 1) }}
{{ $medal = ":first_place:" }}
{{ $markdownBefore = "_**__" }}
{{ $markdownAfter = "__**_" }}
{{ else if (eq (toInt $rank) 2) }}
{{ $medal = ":second_place:" }}
{{ $markdownBefore = "__**" }}
{{ $markdownAfter = "**__" }}
{{ else if (eq (toInt $rank) 3) }}
{{ $medal = ":third_place:" }}
{{ $markdownBefore = "_**" }}
{{ $markdownAfter = "**_" }}
{{ else if (and (gt (toInt $rank) 3) (le (toInt $rank) 10)) }}
{{ $medal = ":medal:" }}
{{ $markdownBefore = "_" }}
{{ $markdownAfter = "_" }}
{{ end }}
{{/* Get Ordinal Suffix */}}
{{ $ones := toInt (mod (toInt $rank) 10) }}
{{ $tens := toInt (mod (roundFloor (fdiv (toInt $rank) 10)) 10) }}
{{ if (eq $tens 1) }}
{{ $suff = "th" }}
{{ else }}
{{ if (eq $ones 1) }}
{{ $suff = "st" }}
{{ else if (eq $ones 2) }}
{{ $suff = "nd" }}
{{ else if (eq $ones 3) }}
{{ $suff = "rd" }}
{{ else }}
{{ $suff = "th" }}
{{ end }}
{{ end }}
{{ $rankStr := printf "%s %s%d%s%s"
$medal $markdownBefore (toInt $rank) $suff $markdownAfter
}} {{/* Ordinal format rank. */}}
{{ $displayRanks = printf "%s\n%s"
$displayRanks $rankStr
}} {{/* Format ranks column */}}
{{ $displayNames = printf "%s\n[%s](https://discordapp.com/users/%d/)"
$displayNames .User.Username .User.ID
}} {{/* Format users column */}}
{{ $displayLevels = printf "%s\nLevel %d (%d XP)"
$displayLevels (toInt (roundFloor (mult 0.1 (sqrt $xp)))) $xp
}} {{/* Format levels column */}}
{{- end -}}
{{ editMessage nil .ReactionMessage.ID (cembed
"title" nil
"author" (sdict
"name" "Leaderboard"
"icon_url" $iconURL
)
"thumbnail" (sdict "url" $thumbnailURL)
"color" 14232643
"description" nil
"fields" (cslice
(sdict
"name" "Rank"
"value" $displayRanks
"inline" true
)
(sdict
"name" "Member"
"value" $displayNames
"inline" true
)
(sdict
"name" "Level"
"value" $displayLevels
"inline" true
)
)
"footer" (sdict
"icon_url" $guildIconURL
"text" (printf "Page %d of %d\t%s | Yagscript by HeavyPaws"
$page $pageCount .Guild.Name)
)
) }} {{/* Edit embed */}}
{{ end }}
{{ end }}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment