feature: warn on invalid gateway intents (#1948)

This commit is contained in:
Quin Lynch
2021-11-26 11:30:19 -04:00
committed by GitHub
parent 82276e351a
commit 51e06e9ce1
2 changed files with 56 additions and 0 deletions

View File

@@ -76,6 +76,7 @@ namespace Discord.WebSocket
internal int? HandlerTimeout { get; private set; } internal int? HandlerTimeout { get; private set; }
internal bool AlwaysDownloadDefaultStickers { get; private set; } internal bool AlwaysDownloadDefaultStickers { get; private set; }
internal bool AlwaysResolveStickers { get; private set; } internal bool AlwaysResolveStickers { get; private set; }
internal bool LogGatewayIntentWarnings { get; private set; }
internal new DiscordSocketApiClient ApiClient => base.ApiClient; internal new DiscordSocketApiClient ApiClient => base.ApiClient;
/// <inheritdoc /> /// <inheritdoc />
public override IReadOnlyCollection<SocketGuild> Guilds => State.Guilds; public override IReadOnlyCollection<SocketGuild> Guilds => State.Guilds;
@@ -147,6 +148,7 @@ namespace Discord.WebSocket
AlwaysDownloadUsers = config.AlwaysDownloadUsers; AlwaysDownloadUsers = config.AlwaysDownloadUsers;
AlwaysDownloadDefaultStickers = config.AlwaysDownloadDefaultStickers; AlwaysDownloadDefaultStickers = config.AlwaysDownloadDefaultStickers;
AlwaysResolveStickers = config.AlwaysResolveStickers; AlwaysResolveStickers = config.AlwaysResolveStickers;
LogGatewayIntentWarnings = config.LogGatewayIntentWarnings;
HandlerTimeout = config.HandlerTimeout; HandlerTimeout = config.HandlerTimeout;
State = new ClientState(0, 0); State = new ClientState(0, 0);
Rest = new DiscordSocketRestClient(config, ApiClient); Rest = new DiscordSocketRestClient(config, ApiClient);
@@ -238,6 +240,9 @@ namespace Discord.WebSocket
_defaultStickers = builder.ToImmutable(); _defaultStickers = builder.ToImmutable();
} }
if(LogGatewayIntentWarnings)
await LogGatewayIntentsWarning().ConfigureAwait(false);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -708,6 +713,52 @@ namespace Discord.WebSocket
game); game);
} }
private async Task LogGatewayIntentsWarning()
{
if(_gatewayIntents.HasFlag(GatewayIntents.GuildPresences) && !_presenceUpdated.HasSubscribers)
{
await _gatewayLogger.WarningAsync("You're using the GuildPresences intent without listening to the PresenceUpdate event, consider removing the intent from your config.").ConfigureAwait(false);
}
if(!_gatewayIntents.HasFlag(GatewayIntents.GuildPresences) && _presenceUpdated.HasSubscribers)
{
await _gatewayLogger.WarningAsync("You're using the PresenceUpdate event without specifying the GuildPresences intent, consider adding the intent to your config.").ConfigureAwait(false);
}
bool hasGuildScheduledEventsSubscribers =
_guildScheduledEventCancelled.HasSubscribers ||
_guildScheduledEventUserRemove.HasSubscribers ||
_guildScheduledEventCompleted.HasSubscribers ||
_guildScheduledEventCreated.HasSubscribers ||
_guildScheduledEventStarted.HasSubscribers ||
_guildScheduledEventUpdated.HasSubscribers ||
_guildScheduledEventUserAdd.HasSubscribers;
if(_gatewayIntents.HasFlag(GatewayIntents.GuildScheduledEvents) && !hasGuildScheduledEventsSubscribers)
{
await _gatewayLogger.WarningAsync("You're using the GuildScheduledEvents gateway intent without listening to any events related to that intent, consider removing the intent from your config.").ConfigureAwait(false);
}
if(!_gatewayIntents.HasFlag(GatewayIntents.GuildScheduledEvents) && hasGuildScheduledEventsSubscribers)
{
await _gatewayLogger.WarningAsync("You're using events related to the GuildScheduledEvents gateway intent without specifying the intent, consider adding the intent to your config.").ConfigureAwait(false);
}
bool hasInviteEventSubscribers =
_inviteCreatedEvent.HasSubscribers ||
_inviteDeletedEvent.HasSubscribers;
if (_gatewayIntents.HasFlag(GatewayIntents.GuildInvites) && !hasInviteEventSubscribers)
{
await _gatewayLogger.WarningAsync("You're using the GuildInvites gateway intent without listening to any events related to that intent, consider removing the intent from your config.").ConfigureAwait(false);
}
if (!_gatewayIntents.HasFlag(GatewayIntents.GuildInvites) && hasInviteEventSubscribers)
{
await _gatewayLogger.WarningAsync("You're using events related to the GuildInvites gateway intent without specifying the intent, consider adding the intent to your config.").ConfigureAwait(false);
}
}
#region ProcessMessageAsync #region ProcessMessageAsync
private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload) private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload)
{ {

View File

@@ -183,6 +183,11 @@ namespace Discord.WebSocket
/// </remarks> /// </remarks>
public GatewayIntents GatewayIntents { get; set; } = GatewayIntents.AllUnprivileged; public GatewayIntents GatewayIntents { get; set; } = GatewayIntents.AllUnprivileged;
/// <summary>
/// Gets or sets whether or not to log warnings related to guild intents and events.
/// </summary>
public bool LogGatewayIntentWarnings { get; set; } = true;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DiscordSocketConfig"/> class with the default configuration. /// Initializes a new instance of the <see cref="DiscordSocketConfig"/> class with the default configuration.
/// </summary> /// </summary>