Skip to main content

Reaction Handler

This command handles reactions for version 2 of the giveaway package.

For more information about this particular version of the giveaway package, see this page.

Trigger

Type: Reaction
Additional options: Added + Removed Reactions

Configuration

See the giveaway package overview for more information about how to configure this command.

Code

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

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

{{/* Configurable values */}}
{{ $giveawayEmoji := `🎉` }}
{{/* End of configurable values */}}

{{ $data := sdict }}

{{/* if reaction emoji matches giveaway emoji and user reacting is not a bot , proceed */}}
{{ if and (eq .Reaction.Emoji.Name $giveawayEmoji) (not .User.Bot ) }}
{{/* fetching active giveaways data */}}
{{ with (dbGet 7777 "giveaway_active").Value }}{{ $data = sdict . }}{{ end }}
{{$giveawayData := $data.Get (joinStr "" .Reaction.ChannelID .Reaction.MessageID)}}

{{/* if current message is an active giveaway announcement message */}}
{{ if $giveawayData }}
{{ $giveawayData = sdict $giveawayData }}
{{/* Regex for the User ID */}}
{{$IDregex:=print .User.ID ","}}

{{/* if reaction was added increase count by 1 and add user ID to ID list */}}
{{if .ReactionAdded}}
{{$amount := 1}}
{{/* If user is somwhow already present in list, dont increase count but update position in list */}}
{{if reFind $IDregex $giveawayData.listID}}
{{$giveawayData.Set "listID" (reReplace $IDregex $giveawayData.listID "")}}
{{$amount = 0}}
{{end}}
{{$giveawayData.Set "listID" (print $giveawayData.listID $IDregex)}}
{{$giveawayData.Set "count" (add $giveawayData.count $amount)}}
{{else}}
{{/* if reaction was removed reduce count by 1 and remove user ID from ID list if user ID is present in list. Else do nothing. */}}
{{if reFind $IDregex $giveawayData.listID}}
{{$giveawayData.Set "listID" (reReplace $IDregex $giveawayData.listID "")}}
{{$giveawayData.Set "count" (add $giveawayData.count -1)}}
{{end}}
{{end}}

{{/* update active giveaway database entry */}}
{{ $data.Set (joinStr "" .Reaction.ChannelID .Reaction.MessageID) $giveawayData }}
{{ dbSet 7777 "giveaway_active" $data }}
{{ end }}
{{ end }}

Author

This custom command was written by @Satty9361.