Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add discord /missing proper command #59

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ linters:
- durationcheck
- gocyclo
- exhaustive
- mnd
24 changes: 19 additions & 5 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
statePkg "main/pkg/state"
templatesPkg "main/pkg/templates"
types "main/pkg/types"
"main/pkg/utils"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -82,6 +83,7 @@ func (reporter *Reporter) Init() {
reporter.Commands = map[string]*Command{
"params": reporter.GetParamsCommand(),
"missing": reporter.GetMissingCommand(),
"validators": reporter.GetValidatorsCommand(),
"subscribe": reporter.GetSubscribeCommand(),
"unsubscribe": reporter.GetUnsubscribeCommand(),
"status": reporter.GetStatusCommand(),
Expand Down Expand Up @@ -199,16 +201,28 @@ func (reporter *Reporter) Send(report *types.Report) error {
}

func (reporter *Reporter) BotRespond(s *discordgo.Session, i *discordgo.InteractionCreate, text string) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
chunks := utils.SplitStringIntoChunks(text, 2000)
firstChunk, rest := chunks[0], chunks[1:]

if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: text,
Content: firstChunk,
},
})

if err != nil {
}); err != nil {
reporter.Logger.Error().Err(err).Msg("Error sending response")
}

for index, chunk := range rest {
if _, err := s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
Content: chunk,
}); err != nil {
reporter.Logger.Error().
Int("chunk", index).
Err(err).
Msg("Error sending followup message")
}
}
}

func (reporter *Reporter) SerializeDate(date time.Time) string {
Expand Down
9 changes: 1 addition & 8 deletions pkg/reporters/discord/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ func (reporter *Reporter) GetHelpCommand() *Command {
return
}

if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: template,
},
}); err != nil {
reporter.Logger.Error().Err(err).Msg("Error sending help")
}
reporter.BotRespond(s, i, template)
},
}
}
13 changes: 1 addition & 12 deletions pkg/reporters/discord/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,7 @@ func (reporter *Reporter) GetMissingCommand() *Command {
return
}

chunks := utils.SplitStringIntoChunks(template, 2000)

for _, chunk := range chunks {
if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: chunk,
},
}); err != nil {
reporter.Logger.Error().Err(err).Msg("Error sending missing")
}
}
reporter.BotRespond(s, i, template)
},
}
}
9 changes: 1 addition & 8 deletions pkg/reporters/discord/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ func (reporter *Reporter) GetParamsCommand() *Command {
return
}

if err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: template,
},
}); err != nil {
reporter.Logger.Error().Err(err).Msg("Error sending params")
}
reporter.BotRespond(s, i, template)
},
}
}
67 changes: 67 additions & 0 deletions pkg/reporters/discord/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package discord

import (
"fmt"
"main/pkg/constants"
snapshotPkg "main/pkg/snapshot"
"main/pkg/utils"
"sort"

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetValidatorsCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "validators",
Description: "Get the list of all validators and their missing blocks",
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.TelegramReporterName, "validators")

snapshot, found := reporter.SnapshotManager.GetNewerSnapshot()
if !found {
reporter.Logger.Info().
Msg("No older snapshot on discord validators query!")
reporter.BotRespond(s, i, "Error getting validators list")
return
}

validatorEntries := snapshot.Entries.ToSlice()
activeValidatorsEntries := utils.Filter(validatorEntries, func(v snapshotPkg.Entry) bool {
return v.Validator.Active()
})

sort.Slice(activeValidatorsEntries, func(firstIndex, secondIndex int) bool {
first := activeValidatorsEntries[firstIndex]
second := activeValidatorsEntries[secondIndex]

return first.SignatureInfo.GetNotSigned() < second.SignatureInfo.GetNotSigned()
})

render := missingValidatorsRender{
Config: reporter.Config,
Validators: utils.Map(activeValidatorsEntries, func(v snapshotPkg.Entry) missingValidatorsEntry {
link := reporter.Config.ExplorerConfig.GetValidatorLink(v.Validator)
group, _, _ := reporter.Config.MissedBlocksGroups.GetGroup(v.SignatureInfo.GetNotSigned())
link.Text = fmt.Sprintf("%s %s", group.EmojiEnd, v.Validator.Moniker)

return missingValidatorsEntry{
Validator: v.Validator,
Link: link,
NotSigned: v.SignatureInfo.GetNotSigned(),
BlocksWindow: reporter.Config.BlocksWindow,
}
}),
}

template, err := reporter.TemplatesManager.Render("Validators", render)
if err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering missing")
return
}

reporter.BotRespond(s, i, template)
},
}
}
4 changes: 2 additions & 2 deletions templates/discord/Help.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The bot can understand the following commands:
- </subscribe:{{ .Commands.subscribe.Info.ID }}> [validator address] - subscribe to validator's notifications
- </unsubscribe:{{ .Commands.unsubscribe.Info.ID }}> [validator address] - unsubscribe from validator's notifications
- </status:{{ .Commands.status.Info.ID }}> - see the notification on validators you are subscribed to
- /missing - see the missed blocks counter of validators missing blocks
- /validators - see the missed blocks counter of all validators
- </missing:{{ .Commands.missing.Info.ID }}> - see the missed blocks counter of validators missing blocks
- </validators:{{ .Commands.validators.Info.ID }}> - see the missed blocks counter of all validators
- </params:{{ .Commands.params.Info.ID }}> - see the app config and chain params
- </notifiers:{{ .Commands.notifiers.Info.ID }}> - see notifiers for each validator
2 changes: 1 addition & 1 deletion templates/discord/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ You are subscribed to the following validators' updates on {{ .ChainConfig.GetNa
{{- else -}}
**{{ SerializeLink .Link }}** ({{ $render.FormatVotingPower . }}): {{ .SigningInfo.GetNotSigned }} missed blocks ({{ $render.FormatNotSignedPercent . }}%)
{{- end -}}
{{ end }}
{{ end }}
8 changes: 8 additions & 0 deletions templates/discord/Validators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if not .Validators }}
There are no active validators on {{ .Config.GetName }}!
{{- else }}
**Validators' status on {{ .Config.GetName }}:**
{{- end }}
{{ range .Validators -}}
**{{ SerializeLink .Link }}**: {{ .NotSigned }} missed blocks ({{ .FormatMissed }}%)
{{ end }}
Loading