Skip to content

Commit

Permalink
Invoke webhooks on game start and phase start
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 14, 2024
1 parent 8b62594 commit 34da477
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
31 changes: 18 additions & 13 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,19 +1143,24 @@ func asyncStartGame(ctx context.Context, gameID *datastore.Key, host string) err
}
g.ID = gameID

gameStartedWebhook := g.DiscordWebhooks.GameStarted
// Invoke webhook using discordgo
if gameStartedWebhook.Id != "" && gameStartedWebhook.Token != "" {
discordSession, err := discordgo.New("Bot " + host)
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
discordBotToken, err := auth.GetDiscordBotToken(ctx)
if err != nil {
log.Warningf(ctx, "auth.GetDiscordBotToken(...): %v", 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
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions game/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 @@ -395,6 +396,33 @@ func sendPhaseNotificationsToFCM(ctx context.Context, host string, gameID *datas
func sendPhaseNotificationsToUsers(ctx context.Context, host string, gameID *datastore.Key, phaseOrdinal int64, origUids []string) error {
log.Infof(ctx, "sendPhaseNotificationsToUsers(..., %q, %v, %v, %+v)", host, gameID, phaseOrdinal, origUids)

g := &Game{}
if err := datastore.Get(ctx, gameID, g); err != nil {
log.Errorf(ctx, "datastore.Get(..., %v, %v): %v; hope datastore will get fixed", gameID, g, err)
return err
}

discordBotToken, err := auth.GetDiscordBotToken(ctx)
if err != nil {
log.Warningf(ctx, "auth.GetDiscordBotToken(...): %v", 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
}
}
}

if len(origUids) == 0 {
log.Infof(ctx, "sendPhaseNotificationsToUsers(..., %q, %v, %v, %+v) *** NO UIDS ***", host, gameID, phaseOrdinal, origUids)
return nil
Expand Down

0 comments on commit 34da477

Please sign in to comment.