Skip to content

Commit

Permalink
feat: add tally command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 20, 2024
1 parent 8577dee commit 43ad844
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (reporter *Reporter) Init() error {
"proposals_unmute": reporter.GetDeleteMuteCommand(),
"proposals_mutes": reporter.GetMutesCommand(),
"params": reporter.GetParamsCommand(),
"tally": reporter.GetTallyCommand(),

Check warning on line 99 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L92-L99

Added lines #L92 - L99 were not covered by tests
}

go reporter.InitCommands()
Expand Down Expand Up @@ -236,24 +237,31 @@ func (reporter *Reporter) BotRespond(s *discordgo.Session, i *discordgo.Interact
chunks := utils.SplitStringIntoChunks(text, 2000)
firstChunk, rest := chunks[0], chunks[1:]

Check warning on line 238 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L236-L238

Added lines #L236 - L238 were not covered by tests

reporter.BotSendInteraction(s, i, firstChunk)

Check warning on line 240 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L240

Added line #L240 was not covered by tests

for _, chunk := range rest {
reporter.BotSendFollowup(s, i, chunk)

Check warning on line 243 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L242-L243

Added lines #L242 - L243 were not covered by tests
}
}

func (reporter *Reporter) BotSendInteraction(s *discordgo.Session, i *discordgo.InteractionCreate, text string) {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: firstChunk,
Content: text,
},
}); err != nil {
reporter.Logger.Error().Err(err).Msg("Error sending response")

Check warning on line 254 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L247-L254

Added lines #L247 - L254 were not covered by tests
}
}

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) BotSendFollowup(s *discordgo.Session, i *discordgo.InteractionCreate, text string) {
if _, err := s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
Content: text,
}); err != nil {
reporter.Logger.Error().
Err(err).
Msg("Error sending followup message")

Check warning on line 264 in pkg/reporters/discord/discord.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L258-L264

Added lines #L258 - L264 were not covered by tests
}
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/reporters/discord/tally.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package discord

import (
"context"
"fmt"
"main/pkg/utils"

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetTallyCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "tally",
Description: "Get active proposals' tallies",
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
reporter.BotSendInteraction(s, i, "Calculating tally for proposals. This might take a while...")

Check warning on line 18 in pkg/reporters/discord/tally.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/tally.go#L11-L18

Added lines #L11 - L18 were not covered by tests

tallies, err := reporter.DataManager.GetTallies(context.Background())
if err != nil {
reporter.BotRespond(s, i, fmt.Sprintf("Error getting tallies info: %s", err))
return

Check warning on line 23 in pkg/reporters/discord/tally.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/tally.go#L20-L23

Added lines #L20 - L23 were not covered by tests
}

template, err := reporter.TemplatesManager.Render("tally", tallies)
if err != nil {
reporter.Logger.Error().Err(err).Str("template", "tally").Msg("Error rendering template")
return

Check warning on line 29 in pkg/reporters/discord/tally.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/tally.go#L26-L29

Added lines #L26 - L29 were not covered by tests
}

chunks := utils.SplitStringIntoChunks(template, 2000)
for _, chunk := range chunks {
reporter.BotSendFollowup(s, i, chunk)

Check warning on line 34 in pkg/reporters/discord/tally.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/tally.go#L32-L34

Added lines #L32 - L34 were not covered by tests
}
},
}
}
1 change: 1 addition & 0 deletions templates/discord/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- </proposals_unmute:{{ .Commands.proposals_unmute.Info.ID }}> - unmute notifications for a specific chain or proposal
- </proposals_mutes:{{ .Commands.proposals_mutes.Info.ID }}> - displays the active mutes list
- </params:{{ .Commands.params.Info.ID }}> - list chains params
- </tally:{{ .Commands.tally.Info.ID }}> - list active proposals' tallies
- </help:{{ .Commands.help.Info.ID }}> - display this message

Created by [🐹 Quokka Stake](<https://quokkastake.io>) with ❤️.
Expand Down
18 changes: 18 additions & 0 deletions templates/discord/tally.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{- if not . }}
**No active proposals.**
{{- end }}
{{- range $chainName, $tallyInfos := . }}
{{- if $tallyInfos.TallyInfos }}
**Proposals on chain {{ $tallyInfos.Chain.GetName }}:**
{{ range $chainIndex, $tallyInfo := $tallyInfos.TallyInfos }}
{{- $proposalLink := $tallyInfos.Chain.GetProposalLink .Proposal }}
Proposal #{{ .Proposal.ID }}: {{ SerializeLink $proposalLink }}
Ends in: {{ .Proposal.GetTimeLeft }}
- Not voted: {{ .GetNotVoted }}
- Voted: {{ .GetQuorum }}
{{- range $tallyOptionIndex, $tallyOption := .Tally }}
- Voted "{{ $tallyOption.Option }}": {{ $tallyInfo.Tally.GetVoted $tallyOption }}
{{- end }}
{{ end }}
{{- end }}
{{ end }}
1 change: 1 addition & 0 deletions templates/telegram/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- /proposals_unmute [&lt;chain&gt; &lt;proposal ID&gt;] - unmute notifications for a specific chain/proposal
- /proposals_mutes - display the active proposals mutes list
- /params - list chains params
- /tally - list active proposals' tallies
- /help - display this command

Created by <a href="https://quokkastake.io">🐹 Quokka Stake</a> with ❤️.
Expand Down

0 comments on commit 43ad844

Please sign in to comment.