Skip to content

Commit

Permalink
Refactor discord webook functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 17, 2024
1 parent 33a9c64 commit acffcef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
35 changes: 35 additions & 0 deletions game/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package game

import (
"context"

"github.com/bwmarrin/discordgo"
"github.com/zond/diplicity/auth"
"google.golang.org/appengine/v2/log"
)

type DiscordWebhook struct {
Id string
Token string
}

type DiscordWebhooks struct {
GameStarted DiscordWebhook
PhaseStarted DiscordWebhook
}

func CreateDiscordSession(ctx context.Context) (*discordgo.Session, error) {
log.Infof(ctx, "Creating Discord session")
discordBotToken, err := auth.GetDiscordBotToken(ctx)
if err != nil {
log.Warningf(ctx, "Error getting Discord bot token", err)
return nil, err
} else {
discordSession, err := discordgo.New("Bot " + discordBotToken.Token)
if err != nil {
log.Errorf(ctx, "Error creating Discord session", err)
return nil, err
}
return discordSession, nil
}
}
46 changes: 20 additions & 26 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,6 @@ func (g Games) Item(r Request, user *auth.User, cursor *datastore.Cursor, limit
return gamesItem
}

type DiscordWebhook struct {
Id string
Token string
}

type DiscordWebhooks struct {
GameStarted DiscordWebhook
PhaseStarted DiscordWebhook
}

type Game struct {
ID *datastore.Key `datastore:"-"`

Expand Down Expand Up @@ -499,6 +489,21 @@ func (g *Game) Load(props []datastore.Property) error {
return err
}

func (g *Game) invokeWebhook(session *discordgo.Session, webhook DiscordWebhook, content string) error {
_, err := session.WebhookExecute(webhook.Id, webhook.Token, false, &discordgo.WebhookParams{
Content: content,
})
return err
}

func (g *Game) InvokePhaseStartedDiscordWebhook(session *discordgo.Session) error {
return g.invokeWebhook(session, g.DiscordWebhooks.PhaseStarted, "Phase has started!")
}

func (g *Game) InvokeGameStartedDiscordWebhook(session *discordgo.Session) error {
return g.invokeWebhook(session, g.DiscordWebhooks.GameStarted, "Game has started!")
}

func (g *Game) canMergeInto(o *Game, avoid *auth.User) bool {
if g.NoMerge || o.NoMerge {
return false
Expand Down Expand Up @@ -1143,24 +1148,13 @@ func asyncStartGame(ctx context.Context, gameID *datastore.Key, host string) err
}
g.ID = gameID

discordBotToken, err := auth.GetDiscordBotToken(ctx)
discordSession, err := CreateDiscordSession(ctx)
if err != nil {
log.Warningf(ctx, "auth.GetDiscordBotToken(...): %v", err)
log.Warningf(ctx, "Error creating discord session", err)
} else {
gameStartedWebhook := g.DiscordWebhooks.GameStarted
// Invoke webhook using discordgo
if gameStartedWebhook.Id != "" && gameStartedWebhook.Token != "" {
discordSession, err := discordgo.New("Bot " + discordBotToken.Token)
if err != nil {
log.Errorf(ctx, "discordgo.New(...): %v", err)
return err
}
if _, err := discordSession.WebhookExecute(gameStartedWebhook.Id, gameStartedWebhook.Token, false, &discordgo.WebhookParams{
Content: fmt.Sprintf("Game %v has started!", g.Desc),
}); err != nil {
log.Errorf(ctx, "discordSession.WebhookExecute(...): %v", err)
return err
}
err = g.InvokeGameStartedDiscordWebhook(discordSession)
if err != nil {
log.Errorf(ctx, "Error invoking game started discord webhook", err)
}
}

Expand Down
22 changes: 5 additions & 17 deletions game/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"time"

"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize/english"
"github.com/zond/diplicity/auth"
"github.com/zond/godip"
Expand Down Expand Up @@ -402,24 +401,13 @@ func sendPhaseNotificationsToUsers(ctx context.Context, host string, gameID *dat
return err
}

discordBotToken, err := auth.GetDiscordBotToken(ctx)
discordSession, err := CreateDiscordSession(ctx)
if err != nil {
log.Warningf(ctx, "auth.GetDiscordBotToken(...): %v", err)
log.Warningf(ctx, "Error creating discord session", err)
} else {
phaseStartedWebhook := g.DiscordWebhooks.PhaseStarted
// Invoke webhook using discordgo
if phaseStartedWebhook.Id != "" && phaseStartedWebhook.Token != "" {
discordSession, err := discordgo.New("Bot " + discordBotToken.Token)
if err != nil {
log.Errorf(ctx, "discordgo.New(...): %v", err)
return err
}
if _, err := discordSession.WebhookExecute(phaseStartedWebhook.Id, phaseStartedWebhook.Token, false, &discordgo.WebhookParams{
Content: fmt.Sprintf("Phase %v has started!", phaseOrdinal),
}); err != nil {
log.Errorf(ctx, "discordSession.WebhookExecute(...): %v", err)
return err
}
err = g.InvokePhaseStartedDiscordWebhook(discordSession)
if err != nil {
log.Errorf(ctx, "Error invoking phase started discord webhook", err)
}
}

Expand Down

0 comments on commit acffcef

Please sign in to comment.