Skip to content

Instantly share code, notes, and snippets.

@buthed010203
Last active October 13, 2020 14:42
Show Gist options
  • Save buthed010203/39b4961df9be0702d9112d0bbcc97b26 to your computer and use it in GitHub Desktop.
Save buthed010203/39b4961df9be0702d9112d0bbcc97b26 to your computer and use it in GitHub Desktop.
XP Rank System

Setup & Usage

The main command should be of type regex, the trigger should be .* like so. image The command has a few config options at the top:

  • $channel can be set to either 0 or a channel ID. If specified rankup messages will be sent to that channel, otherwise they are sent to the channel where the user ranks up.
  • $cooldown is the cooldown in seconds between messages which are counted towards the user's total message count, this is to prevent spam.
  • $ranks are the actual roles which are assigned to the user at specific message counts, you may add as many ranks as you want in the format of (cslice <roleid> <msg-required>)
  • $prefix Removed for now is the prefix to your commands, this will allow you to match your server prefix if needed.
    To complete the setup type -config, this will update your settings and inform you if anything is setup incorrectly.
    Once you have completed the setup correctly, you will be able to use -rank and -leaderboard/-lb.

TODO:

  • Add footer to -rank showing progress to next rank in % and messages/required
  • Allow -config command to have flags -ranks -channel and -cooldown so that the config can be changed in discord
  • Add a -ranks command or something similar to display all rank rewards nicely
  • Refer to an early version of the command to implement a custom prefix
  • A command list?
  • Proper docs
  • Eventually add support for leaderboard paginating (this is something that I have done before but removed because I wanted this all to be one command)

Version 1.0

{{/* CONFIGURE TO YOUR LIKING:
	channel is the id of the channel where rank up messages are sent, set to 0 for where the user is typing
	cooldown is the delay between counted messages in seconds
	rank format: (cslice role-id msg-requirement)
	when you are done, press save and run -config in discord */}}

{{$channel := 0}}
{{$cooldown := 30}}
{{$ranks := (cslice (cslice 1234 10) (cslice 6789) 5)}}
{{/* DONT TOUCH ANYTHING BEYOND THIS POINT
--------------------------------------------------------------------------------  V1 by buthed010203#0405 */}}

{{/* CHECK AND SET SERVER CONFIG */}}

{{if eq .Cmd "-config"}}{{if in (exec "viewperms") "ManageServer"}}{{$listroles := ""}}{{range .Guild.Roles}}{{$listroles = (joinStr "\n" $listroles .ID)}}{{end}}{{$check := 0}}

{{/* Warnings */}}
{{if not $channel}}{{- "**Warning:** No channel was set, rankup messages will be sent to the channel where the user sends their message."}}{{end -}}
{{if not $cooldown}}{{- "**Warning:** No cooldown was set, a cooldown of at least `30` is recommended to disincentivize spam."}}{{end -}}

{{/* Errors */}}
{{if not $ranks}}{{- "**Error:** No ranks were set. If you are trying to disable the command, set it to only run in specified channels but don't select any channels.\n<:downvote:763761318389874698> Config changes not updated!" -}}
{{- else}}{{range $ranks}}{{if not $check -}}
	{{- if not (eq (len .) 2)}}{{$check = 1 -}}
	{{- else if not (reFind (joinStr "" "(?i)\\b" (index . 0) "\\b") (toString $listroles))}}{{$check = 2 -}}
	{{- end}}{{end}}{{end -}}
{{if eq $check 1}}{{- "**Error:** It seems not all ranks are setup correctly.\n<:downvote:763761318389874698> Config changes not updated!" -}}
{{else if eq $check 2}}{{- "**Error:** Apparently one of the specified roles doesn't exist?\n<:downvote:763761318389874698> Config changes not updated!" -}}
{{else}}
{{/* Actually set the db entry */}}{{$cfg := (sdict "channel" $channel "cooldown" $cooldown "ranks" $ranks)}}{{dbSet 0 "cfg_rank" $cfg}}{{json $cfg -}}{{$ranks := ""}}{{range $cfg.ranks}}{{$ranks = (joinStr "" $ranks (printf "\n%-12d%s%d" (index . 1) "| " (index . 0)))}}{{end}}```prolog
Msgs Needed | Rank (Role){{$ranks}}```<:upvote:763761275506262026> Config changes saved successfully!{{end}}{{end}}{{else}}You dont have permission!{{end}}



{{/* LEADERBOARD COMMAND */}}
{{else if or (eq .Cmd "-leaderboard") (eq .Cmd "-lb")}}
{{$msg := ""}}
{{$pos := 0}}
{{$user := .User.ID}}
{{range dbTopEntries "msgcount" 30 0}}
{{$pos = add $pos 1}}
{{if eq .UserID $user}}{{$msg = (joinStr "" $msg "\n**" $pos ". " (getMember .UserID).User.String " - " (toInt .Value) "**")}}{{else}}
{{$msg = (joinStr "" $msg "\n" $pos ". " (getMember .UserID).User.String " - " (toInt .Value))}}{{end}}{{end}}{{$msg = cembed "description" $msg "title" "Message Leaderboard:"}}{{$x := sendMessageRetID nil $msg}}





{{/* RANK COMMAND */}}
{{else if eq .Cmd "-rank"}}

{{/* Legacy code for versions of the bot without dbRank (not recommended to use this as part of the command as it is very slow and can cause the cc has been running for more than 1 min error)
{{$start := currentTime}}
{{$count := dbCount "msgcount"}}
{{$user := .User.ID}}		
{{$success := 0}}
{{$skip := 0}}
{{$msg := 0}}
{{$lastreal := currentTime}}
{{$last := currentTime}}
{{$channel := .Channel.ID}}
{{$embed := cembed "description" (joinStr "" .User.Mention ", calculating your rank please wait as this may take a few seconds...\nDone: 0/ " $count " | 0%\nRunning For: `0 seconds`")}}
{{if not .ExecData}}
{{$msg = sendMessageRetID nil $embed}}{{sleep 1}}
{{else}}
{{$channel = .ExecData.channel}}
{{$start = .ExecData.start}}
{{$skip = .ExecData.skip}}
{{$msg = .ExecData.msg}}
{{$last = .ExecData.last}}
{{$lastreal = .ExecData.lastreal}}
{{end}}
{{$pos := $skip}}

{{$loops := 2}}{{if .IsPremium}}{{$loops = 10}}{{end}}{{range seq 1 $loops}}{{range dbTopEntries "msgcount" 100 $skip}}{{$pos = add $pos 1}}{{if eq .UserID $user}}{{$success = 1}}{{$embed := cembed "description" (joinStr "" "<@" $user ">, you are rank " $pos "/" $count ".")}}{{editMessage $channel $msg $embed}}<@{{$user}}>{{deleteResponse 1}}{{editMessage $channel $msg $embed}}{{end}}{{end}} {{$skip = add $skip 100}}{{if not $success}}{{$embed = cembed "description" (joinStr "" "<@" $user ">, calculating your rank please wait as this may take a few seconds...\nDone: " $pos "/" $count " | " (toInt (mult (fdiv (toFloat $pos) $count) 100)) "%\nRunning for: `" (humanizeDurationSeconds (currentTime.Sub $start)) "`")}}{{if gt (toDuration $last) (toDuration "1s")}}{{$lastreal = currentTime}}{{editMessage $channel $msg $embed}}{{end}}{{$last = currentTime.Sub $lastreal}}{{end}}{{end}}
	
{{if not $success}}{{execCC .CCID .Channel.ID 10 (dict "channel" $channel "skip" $skip "msg" $msg "start" $start "last" $last "lastreal" $lastreal)}}{{end}} */}}


{{$channel := (dbGet 0 "cfg_rank").Value.channel}}{{if not $channel}}{{$channel = .Channel.ID}}{{end}}{{$embed := cembed "description" (joinStr "" .User.Mention ", you are rank " (dbRank (sdict) .User.ID "msgcount") "/" (dbCount "msgcount") ".")}} {{sendMessage $channel $embed}}



{{/* ADJUST COUNT AND CHECK RANK */}}
{{else}}
{{if not (dbGet .User.ID "cld_levels")}}{{$config := (dbGet 0 "cfg_rank").Value}}{{if $config}}{{dbSetExpire .User.ID "cld_levels" "True" (toInt $config.cooldown)}}
{{$channel = $config.channel}}{{if not $channel}}{{$channel = .Channel.ID}}{{end}}{{$ranks = $config.ranks}}
{{$count := (toInt (dbIncr .User.ID "msgcount" 1))}}{{$mention := .User.Mention}}
{{range $ranks}}
{{if and (ge $count (toInt (index . 1))) (not (hasRoleID (toInt (index . 0))))}}
{{addRoleID (index . 0)}}
{{$embed := cembed "description" (joinStr "" "Added role " (mentionRoleID (index . 0)) " to: " $mention)}}
{{$x := sendMessageRetID $channel $mention}}
{{deleteMessage $channel $x 1}}
{{sendMessage $channel $embed}}
{{end}}{{end}}{{end}}{{end}}{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment