From 51e06e9ce190ac46aeaeb8650d71d58eba0b5412 Mon Sep 17 00:00:00 2001
From: Quin Lynch <49576606+quinchs@users.noreply.github.com>
Date: Fri, 26 Nov 2021 11:30:19 -0400
Subject: [PATCH] feature: warn on invalid gateway intents (#1948)
---
.../DiscordSocketClient.cs | 51 +++++++++++++++++++
.../DiscordSocketConfig.cs | 5 ++
2 files changed, 56 insertions(+)
diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs
index 444e69a26..9ef827778 100644
--- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs
+++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs
@@ -76,6 +76,7 @@ namespace Discord.WebSocket
internal int? HandlerTimeout { get; private set; }
internal bool AlwaysDownloadDefaultStickers { get; private set; }
internal bool AlwaysResolveStickers { get; private set; }
+ internal bool LogGatewayIntentWarnings { get; private set; }
internal new DiscordSocketApiClient ApiClient => base.ApiClient;
///
public override IReadOnlyCollection Guilds => State.Guilds;
@@ -147,6 +148,7 @@ namespace Discord.WebSocket
AlwaysDownloadUsers = config.AlwaysDownloadUsers;
AlwaysDownloadDefaultStickers = config.AlwaysDownloadDefaultStickers;
AlwaysResolveStickers = config.AlwaysResolveStickers;
+ LogGatewayIntentWarnings = config.LogGatewayIntentWarnings;
HandlerTimeout = config.HandlerTimeout;
State = new ClientState(0, 0);
Rest = new DiscordSocketRestClient(config, ApiClient);
@@ -238,6 +240,9 @@ namespace Discord.WebSocket
_defaultStickers = builder.ToImmutable();
}
+
+ if(LogGatewayIntentWarnings)
+ await LogGatewayIntentsWarning().ConfigureAwait(false);
}
///
@@ -708,6 +713,52 @@ namespace Discord.WebSocket
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
private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload)
{
diff --git a/src/Discord.Net.WebSocket/DiscordSocketConfig.cs b/src/Discord.Net.WebSocket/DiscordSocketConfig.cs
index 8615eac71..f0e6dc857 100644
--- a/src/Discord.Net.WebSocket/DiscordSocketConfig.cs
+++ b/src/Discord.Net.WebSocket/DiscordSocketConfig.cs
@@ -183,6 +183,11 @@ namespace Discord.WebSocket
///
public GatewayIntents GatewayIntents { get; set; } = GatewayIntents.AllUnprivileged;
+ ///
+ /// Gets or sets whether or not to log warnings related to guild intents and events.
+ ///
+ public bool LogGatewayIntentWarnings { get; set; } = true;
+
///
/// Initializes a new instance of the class with the default configuration.
///