Skip to content

Commit

Permalink
feat: add proposals command for discord
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 19, 2024
1 parent e8a9d61 commit 1a3bcd0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
version,
log,
stateManager,
stateGenerator,
timeZone,
tracer,
),

Check warning on line 85 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L77-L85

Added lines #L77 - L85 were not covered by tests
Expand Down
6 changes: 5 additions & 1 deletion pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Reporter struct {
Version string

DiscordSession *discordgo.Session
StateGenerator *statePkg.Generator
Logger zerolog.Logger
Config *types.Config
Manager *statePkg.Manager
Expand All @@ -38,6 +39,7 @@ func NewReporter(
version string,
logger *zerolog.Logger,
manager *statePkg.Manager,
stateGenerator *statePkg.Generator,
timezone *time.Location,
tracer trace.Tracer,
) *Reporter {
Expand All @@ -48,6 +50,7 @@ func NewReporter(
Config: config,
Logger: logger.With().Str("component", "discord_reporter").Logger(),
Manager: manager,
StateGenerator: stateGenerator,
TemplatesManager: templatesPkg.NewDiscordTemplatesManager(logger, timezone),
Commands: make(map[string]*Command, 0),
Version: version,
Expand Down Expand Up @@ -79,7 +82,8 @@ func (reporter *Reporter) Init() error {
reporter.Logger.Info().Err(err).Msg("Discord bot listening")

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

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L82

Added line #L82 was not covered by tests

reporter.Commands = map[string]*Command{
"help": reporter.GetHelpCommand(),
"help": reporter.GetHelpCommand(),
"proposals": reporter.GetProposalsCommand(),

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

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L84-L86

Added lines #L84 - L86 were not covered by tests
}

go reporter.InitCommands()
Expand Down
29 changes: 29 additions & 0 deletions pkg/reporters/discord/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package discord

import (
"context"
statePkg "main/pkg/state"

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetProposalsCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "proposals",
Description: "Get list of active proposals and your wallet's votes on them.",
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
state := reporter.StateGenerator.GetState(statePkg.NewState(), context.Background())
renderedState := state.ToRenderedState()

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

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/proposals.go#L10-L18

Added lines #L10 - L18 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

reporter.BotRespond(s, i, template)

Check warning on line 26 in pkg/reporters/discord/proposals.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/proposals.go#L26

Added line #L26 was not covered by tests
},
}
}
2 changes: 1 addition & 1 deletion templates/discord/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Notifies you about the proposals your wallets hasn't voted upon.
Can understand the following commands:
- /proposals - displays active proposals and your wallets' votes on them
- </proposals:{{ .Commands.proposals.Info.ID }}> - displays active proposals and your wallets' votes on them
- /proposals_mute &lt;duration&gt; &lt;chain&gt; &lt;proposal ID&gt; - mute notifications for a specific proposal
- /proposals_mutes - display the active proposals mutes list
- </help:{{ .Commands.help.Info.ID }}> - display this message
Expand Down
24 changes: 24 additions & 0 deletions templates/discord/proposals.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{- if not .ChainInfos }}
**No active proposals**
{{- end }}
{{- range .ChainInfos }}
{{- $chain := .Chain -}}
**{{ .Chain.GetName }}**
{{- if .HasProposalsError }}
❌ Error querying for proposals: {{ .ProposalsError }}
{{- end }}
{{- range .ProposalVotes }}
{{- $proposalLink := $chain.GetProposalLink .Proposal }}
Proposal #{{ .Proposal.ID }}: {{ SerializeLink $proposalLink }} (voting ends in {{ .Proposal.GetTimeLeft }})
{{- range $wallet, $vote := .Votes }}
{{- $walletLink := $chain.GetWalletLink $vote.Wallet -}}
{{- if $vote.IsError }}
❌ Wallet {{ SerializeLink $walletLink }} - error querying: {{ $vote.Error }}
{{- else if $vote.HasVoted }}
✅ Wallet {{ SerializeLink $walletLink }} - voted: {{ $vote.Vote.ResolveVote }}
{{- else }}
🔴 Wallet {{ SerializeLink $walletLink }} - not voted
{{- end }}
{{- end }}
{{ end }}
{{ end }}

0 comments on commit 1a3bcd0

Please sign in to comment.