Skip to content

Commit

Permalink
feat: add mute command for discord
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 19, 2024
1 parent 1a3bcd0 commit 0b9a7d6
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 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,
mutesManager,
stateGenerator,
timeZone,
tracer,
Expand Down
82 changes: 82 additions & 0 deletions pkg/reporters/discord/add_mute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package discord

import (
"fmt"
mutes "main/pkg/mutes"
"time"

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetAddMuteCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "proposals_mute",
Description: "Mute proposals' notifications",
Options: []*discordgo.ApplicationCommandOption{

Check warning on line 16 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L11-L16

Added lines #L11 - L16 were not covered by tests
{
Type: discordgo.ApplicationCommandOptionString,
Name: "duration",
Description: "For how long to mute notifications",
Required: true,
},

Check warning on line 22 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L18-L22

Added lines #L18 - L22 were not covered by tests
{
Type: discordgo.ApplicationCommandOptionString,
Name: "chain",
Description: "Chain to mute notifications on",
Required: false,
},

Check warning on line 28 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L24-L28

Added lines #L24 - L28 were not covered by tests
{
Type: discordgo.ApplicationCommandOptionString,
Name: "proposal",
Description: "Proposal to mute notifications on",
Required: false,
},
},
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options

Check warning on line 38 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L30-L38

Added lines #L30 - L38 were not covered by tests

durationString, _ := options[0].Value.(string)
var chain string
var proposal string

Check warning on line 42 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L40-L42

Added lines #L40 - L42 were not covered by tests

_, opts := options[0], options[1:]

Check warning on line 44 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L44

Added line #L44 was not covered by tests

for _, opt := range opts {
if opt.Name == "chain" {
chain, _ = opt.Value.(string)

Check warning on line 48 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L46-L48

Added lines #L46 - L48 were not covered by tests
}
if opt.Name == "proposal" {
proposal, _ = opt.Value.(string)

Check warning on line 51 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}

duration, err := time.ParseDuration(durationString)
if err != nil {
reporter.BotRespond(s, i, "Invalid mute duration provided: %s")
return

Check warning on line 58 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L55-L58

Added lines #L55 - L58 were not covered by tests
}

mute := &mutes.Mute{
Chain: chain,
ProposalID: proposal,
Expires: time.Now().Add(duration),
Comment: fmt.Sprintf(
"Muted using cosmos-proposals-checker for %s",
duration,
),

Check warning on line 68 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L61-L68

Added lines #L61 - L68 were not covered by tests
}

reporter.MutesManager.AddMute(mute)

Check warning on line 71 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L71

Added line #L71 was not covered by tests

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

Check warning on line 76 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L73-L76

Added lines #L73 - L76 were not covered by tests
}

reporter.BotRespond(s, i, template)

Check warning on line 79 in pkg/reporters/discord/add_mute.go

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/add_mute.go#L79

Added line #L79 was not covered by tests
},
}
}
9 changes: 7 additions & 2 deletions pkg/reporters/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"context"
mutes "main/pkg/mutes"
"main/pkg/report/entry"
statePkg "main/pkg/state"
templatesPkg "main/pkg/templates"
Expand All @@ -28,6 +29,7 @@ type Reporter struct {
Logger zerolog.Logger
Config *types.Config
Manager *statePkg.Manager
MutesManager *mutes.Manager
TemplatesManager templatesPkg.Manager
Commands map[string]*Command
Tracer trace.Tracer
Expand All @@ -39,6 +41,7 @@ func NewReporter(
version string,
logger *zerolog.Logger,
manager *statePkg.Manager,
mutesManager *mutes.Manager,
stateGenerator *statePkg.Generator,
timezone *time.Location,
tracer trace.Tracer,
Expand All @@ -50,6 +53,7 @@ func NewReporter(
Config: config,
Logger: logger.With().Str("component", "discord_reporter").Logger(),
Manager: manager,
MutesManager: mutesManager,
StateGenerator: stateGenerator,
TemplatesManager: templatesPkg.NewDiscordTemplatesManager(logger, timezone),
Commands: make(map[string]*Command, 0),
Expand Down Expand Up @@ -82,8 +86,9 @@ func (reporter *Reporter) Init() error {
reporter.Logger.Info().Err(err).Msg("Discord bot listening")

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#L86

Added line #L86 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/reporters/discord/discord.go#L88-L91

Added lines #L88 - L91 were not covered by tests
}

go reporter.InitCommands()
Expand Down
2 changes: 1 addition & 1 deletion templates/discord/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Notifies you about the proposals your wallets hasn't voted upon.
Can understand the following commands:
- </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_mute:{{ .Commands.add_mute.Info.ID}}> - mute notifications for a specific proposal
- /proposals_mutes - display the active proposals mutes list
- </help:{{ .Commands.help.Info.ID }}> - display this message

Expand Down
12 changes: 12 additions & 0 deletions templates/discord/mute_added.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Created mute with the following params:
{{- if .Chain }}
**Chain:** {{ .Chain }}
{{- else }}
**Chain:** all chains
{{- end }}
{{- if .ProposalID }}
**Proposal ID:** {{ .ProposalID }}
{{- else }}
**Proposal ID:** all proposals
{{- end }}
**Expires: **{{ SerializeDate .Expires }}

0 comments on commit 0b9a7d6

Please sign in to comment.