diff --git a/endpoints.go b/endpoints.go index 6c33ef0..0ecdf0b 100644 --- a/endpoints.go +++ b/endpoints.go @@ -18,13 +18,14 @@ var ( EndpointSmActive = EndpointSm + "active.json" EndpointSmUpcoming = EndpointSm + "upcoming.json" - EndpointDiscord = "https://discordapp.com/" - EndpointAPI = EndpointDiscord + "api/" - EndpointGuilds = EndpointAPI + "guilds/" - EndpointChannels = EndpointAPI + "channels/" - EndpointUsers = EndpointAPI + "users/" - EndpointGateway = EndpointAPI + "gateway" - EndpointWebhooks = EndpointAPI + "webhooks/" + EndpointDiscord = "https://discordapp.com/" + EndpointAPI = EndpointDiscord + "api/" + EndpointGuilds = EndpointAPI + "guilds/" + EndpointChannels = EndpointAPI + "channels/" + EndpointUsers = EndpointAPI + "users/" + EndpointGateway = EndpointAPI + "gateway" + EndpointGatewayBot = EndpointGateway + "/bot" + EndpointWebhooks = EndpointAPI + "webhooks/" EndpointCDN = "https://cdn.discordapp.com/" EndpointCDNAttachments = EndpointCDN + "attachments/" diff --git a/restapi.go b/restapi.go index fe10222..46844d3 100644 --- a/restapi.go +++ b/restapi.go @@ -1716,6 +1716,28 @@ func (s *Session) Gateway() (gateway string, err error) { return } +// Gateway returns the websocket Gateway address and the reccomended number of shards +func (s *Session) GatewayBot() (st *GatewayBotResponse, err error) { + + response, err := s.RequestWithBucketID("GET", EndpointGatewayBot, nil, EndpointGatewayBot) + if err != nil { + return + } + + err = unmarshal(response, &st) + if err != nil { + return + } + + // Ensure the gateway always has a trailing slash. + // MacOS will fail to connect if we add query params without a trailing slash on the base domain. + if !strings.HasSuffix(st.URL, "/") { + st.URL += "/" + } + + return +} + // Functions specific to Webhooks // WebhookCreate returns a new Webhook. diff --git a/restapi_test.go b/restapi_test.go index a5d326b..cf64612 100644 --- a/restapi_test.go +++ b/restapi_test.go @@ -166,6 +166,17 @@ func TestGateway(t *testing.T) { } } +func TestGatewayBot(t *testing.T) { + + if dg == nil { + t.Skip("Skipping, dg not set.") + } + _, err := dg.GatewayBot() + if err != nil { + t.Errorf("GatewayBot() returned error: %+v", err) + } +} + func TestVoiceICE(t *testing.T) { if dg == nil { diff --git a/structs.go b/structs.go index ad081be..9697fa5 100644 --- a/structs.go +++ b/structs.go @@ -512,6 +512,12 @@ type MessageReaction struct { ChannelID string `json:"channel_id"` } +// GatewayBotResponse stores the data for the gateway/bot response +type GatewayBotResponse struct { + URL string `json:"url"` + Shards int `json:"shards"` +} + // Constants for the different bit offsets of text channel permissions const ( PermissionReadMessages = 1 << (iota + 10)