Skip to content

Commit

Permalink
feat: add discord command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jan 26, 2025
1 parent b561996 commit 40d9f45
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ params - See chain and config params
config - See chain and config params
jails - See latest jails and tombstones events
events - See latest events for a validator
jailscount - see jails count for each validator since the app was started
jailscount - See jails count for each validator since the app was started
```

Then add a Telegram config to your config file (see `config.example.toml` for reference).
Expand Down
1 change: 1 addition & 0 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (reporter *Reporter) Init() {
"notifiers": reporter.GetNotifiersCommand(),
"jails": reporter.GetJailsCommand(),
"events": reporter.GetValidatorEventsCommand(),
"jailscount": reporter.GetJailsCountCommand(),
}

for query := range reporter.Commands {
Expand Down
56 changes: 56 additions & 0 deletions pkg/reporters/discord/jails_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package discord

import (
"main/pkg/constants"

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetJailsCountCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "jailscount",
Description: "See jails count for each validator since the app was started",
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.DiscordReporterName, "jailscount")

snapshot, found := reporter.SnapshotManager.GetNewerSnapshot()
if !found {
reporter.Logger.Error().Msg("Error searching for historical events!")
reporter.BotRespond(s, i, "Error searching for jails count!")
return
}

jailsCount, err := reporter.Manager.FindAllJailsCount()
if err != nil {
reporter.Logger.Error().Err(err).Msg("Error searching for jails count")
reporter.BotRespond(s, i, "Error searching for jails count!")
return
}

jailsCountRendered := make([]renderedJailsCount, len(jailsCount))

for index, validatorJailsCount := range jailsCount {
validatorEntries := snapshot.Entries.ByValidatorAddresses([]string{validatorJailsCount.Validator})
if len(validatorEntries) == 0 {
reporter.BotRespond(s, i, "Validator is not found!")
return
}

jailsCountRendered[index] = renderedJailsCount{
ValidatorLink: reporter.Config.ExplorerConfig.GetValidatorLink(validatorEntries[0].Validator),
JailsCount: validatorJailsCount.JailsCount,
}
}

renderedTemplate, err := reporter.TemplatesManager.Render("JailsCount", jailsCountRendered)
if err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering template")
return
}

reporter.BotRespond(s, i, renderedTemplate)
},
}
}
5 changes: 5 additions & 0 deletions pkg/reporters/discord/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ type eventsRender struct {
ValidatorLink types.Link
Events []renderedHistoricalEvent
}

type renderedJailsCount struct {
ValidatorLink types.Link
JailsCount int
}
1 change: 1 addition & 0 deletions templates/discord/Help.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ The bot can understand the following commands:
- </notifiers:{{ .Commands.notifiers.Info.ID }}> - see notifiers for each validator
- </jails:{{ .Commands.jails.Info.ID }}> - see latest jails and tombstones events
- </events:{{ .Commands.events.Info.ID }}> [validator address] - see latest events for a validator
- </jailscount:{{ .Commands.jailscount.Info.ID }}> - see jails count for each validator since the app was started
8 changes: 8 additions & 0 deletions templates/discord/JailsCount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if . }}
**Validators jails count observed by the app:**
{{- range . }}
- {{ SerializeLink .ValidatorLink }}: {{ .JailsCount }}
{{- end }}
{{- else }}
Nobody has been jailed since the app launch.
{{- end }}

0 comments on commit 40d9f45

Please sign in to comment.