Skip to content

Commit

Permalink
feat: add discord reporter (#95)
Browse files Browse the repository at this point in the history
* feat: add discord reporter

* chore: add tests for SplitStringIntoChunks

* feat: add proposals command for discord

* feat: add mute command for discord

* feat: delete mute command for discord

* feat: list mutes command for discord

* feat: add params command for discord + updating commands

* feat: add tally command
  • Loading branch information
freak12techno committed May 20, 2024
1 parent 5cb4556 commit 698f895
Show file tree
Hide file tree
Showing 30 changed files with 1,027 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.22.1
require (
cosmossdk.io/math v1.0.1
github.com/BurntSushi/toml v1.1.0
github.com/bwmarrin/discordgo v0.28.1
github.com/creasty/defaults v1.7.0
github.com/guregu/null/v5 v5.0.0
github.com/robfig/cron/v3 v3.0.1
Expand All @@ -27,13 +28,15 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.19.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cosmossdk.io/math v1.0.1 h1:Qx3ifyOPaMLNH/89WeZFH268yCvU4xEcnPLu3sJqPPg=
cosmossdk.io/math v1.0.1/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
Expand All @@ -28,6 +30,8 @@ github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3K
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/guregu/null/v5 v5.0.0 h1:PRxjqyOekS11W+w/7Vfz6jgJE/BCwELWtgvOJzddimw=
Expand Down Expand Up @@ -86,7 +90,10 @@ go.opentelemetry.io/proto/otlp v1.2.0/go.mod h1:gGpR8txAl5M03pDhMC79G6SdqNV26naR
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
12 changes: 12 additions & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
mutes "main/pkg/mutes"
"main/pkg/report"
reportersPkg "main/pkg/reporters"
"main/pkg/reporters/discord"
"main/pkg/reporters/pagerduty"
"main/pkg/reporters/telegram"
"main/pkg/state"
Expand Down Expand Up @@ -73,6 +74,17 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
timeZone,
tracer,
),
discord.NewReporter(
config,
version,
log,
stateManager,
mutesManager,
dataManager,
stateGenerator,
timeZone,
tracer,
),
}

reportDispatcher := report.NewDispatcher(log, mutesManager, reporters, 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{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "duration",
Description: "For how long to mute notifications",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "chain",
Description: "Chain to mute notifications on",
Required: false,
},
{
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

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

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

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

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

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

reporter.MutesManager.AddMute(mute)

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
}

reporter.BotRespond(s, i, template)
},
}
}
66 changes: 66 additions & 0 deletions pkg/reporters/discord/delete_mute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package discord

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

"github.com/bwmarrin/discordgo"
)

func (reporter *Reporter) GetDeleteMuteCommand() *Command {
return &Command{
Info: &discordgo.ApplicationCommand{
Name: "proposals_unmute",
Description: "Unmute proposals' notifications",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "chain",
Description: "Chain to mute notifications on",
Required: false,
},
{
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

var chain string
var proposal string

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

mute := &mutes.Mute{
Chain: chain,
ProposalID: proposal,
Expires: time.Now(),
Comment: "",
}

if found := reporter.MutesManager.DeleteMute(mute); !found {
reporter.BotRespond(s, i, "Could not find the mute to delete!")
return
}

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

reporter.BotRespond(s, i, template)
},
}
}
Loading

0 comments on commit 698f895

Please sign in to comment.