From 4023a58f6706e875ec2c49add80a15cb6883eb53 Mon Sep 17 00:00:00 2001 From: samhza Date: Thu, 9 Sep 2021 21:45:08 -0400 Subject: [PATCH] gateway: Change type of IdentifyData.Intents to option.Uint (#275) --- bot/extras/middlewares/middlewares_test.go | 5 +++-- gateway/gateway.go | 11 ++++++++--- gateway/identify.go | 11 ++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bot/extras/middlewares/middlewares_test.go b/bot/extras/middlewares/middlewares_test.go index 046b6ad..3acdd7c 100644 --- a/bot/extras/middlewares/middlewares_test.go +++ b/bot/extras/middlewares/middlewares_test.go @@ -10,6 +10,7 @@ import ( "github.com/diamondburned/arikawa/v3/session" "github.com/diamondburned/arikawa/v3/state" "github.com/diamondburned/arikawa/v3/state/store" + "github.com/diamondburned/arikawa/v3/utils/json/option" ) func TestAdminOnly(t *testing.T) { @@ -19,7 +20,7 @@ func TestAdminOnly(t *testing.T) { Gateway: &gateway.Gateway{ Identifier: &gateway.Identifier{ IdentifyData: gateway.IdentifyData{ - Intents: gateway.IntentGuilds | gateway.IntentGuildMembers, + Intents: option.NewUint(uint(gateway.IntentGuilds | gateway.IntentGuildMembers)), }, }, }, @@ -65,7 +66,7 @@ func TestGuildOnly(t *testing.T) { Gateway: &gateway.Gateway{ Identifier: &gateway.Identifier{ IdentifyData: gateway.IdentifyData{ - Intents: gateway.IntentGuilds, + Intents: option.NewUint(uint(gateway.IntentGuilds)), }, }, }, diff --git a/gateway/gateway.go b/gateway/gateway.go index e43ebe1..f3db18a 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -17,6 +17,7 @@ import ( "github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/internal/moreatomic" "github.com/diamondburned/arikawa/v3/utils/json" + "github.com/diamondburned/arikawa/v3/utils/json/option" "github.com/diamondburned/arikawa/v3/utils/wsutil" "github.com/gorilla/websocket" "github.com/pkg/errors" @@ -197,7 +198,11 @@ func NewCustomIdentifiedGateway(gatewayURL string, id *Identifier) *Gateway { // AddIntents adds a Gateway Intent before connecting to the Gateway. As such, // this function will only work before Open() is called. func (g *Gateway) AddIntents(i Intents) { - g.Identifier.Intents |= i + if g.Identifier.Intents == nil { + g.Identifier.Intents = option.NewUint(uint(i)) + } else { + *g.Identifier.Intents |= uint(i) + } } // HasIntents reports if the Gateway has the passed Intents. @@ -205,11 +210,11 @@ func (g *Gateway) AddIntents(i Intents) { // If no intents are set, i.e. if using a user account HasIntents will always // return true. func (g *Gateway) HasIntents(intents Intents) bool { - if g.Identifier.Intents == 0 { + if g.Identifier.Intents == nil { return true } - return g.Identifier.Intents.Has(intents) + return Intents(*g.Identifier.Intents).Has(intents) } // Close closes the underlying Websocket connection, invalidating the session diff --git a/gateway/identify.go b/gateway/identify.go index 362353b..6ac4f30 100644 --- a/gateway/identify.go +++ b/gateway/identify.go @@ -5,6 +5,7 @@ import ( "runtime" "time" + "github.com/diamondburned/arikawa/v3/utils/json/option" "github.com/pkg/errors" "golang.org/x/time/rate" ) @@ -76,7 +77,15 @@ type IdentifyData struct { Presence *UpdateStatusData `json:"presence,omitempty"` - Intents Intents `json:"intents,omitempty"` + // Intents specifies which groups of events the gateway + // connection will receive. + // + // For user accounts, it must be nil. + // + // For bot accounts, it must not be nil, and + // Gateway.AddIntents(0) can be used if you want to + // specify no intents. + Intents option.Uint `json:"intents"` } // DefaultIdentifyData creates a default IdentifyData with the given token.