Skip to main content

Reaction Handler

This command allows users to react to messages within the starboard channel with stars/anti-stars.

For more information about the starboard system, see this page.

Trigger

Type: Reaction
Additional options: Added + Removed Reactions

note

This command should be set to only run in your starboard channel in the channel restrictions.

Configuration

See the starboard system overview for in-depth information about configuration.

Code

{{/*
Allows users to react to messages within the starboard channel with stars/anti-stars.
See <https://yagpdb-cc.github.io/fun/starboard/reaction-handler> for more information.

Author: dvoraknt <https://github.com/dvoraknt>
*/}}

{{/* Configurable values */}}
{{$starEmoji := "⭐"}}
{{$starEmFull := "⭐"}}
{{$starLimit := 4}}
{{$starboard := 712681639410663516}}
{{$maxAge := "2w"}}
{{$selfStarEnable := false}}
{{$warnMessages := false}}

{{$antiStarEnable := false}}
{{$antiStarEmoji := "❌"}}
{{$antiStarEmFull := "❌"}}
{{$antiStarExtra := 0}}
{{/* End of configurable values */}}

{{$starboardMessage := 0}}{{$thisID := 0}}{{$chanID := 0}}{{$count := 0}}{{$antiCount := 0}}
{{$starboardMessage = .ReactionMessage.ID}}
{{with (dbGet 0 "starboardMessages").Value}}{{$data := (print . )}}
{{$idRegex := printf `(\d+):%d:(\d+)` $starboardMessage}}
{{with reFindAllSubmatches $idRegex .}} {{$thisID = index . 0 1}} {{$chanID = index . 0 2}} {{end}}
{{if gt (len $data) 90000}}
{{dbSet 0 "starboardMessages" (reReplace `\A(?:\n?(?:\d+:){2}\d+\n?){160}` $data "")}}
{{end}}
{{end}}

{{$data := ""}}{{$sboardPost := print $thisID}}{{$skip := true}}{{$starboardData := ""}}
{{if and (or (eq .Reaction.Emoji.Name $starEmoji) (eq .Reaction.Emoji.Name $antiStarEmoji)) (not .User.Bot)}}
{{if not (dbGet 0 "starboardReactions")}}{{dbSet 0 "starboardReactions" (sdict $sboardPost (sdict "ID" $sboardPost "listID" (print .User.ID ",")))}}{{end}}
{{with (dbGet 0 "starboardReactions").Value}}{{$data = sdict .}}{{end}}
{{$starboardData = $data.Get $sboardPost}}

{{if and .ReactionAdded (not $selfStarEnable) (eq .User.ID ((getMessage $chanID $thisID).Author).ID)}}
{{deleteMessageReaction $starboard $starboardMessage .User.ID $starEmFull $antiStarEmFull}}
{{if $warnMessages}}{{$ret := sendMessageRetID nil (print .User.Mention " You can't influence your own post!")}}
{{deleteMessage $starboard $ret 3}}{{end}}
{{else if $starboardData}}
{{$starboardData = sdict $starboardData}}

{{if gt (len (print $data)) 18000}}
{{$dataSlice := cslice}}
{{range $k, $v := $data}}{{- $dataSlice = $dataSlice.Append (cslice $k $v) -}}{{end}}
{{$lenCount := 0}}{{$toDel := cslice}}
{{range $dataSlice}}
{{- if lt $lenCount 2000}}
{{- $lenCount = add $lenCount (len (print .)) -}}
{{- $toDel = $toDel.Append (index . 0)}}
{{- end -}}{{- end -}}
{{range $toDel}}
{{- $data.Del . -}}
{{- end -}}{{- end -}}

{{if and .ReactionAdded (eq .Reaction.Emoji.Name $starEmoji)}}
{{if reFind (print "PO" .User.ID ",") $starboardData.listID}}
{{deleteMessageReaction $starboard $starboardMessage .User.ID $starEmFull}}
{{if $warnMessages}}{{$ret := sendMessageRetID nil (print .User.Mention " You already reacted on the post!")}}
{{deleteMessage $starboard $ret 3}}{{end}}
{{else if reFind (print .User.ID ",") $starboardData.listAnti}}
{{deleteMessageReaction $starboard $starboardMessage .User.ID $starEmFull}}
{{if $warnMessages}}{{$ret := sendMessageRetID nil (print .User.Mention " You can't star and anti-star the same message!")}}
{{deleteMessage $starboard $ret 3}}{{end}}
{{else}}
{{$IDregex:= print "SB" .User.ID ","}}
{{$starboardData.Set "listID" (print $starboardData.listID $IDregex)}}
{{$skip = false}}
{{end}}

{{else if eq .Reaction.Emoji.Name $starEmoji}}
{{$IDregex := print "SB" .User.ID ","}}
{{if reFind $IDregex $starboardData.listID}}
{{$starboardData.Set "listID" (reReplace $IDregex $starboardData.listID "")}}
{{end}}
{{$skip = false}}
{{end}}

{{if and $antiStarEnable .ReactionAdded (eq .Reaction.Emoji.Name $antiStarEmoji)}}
{{if reFind (print "PO" .User.ID ",") $starboardData.listAnti}}
{{deleteMessageReaction $starboard $starboardMessage .User.ID $antiStarEmFull}}
{{if $warnMessages}}{{$ret := sendMessageRetID nil (print .User.Mention " You already reacted on the post!")}}
{{deleteMessage $starboard $ret 3}}{{end}}
{{else if reFind (print .User.ID ",") $starboardData.listID}}
{{deleteMessageReaction $starboard $starboardMessage .User.ID $antiStarEmFull}}
{{if $warnMessages}}{{$ret := sendMessageRetID nil (print .User.Mention " You can't star and anti-star the same message!")}}
{{deleteMessage $starboard $ret 3}}{{end}}
{{else}}
{{$IDregex:= print "SB" .User.ID ","}}
{{$starboardData.Set "listAnti" (print $starboardData.listAnti $IDregex)}}
{{$skip = false}}
{{end}}

{{else if and $antiStarEnable (eq .Reaction.Emoji.Name $antiStarEmoji)}}
{{$IDregex := print "SB" .User.ID ","}}
{{if reFind $IDregex $starboardData.listAnti}}
{{$starboardData.Set "listAnti" (reReplace $IDregex $starboardData.listAnti "")}}
{{end}}
{{$skip = false}}
{{end}}

{{$data.Set $sboardPost $starboardData}}
{{dbSet 0 "starboardReactions" $data}}

{{else}}
{{$data.Set $sboardPost (sdict "ID" $sboardPost "listID" (print .User.ID ","))}}
{{dbSet 0 "starboardReactions" $data}}
{{$skip = false}}
{{end}}
{{end}}

{{$countTwo := 0}}{{$antiCountTwo := 0}}
{{range .ReactionMessage.Reactions}}
{{- if eq .Emoji.Name $starEmoji}}
{{- $countTwo = .Count}}
{{- end -}}
{{- if and $antiStarEnable (eq .Emoji.Name $antiStarEmoji)}}
{{- $antiCountTwo = .Count}}
{{- end -}}
{{end}}

{{range (getMessage $chanID $thisID).Reactions}}
{{- if eq .Emoji.Name $starEmoji}}
{{- $count = (sub (add $countTwo .Count) 1)}}
{{- end -}}
{{- if and $antiStarEnable (eq .Emoji.Name $antiStarEmoji)}}
{{- $antiCount = (sub (add $antiCountTwo .Count) 1)}}
{{else if $antiStarEnable}}
{{- $antiCount = sub $antiCountTwo 1}}
{{- end -}}
{{end}}

{{if not $antiStarEnable}} {{$antiStarExtra = 0}} {{end}} {{/*resetting count to zero to prevent user configured variable from interrupting desired functionality*/}}
{{if and (not $skip) (or (lt $count $starLimit) (ge (add $antiCount $antiStarExtra) $count))}}
{{with (dbGet 0 "starboardMessages").Value}}
{{$idRegex := printf `\n%s:(\d+):(\d+)` $thisID}}
{{with reFindAllSubmatches $idRegex .}} {{$starboardMessage = index . 0 1}} {{end}}
{{deleteMessage $starboard $starboardMessage 0}}
{{dbSet 0 "starboardMessages" (reReplace $idRegex . "")}}
{{end}}

{{if $starboardData}}
{{$nil := $starboardData.Set "listID" (reReplace `SB\d+,` $starboardData.listID "")}}
{{$nil := $starboardData.Set "listAnti" (reReplace `SB\d+,` $starboardData.listAnti "")}}
{{$data.Set $sboardPost $starboardData}}
{{dbSet 0 "starboardReactions" $data}}
{{end}}

{{else if and $count (not $skip) (or (eq .Reaction.Emoji.Name $starEmoji) (eq .Reaction.Emoji.Name $antiStarEmoji)) (le (currentTime.Sub .Message.Timestamp.Parse) (toDuration $maxAge))}}

{{$emoji := "🌠"}}
{{if lt $count 5}} {{$emoji = "⭐"}}
{{else if lt $count 10}} {{$emoji = "🌟"}}
{{else if lt $count 15}} {{$emoji = "✨"}}
{{else if lt $count 20}} {{$emoji = "💫"}}
{{else if lt $count 30}} {{$emoji = "🎇"}}
{{else if lt $count 40}} {{$emoji = "🎆"}}
{{else if lt $count 50}} {{$emoji = "☄️"}}
{{end}}

{{$embed := structToSdict (index ((getMessage $starboard $starboardMessage).Embeds) 0)}}
{{range $k, $v := $embed}}
{{- if eq (kindOf $v true) "struct"}}
{{- $embed.Set $k (structToSdict $v)}}
{{- end -}}{{end}}
{{if $embed.Author}} {{$embed.Author.Set "Icon_URL" $embed.Author.IconURL}} {{end}}
{{if $embed.Footer}} {{$embed.Footer.Set "Icon_URL" $embed.Footer.IconURL}} {{end}}

{{$embed.Set "footer" (sdict "text" (printf "%s %d | %s" $emoji $count $thisID))}}
{{editMessage $starboard $starboardMessage (cembed $embed)}}
{{end}}

Author

This custom command was written by @dvoraknt.