diff --git a/game/discord.go b/game/discord.go new file mode 100644 index 0000000..8b1b2f9 --- /dev/null +++ b/game/discord.go @@ -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 + } +} diff --git a/game/game.go b/game/game.go index 0b9486b..7cb74bd 100644 --- a/game/game.go +++ b/game/game.go @@ -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:"-"` @@ -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 @@ -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) } } diff --git a/game/phase.go b/game/phase.go index 7fd86a9..877f61a 100644 --- a/game/phase.go +++ b/game/phase.go @@ -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" @@ -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) } }