Skip to content

Commit

Permalink
Rich presence (#46)
Browse files Browse the repository at this point in the history
Closes #37
  • Loading branch information
NiJeTi authored Jan 2, 2025
1 parent 81f01bc commit f26510b
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ linters-settings:
allowStrs: "\"\""
allowInts: "-1,0,1,2"
allowFloats: "-1.0,-1.,0.0,0.,1.0,1.,2.0,2."
ignoreFuncs: "os\\.*,fmt\\.Println,make"
ignoreFuncs: "os\\.*,fmt\\.Println,make,logger\\.*"
- name: argument-limit
disabled: true
- name: banned-characters
Expand Down
8 changes: 8 additions & 0 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/nijeti/cinema-keeper/internal/services/listQuotes"
"github.com/nijeti/cinema-keeper/internal/services/lockVoiceChan"
"github.com/nijeti/cinema-keeper/internal/services/mentionVoiceChan"
"github.com/nijeti/cinema-keeper/internal/services/presence"
"github.com/nijeti/cinema-keeper/internal/services/unlockVoiceChan"
)

Expand Down Expand Up @@ -103,6 +104,7 @@ func run() (code int) {
listQuotesSvc := listQuotes.New(dcAdapter, quotesRepo)
lockVoiceChanSvc := lockVoiceChan.New(dcAdapter)
mentionVoiceChanSvc := mentionVoiceChan.New(dcAdapter)
presenceSvc := presence.New(dcAdapter)
rollSvc := diceRoll.New(dcAdapter)
unlockVoiceChanSvc := unlockVoiceChan.New(dcAdapter)

Expand Down Expand Up @@ -154,6 +156,12 @@ func run() (code int) {

// run
logger.Info("startup complete")

err = presenceSvc.Set(ctx)
if err != nil {
logger.ErrorContext(ctx, "failed to set presence", "error", err)
}

<-ctx.Done()

// shutdown
Expand Down
16 changes: 16 additions & 0 deletions internal/adapters/discord/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ func (a *Adapter) ChannelUnsetUserLimit(

return nil
}

func (a *Adapter) SetActivity(
_ context.Context, activity *discordgo.Activity,
) error {
err := a.session.UpdateStatusComplex(
discordgo.UpdateStatusData{
Status: string(discordgo.StatusOnline),
Activities: []*discordgo.Activity{activity},
},
)
if err != nil {
return fmt.Errorf("failed to update status: %w", err)
}

return nil
}
7 changes: 7 additions & 0 deletions internal/discord/commands/responses/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"github.com/bwmarrin/discordgo"
)

func Activity() *discordgo.Activity {
return &discordgo.Activity{
Name: "Collecting movies and quotes",
Type: discordgo.ActivityTypeCustom,
}
}

func UserNotInVoiceChannel() *discordgo.InteractionResponse {
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Expand Down
84 changes: 84 additions & 0 deletions internal/generated/mocks/services/presence/discord.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/services/presence/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package presence

import (
"context"

"github.com/bwmarrin/discordgo"
)

type discord interface {
SetActivity(ctx context.Context, activity *discordgo.Activity) error
}
29 changes: 29 additions & 0 deletions internal/services/presence/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package presence

import (
"context"
"fmt"

"github.com/nijeti/cinema-keeper/internal/discord/commands/responses"
)

type Service struct {
discord discord
}

func New(
discord discord,
) *Service {
return &Service{
discord: discord,
}
}

func (s *Service) Set(ctx context.Context) error {
err := s.discord.SetActivity(ctx, responses.Activity())
if err != nil {
return fmt.Errorf("failed to set activity: %w", err)
}

return nil
}
57 changes: 57 additions & 0 deletions internal/services/presence/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package presence_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"

"github.com/nijeti/cinema-keeper/internal/discord/commands/responses"
mocks "github.com/nijeti/cinema-keeper/internal/generated/mocks/services/presence"
"github.com/nijeti/cinema-keeper/internal/services/presence"
)

func TestService_Set(t *testing.T) {
t.Parallel()

ctx := context.Background()

type setup func(t *testing.T, err error) *presence.Service

tests := map[string]struct {
err error
setup setup
}{
"set_activity_error": {
err: errors.New("set activity error"),
setup: func(t *testing.T, err error) *presence.Service {
d := mocks.NewMockDiscord(t)

d.EXPECT().SetActivity(ctx, responses.Activity()).Return(err)

return presence.New(d)
},
},
"success": {
err: nil,
setup: func(t *testing.T, _ error) *presence.Service {
d := mocks.NewMockDiscord(t)

d.EXPECT().SetActivity(ctx, responses.Activity()).Return(nil)

return presence.New(d)
},
},
}

for name, tt := range tests {
t.Run(
name, func(t *testing.T) {
s := tt.setup(t, tt.err)
err := s.Set(ctx)
assert.ErrorIs(t, err, tt.err)
},
)
}
}

0 comments on commit f26510b

Please sign in to comment.