twilight-rs / twilight

Powerful, flexible, and scalable ecosystem of Rust libraries for the Discord API.
https://discord.gg/twilight-rs
ISC License
689 stars 130 forks source link

Using `GatewayIntents::GUILD_PRESENCES` causes connection failures #151

Closed Cldfire closed 4 years ago

Cldfire commented 4 years ago

EDIT: If you're coming here in the future, this was a permissions issue. Read the discussion below.

I've been using the following intent configuration in my code, with no issues:

.intents(Some(GatewayIntents::GUILD_MESSAGES))

I just changed it to the following in order to be able to log when the bot connects to guilds (Event::GuildCreate):

.intents(Some(GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILD_PRESENCES))

Unfortunately that change caused the library to start failing to connect. Log output:

[04-23-2020][11:43:07 PM][twilight_http::client][DEBUG] URL: "https://discordapp.com/api/v6/gateway/bot"
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting][DEBUG] Getting bucket for path: GatewayBot
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting][DEBUG] Making new bucket for path: GatewayBot
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Starting background queue task
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Starting to get next in queue
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Starting to wait for headers from response
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Updating bucket
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Starting to get next in queue
[04-23-2020][11:43:07 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [ShardProcessor [0, 1]] Queueing
[04-23-2020][11:43:07 PM][twilight_gateway::queue][INFO] Waiting for allowance!
[04-23-2020][11:43:07 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [ShardProcessor [0, 1]] Finished queue
[04-23-2020][11:43:07 PM][twilight_http::client][DEBUG] URL: "https://discordapp.com/api/v6/gateway"
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting][DEBUG] Getting bucket for path: Gateway
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting][DEBUG] Making new bucket for path: Gateway
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket Gateway] Starting background queue task
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket Gateway] Starting to get next in queue
[04-23-2020][11:43:07 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket Gateway] Starting to wait for headers from response
[04-23-2020][11:43:08 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket Gateway] Starting to get next in queue
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::connect][DEBUG] Shook hands with remote
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::socket_forwarder][DEBUG] [SocketForwarder] Starting driving loop
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 111:118
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (94.07%)
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] Hello(41250)
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::socket_forwarder][TRACE] [SocketForwarder] Sending msg: {"d":{"compression":false,"guild_subscriptions":true,"intents":768,"large_threshold":250,"presence":null,"properties":{"$browser":"twilight.rs","$device":"twilight.rs","$os":"linux","$referrer":"","$referring_domain":""},"shard":[0,1],"token":"Bot <removed>","v":6},"op":2}
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Got None, closing tx
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Leaving loop
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::r#impl][INFO] [resume] Resume started!
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::r#impl][WARN] Was not able to get the id, reconnecting.
[04-23-2020][11:43:08 PM][twilight_gateway::shard::processor::r#impl][INFO] [reconnect] Reconnection started!
[04-23-2020][11:43:08 PM][twilight_gateway::queue][INFO] Waiting for allowance!
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::connect][DEBUG] Shook hands with remote
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::socket_forwarder][DEBUG] [SocketForwarder] Starting driving loop
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 111:118
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (94.07%)
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] Hello(41250)
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::socket_forwarder][TRACE] [SocketForwarder] Sending msg: {"d":{"compression":false,"guild_subscriptions":true,"intents":768,"large_threshold":250,"presence":null,"properties":{"$browser":"twilight.rs","$device":"twilight.rs","$os":"linux","$referrer":"","$referring_domain":""},"shard":[0,1],"token":"Bot <removed>","v":6},"op":2}
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 19:36
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (84.42%)
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] InvalidateSession(false)
[04-23-2020][11:43:13 PM][twilight_gateway::shard::processor::r#impl][INFO] [reconnect] Reconnection started!
[04-23-2020][11:43:13 PM][twilight_gateway::queue][INFO] Waiting for allowance!
[04-23-2020][11:43:17 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket GatewayBot] Bucket appears finished, removing
[04-23-2020][11:43:18 PM][twilight_http::ratelimiting::bucket][DEBUG] [Bucket Gateway] Bucket appears finished, removing
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::connect][DEBUG] Shook hands with remote
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][DEBUG] [SocketForwarder] Starting driving loop
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 111:118
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (94.07%)
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] Hello(41250)
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Got None, closing stream
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Leaving loop
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][TRACE] [SocketForwarder] Sending msg: {"d":{"compression":false,"guild_subscriptions":true,"intents":768,"large_threshold":250,"presence":null,"properties":{"$browser":"twilight.rs","$device":"twilight.rs","$os":"linux","$referrer":"","$referring_domain":""},"shard":[0,1],"token":"Bot <removed>","v":6},"op":2}
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Got None, closing tx
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::socket_forwarder][WARN] [SocketForwarder] Leaving loop
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::r#impl][INFO] [resume] Resume started!
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::r#impl][WARN] Was not able to get the id, reconnecting.
[04-23-2020][11:43:18 PM][twilight_gateway::shard::processor::r#impl][INFO] [reconnect] Reconnection started!
[04-23-2020][11:43:18 PM][twilight_gateway::queue][INFO] Waiting for allowance!
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::connect][DEBUG] Shook hands with remote
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::socket_forwarder][DEBUG] [SocketForwarder] Starting driving loop
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 110:118
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (93.22%)
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] Hello(41250)
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::socket_forwarder][TRACE] [SocketForwarder] Sending msg: {"d":{"compression":false,"guild_subscriptions":true,"intents":768,"large_threshold":250,"presence":null,"properties":{"$browser":"twilight.rs","$device":"twilight.rs","$os":"linux","$referrer":"","$referring_domain":""},"shard":[0,1],"token":"Bot <removed>","v":6},"op":2}
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] in:out: 19:36
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] Data saved: 0KiB (83.77%)
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::inflater][TRACE] Capacity: 32768
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::r#impl][DEBUG] [EVENT] InvalidateSession(false)
[04-23-2020][11:43:23 PM][twilight_gateway::shard::processor::r#impl][INFO] [reconnect] Reconnection started!
[04-23-2020][11:43:23 PM][twilight_gateway::queue][INFO] Waiting for allowance!
Erk- commented 4 years ago

Just to make sure, this is happening when you do it with the shard or the cluster construct? And do you have access to the precense intent enabled for the bot?

dlee13 commented 4 years ago

https://discordapp.com/developers/docs/topics/gateway#gateway-intents

First, if I understand correctly, I think you need the GUILDS intent to get Event::GuildCreate. Second, GUILD_PRESENCES is a privileged intent.

Cldfire commented 4 years ago

Just to make sure, this is happening when you do it with the shard or the cluster construct?

This is on the cluster.

And do you have access to the precense intent enabled for the bot?

I do not! I didn't realize this intent required special permissions, my bad.

https://discordapp.com/developers/docs/topics/gateway#gateway-intents

First, if I understand correctly, I think you need the GUILDS intent to get Event::GuildCreate. Second, GUILD_PRESENCES is a privileged intent.

Yep, you're right. I misread the docs. Thanks!

It would be nice to handle permissions issues and hand some errors back to the library user in the future perhaps?

Erk- commented 4 years ago

Yeah I need to handle unrecoverable error in another way. Also note that for now you just need to flip a switch on the application page. Also had a look at your profile and saw you are using it as a chat relay. Maybe a webhook will be better suited for that as you can change username in the request. At least that is how I have seen others do it.

Cldfire commented 4 years ago

Maybe a webhook will be better suited for that as you can change username in the request.

Ooo, I didn't know about that. That's cool!

In my case I'd rather stick to the bot approach for the flexibility down the road, but thanks for the heads-up :smile: (although I could perhaps mix in webhook usage, hmm...)