GetGatewayInfoAsync throws NullReferenceException when an invalid token has been provided to DiscordShardedClient
Created by: Soyvolon
Summary
Providing an invalid token to a DiscordShardedClient
and calling UseCommandsNextAsync
will call GetGatewayInfoAsync
, but if there is an invalid token the gateway response, info
will be populate with null values that are never checked.
Details
.NET Core 3.1 D#+ Nightly-00725 (Latest from master branch as of 9/23/2020 10:00 EST)
Using an invalid token will cause var resp = await http.GetAsync(url).ConfigureAwait(false);
to get a response of 401 Unauthorized
, but there is no check in GetGatewayInfoAsync
to see if the response was valid or not. When it is parsed to the GatewayInfo
object, all values are either null or 0, causing the NullReferenceException
.
private async Task<GatewayInfo> GetGatewayInfoAsync()
{
// . . .
var info = jo.ToObject<GatewayInfo>();
//There is a delay from parsing here.
timer.Stop();
// info.SessionBucket is null if there is an invalid token.
info.SessionBucket.resetAfter -= (int)timer.ElapsedMilliseconds;
// . . .
}
Steps to reproduce
- Use
DiscordShardedClient
with an invalid token. - Call
DiscordShardedClient.GetCommandsNextAsync
before usingStartAsync
.
Notes
When using a DiscordClient with an invalid token, you get this exception:
throw new Exception("Authentication failed. Check your token and try again.", e);
Throwing a similar exception from GetGatewayInfoAsync
when the response from Discord is 401 Unauthorized
will help users know what the problem is.