Skip to content

Commit

Permalink
Merge pull request #196 from zond/johnpooch/add-webhooks-to-game
Browse files Browse the repository at this point in the history
Invoke webhooks on game start and phase start
  • Loading branch information
johnpooch authored Jun 17, 2024
2 parents 0808d8a + acffcef commit 1b1e1b7
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 10 deletions.
41 changes: 41 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
redirectURLKind = "RedirectURL"
superusersKind = "Superusers"
discordBotCredentialsKind = "DiscordBotCredentials"
discordBotTokenKind = "DiscordBotTokenKind"
prodKey = "prod"
)

Expand All @@ -75,6 +76,8 @@ var (
prodSuperusersLock = sync.RWMutex{}
prodDiscordBotCredentials *DiscordBotCredentials
prodDiscordBotCredentialsLock = sync.RWMutex{}
prodDiscordBotToken *DiscordBotToken
prodDiscordBotTokenLock = sync.RWMutex{}
router *mux.Router

RedirectURLResource *Resource
Expand All @@ -85,6 +88,10 @@ type DiscordBotCredentials struct {
Password string
}

type DiscordBotToken struct {
Token string
}

func getDiscordBotCredentialsKey(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, discordBotCredentialsKind, prodKey, 0, nil)
}
Expand Down Expand Up @@ -119,6 +126,40 @@ func getDiscordBotCredentials(ctx context.Context) (*DiscordBotCredentials, erro
return prodDiscordBotCredentials, nil
}

func getDiscordBotTokenKey(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, discordBotTokenKind, prodKey, 0, nil)
}

func SetDiscordBotToken(ctx context.Context, discordBotToken *DiscordBotToken) error {
return datastore.RunInTransaction(ctx, func(ctx context.Context) error {
currentDiscordBotToken := &DiscordBotToken{}
if err := datastore.Get(ctx, getDiscordBotTokenKey(ctx), currentDiscordBotToken); err == nil {
return HTTPErr{"DiscordBotToken already configured", http.StatusBadRequest}
}
if _, err := datastore.Put(ctx, getDiscordBotTokenKey(ctx), discordBotToken); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: false})
}

func GetDiscordBotToken(ctx context.Context) (*DiscordBotToken, error) {
prodDiscordBotTokenLock.RLock()
if prodDiscordBotToken != nil {
defer prodDiscordBotTokenLock.RUnlock()
return prodDiscordBotToken, nil
}
prodDiscordBotTokenLock.RUnlock()
prodDiscordBotTokenLock.Lock()
defer prodDiscordBotTokenLock.Unlock()
foundDiscordBotToken := &DiscordBotToken{}
if err := datastore.Get(ctx, getDiscordBotTokenKey(ctx), foundDiscordBotToken); err != nil {
return nil, err
}
prodDiscordBotToken = foundDiscordBotToken
return prodDiscordBotToken, nil
}

func init() {
RedirectURLResource = &Resource{
Delete: deleteRedirectURL,
Expand Down
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
}
}
36 changes: 26 additions & 10 deletions game/game.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/davecgh/go-spew/spew"
"github.com/zond/diplicity/auth"
"github.com/zond/godip"
Expand Down Expand Up @@ -424,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 @@ -498,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 @@ -1142,6 +1148,16 @@ func asyncStartGame(ctx context.Context, gameID *datastore.Key, host string) err
}
g.ID = gameID

discordSession, err := CreateDiscordSession(ctx)
if err != nil {
log.Warningf(ctx, "Error creating discord session", err)
} else {
err = g.InvokeGameStartedDiscordWebhook(discordSession)
if err != nil {
log.Errorf(ctx, "Error invoking game started discord webhook", err)
}
}

variant := variants.Variants[g.Variant]
if len(g.Members) != len(variant.Nations) {
log.Warningf(ctx, "Variant %v has %v nations, game %v has %v nations, someone must have dropped out before we got here?", g.Variant, len(variant.Nations), g.ID, len(g.Members))
Expand Down
6 changes: 6 additions & 0 deletions game/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ type configuration struct {
SendGrid *auth.SendGrid
Superusers *auth.Superusers
DiscordBotCredentials *auth.DiscordBotCredentials
DiscordBotToken *auth.DiscordBotToken
}

func handleConfigure(w ResponseWriter, r Request) error {
Expand Down Expand Up @@ -694,6 +695,11 @@ func handleConfigure(w ResponseWriter, r Request) error {
return err
}
}
if conf.DiscordBotToken != nil {
if err := auth.SetDiscordBotToken(ctx, conf.DiscordBotToken); err != nil {
return err
}
}
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions game/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ 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
}

discordSession, err := CreateDiscordSession(ctx)
if err != nil {
log.Warningf(ctx, "Error creating discord session", err)
} else {
err = g.InvokePhaseStartedDiscordWebhook(discordSession)
if err != nil {
log.Errorf(ctx, "Error invoking phase started discord webhook", err)
}
}

if len(origUids) == 0 {
log.Infof(ctx, "sendPhaseNotificationsToUsers(..., %q, %v, %v, %+v) *** NO UIDS ***", host, gameID, phaseOrdinal, origUids)
return nil
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ require (

require (
cloud.google.com/go v0.38.0 // indirect
github.com/bwmarrin/discordgo v0.28.1 // indirect
github.com/gogs/chardet v0.0.0-20150115103509-2404f7772561 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
github.com/kr/text v0.1.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdc
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cheggaaa/pb/v3 v3.0.8 h1:bC8oemdChbke2FHIIGy9mn4DPJ2caZYQnfbRqwmdCoA=
github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA=
Expand Down Expand Up @@ -59,6 +61,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -119,6 +123,7 @@ github.com/zond/replace v0.0.0-20180415193355-5a1dc330b27e/go.mod h1:J7mWP0y029F
go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
Expand Down

0 comments on commit 1b1e1b7

Please sign in to comment.