Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: containerised Pin command #169

Merged
merged 13 commits into from
Jan 24, 2025
Prev Previous commit
Next Next commit
chore: Update elliotwms/bot
This PR updates the elliotwms/bot package, leveraging the package's ability to manage and route application commands, and perform health check

chore(refactor): Remove unused config
chore: remove redundant health check tests (handled in elliotwms/bot)
chore: update elliotwms/fakediscord
  • Loading branch information
elliotwms committed Jan 20, 2025
commit 6966fb09fcf50b4e98d8afdb318a107dfc4e0f42
10 changes: 7 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/bwmarrin/discordgo"
"github.com/elliotwms/bot"
"github.com/elliotwms/pinbot/internal/commandhandlers"
"github.com/elliotwms/pinbot/internal/commands"
"github.com/elliotwms/pinbot/internal/config"
"github.com/elliotwms/pinbot/internal/eventhandlers"
"github.com/sirupsen/logrus"
Expand All @@ -29,17 +31,19 @@ func main() {
}

b := bot.
New(config.ApplicationID, s, log).
New(config.ApplicationID, s).
WithIntents(config.Intents).
WithHandlers(eventhandlers.List(logrus.NewEntry(log))).
WithConfigReporter(config.Output)
WithMigrationEnabled(true).
WithApplicationCommand(commands.Pin, commandhandlers.PinMessageCommandHandler)

if config.HealthCheckAddr != "" {
b.WithHealthCheck(config.HealthCheckAddr)
}

ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

if err := b.Run(ctx); err != nil {
if err := b.Build().Run(ctx); err != nil {
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
fake-discord:
image: ghcr.io/elliotwms/fakediscord:v0.16.8
image: ghcr.io/elliotwms/fakediscord:v0.18.2
ports:
- 8080:8080
volumes:
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.23
require (
github.com/bwmarrin/discordgo v0.28.1
github.com/bwmarrin/snowflake v0.3.0
github.com/elliotwms/bot v0.3.28
github.com/elliotwms/fakediscord v0.16.4
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/elliotwms/bot v0.3.33
github.com/elliotwms/fakediscord v0.18.2
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
)

require (
Expand All @@ -18,8 +17,8 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotwms/bot v0.3.28 h1:GmkZRVzpCIUGikEDKKY4+d8R/hInXkD1GYkbrkDwBC4=
github.com/elliotwms/bot v0.3.28/go.mod h1:atWU8m/C6xAF5QE8CxVF8eXNdtoO/cE72gAAFYxpO4s=
github.com/elliotwms/fakediscord v0.16.4 h1:yHuiwdbLSbg4MbXzhcD9JxWMn9Fi2CDSheTVFT4irQo=
github.com/elliotwms/fakediscord v0.16.4/go.mod h1:uw9CFPmgeR1f2NoOdzv87gyzmQxAhPQX0WCyEFOhQKo=
github.com/elliotwms/bot v0.3.33 h1:McFeMu0fMssxDkkxTOxWpc97G7zAQKRsX3k9cnM3G7w=
github.com/elliotwms/bot v0.3.33/go.mod h1:2laSFmgz3PLUuSLxbwqAocRRsM02CgPbhOKCgJdHngw=
github.com/elliotwms/fakediscord v0.18.2 h1:EN1JeyhMMPpZf+UnZMvROjhuzsAM+z4nw9bVseakcjo=
github.com/elliotwms/fakediscord v0.18.2/go.mod h1:diPjfnMTjg73Izwl5pHMvGoGuOL8VSsMVt2ugFhfCos=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/neilotoole/slogt v1.1.0 h1:c7qE92sq+V0yvCuaxph+RQ2jOKL61c4hqS1Bv9W7FZE=
github.com/neilotoole/slogt v1.1.0/go.mod h1:RCrGXkPc/hYybNulqQrMHRtvlQ7F6NktNVLuLwk6V+w=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
Expand All @@ -27,16 +29,16 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
4 changes: 3 additions & 1 deletion internal/commandhandlers/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const (
pinMessageColor = 0xbb0303
)

func PinMessageCommandHandler(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, data discordgo.ApplicationCommandInteractionData) (err error) {
func PinMessageCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate, data discordgo.ApplicationCommandInteractionData) (err error) {
ctx := context.TODO()

m := data.Resolved.Messages[data.TargetID]
m.GuildID = i.GuildID // guildID is missing from message in resolved context

Expand Down
44 changes: 0 additions & 44 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@ package config

import (
"fmt"
"net/url"
"os"
"strconv"
"sync"

"github.com/bwmarrin/discordgo"
"github.com/elliotwms/pinbot/internal/build"
"github.com/sirupsen/logrus"
)

const DefaultIntents = discordgo.IntentsGuilds |
discordgo.IntentsGuildMessages |
discordgo.IntentsGuildMessageReactions

const DefaultPermissions = discordgo.PermissionViewChannel |
discordgo.PermissionSendMessages |
discordgo.PermissionAddReactions

var (
Token string
ApplicationID string
HealthCheckAddr string
LogLevel logrus.Level
Intents discordgo.Intent
Permissions int
)

var once sync.Once
Expand All @@ -50,50 +43,13 @@ func Configure() {
Intents = discordgo.Intent(i)
}
}

Permissions = DefaultPermissions
if s := os.Getenv("PERMISSIONS"); s != "" {
if i, err := strconv.Atoi(s); err == nil {
Permissions = i
}
}
})
}

func Output(showSensitive bool) logrus.Fields {
fields := logrus.Fields{
"APPLICATION_ID": ApplicationID,
"HEALTH_CHECK_ADDR": HealthCheckAddr,
"LOG_LEVEL": LogLevel,
"INTENTS": Intents,
"PERMISSIONS": Permissions,
"install_url": BuildInstallURL().String(),
"version": build.Version,
}

if showSensitive {
fields["TOKEN"] = Token
}

return fields
}

func mustGetEnv(s string) string {
token := os.Getenv(s)
if token == "" {
panic(fmt.Sprintf("Missing '%s'", s))
}
return token
}

func BuildInstallURL() *url.URL {
u, _ := url.Parse("https://discord.com/oauth2/authorize")

q := u.Query()
q.Add("client_id", ApplicationID)
q.Add("permissions", fmt.Sprintf("%d", Permissions))
q.Add("scope", "applications.commands bot")
u.RawQuery = q.Encode()

return u
}
1 change: 0 additions & 1 deletion internal/eventhandlers/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ const logFieldHandler = "handler"
func List(l *logrus.Entry) []interface{} {
return []interface{}{
Ready(l.WithField(logFieldHandler, "Ready")),
InteractionCreate(l.WithField(logFieldHandler, "InteractionCreate")),
}
}
40 changes: 0 additions & 40 deletions internal/eventhandlers/interaction_create.go

This file was deleted.

22 changes: 0 additions & 22 deletions internal/eventhandlers/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package eventhandlers
import (
"github.com/bwmarrin/discordgo"
"github.com/elliotwms/pinbot/internal/build"
"github.com/elliotwms/pinbot/internal/commands"
"github.com/elliotwms/pinbot/internal/config"
"github.com/sirupsen/logrus"
)

Expand All @@ -16,25 +14,5 @@ func Ready(log *logrus.Entry) func(s *discordgo.Session, _ *discordgo.Ready) {
log.WithError(err).Error("Could not update game status")
return
}

// check if Pin command exists, create if not
cs, err := s.ApplicationCommands(config.ApplicationID, "")
if err != nil {
log.WithError(err).Error("Could not get application commands")
return
}

for _, c := range cs {
if c.Name == commands.Pin.Name && c.Type == commands.Pin.Type {
log.Info("Pin command already exists")
return
}
}

log.Info("Creating Pin command")
_, err = s.ApplicationCommandCreate(config.ApplicationID, "", commands.Pin)
if err != nil {
log.WithError(err).Error("Could not create command")
}
}
}
63 changes: 0 additions & 63 deletions internal/tests/health_stage_test.go

This file was deleted.

14 changes: 0 additions & 14 deletions internal/tests/health_test.go

This file was deleted.

Loading
Loading