Skip to content

Commit 0d54207

Browse files
k-boylefoxbot
authored andcommitted
feature: support guild subscription opt-out (#1386)
1 parent 3d39704 commit 0d54207

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ internal class IdentifyParams
1515
public int LargeThreshold { get; set; }
1616
[JsonProperty("shard")]
1717
public Optional<int[]> ShardingParams { get; set; }
18+
[JsonProperty("guild_subscriptions")]
19+
public Optional<bool> GuildSubscriptions { get; set; }
1820
}
1921
}

src/Discord.Net.WebSocket/DiscordSocketApiClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload
215215
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
216216
}
217217

218-
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null)
218+
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, RequestOptions options = null)
219219
{
220220
options = RequestOptions.CreateOrClone(options);
221221
var props = new Dictionary<string, string>
@@ -226,7 +226,8 @@ public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, i
226226
{
227227
Token = AuthToken,
228228
Properties = props,
229-
LargeThreshold = largeThreshold
229+
LargeThreshold = largeThreshold,
230+
GuildSubscriptions = guildSubscriptions
230231
};
231232
if (totalShards > 1)
232233
msg.ShardingParams = new int[] { shardID, totalShards };

src/Discord.Net.WebSocket/DiscordSocketClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient
4343
private DateTimeOffset? _statusSince;
4444
private RestApplication _applicationInfo;
4545
private bool _isDisposed;
46+
private bool _guildSubscriptions;
4647

4748
/// <summary>
4849
/// Provides access to a REST-only client with a shared state from this client.
@@ -135,6 +136,7 @@ private DiscordSocketClient(DiscordSocketConfig config, API.DiscordSocketApiClie
135136
State = new ClientState(0, 0);
136137
Rest = new DiscordSocketRestClient(config, ApiClient);
137138
_heartbeatTimes = new ConcurrentQueue<long>();
139+
_guildSubscriptions = config.GuildSubscriptions;
138140

139141
_stateLock = new SemaphoreSlim(1, 1);
140142
_gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}");
@@ -240,7 +242,7 @@ private async Task OnConnectingAsync()
240242
else
241243
{
242244
await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false);
243-
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false);
245+
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions).ConfigureAwait(false);
244246
}
245247

246248
//Wait for READY

src/Discord.Net.WebSocket/DiscordSocketConfig.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class DiscordSocketConfig : DiscordRestConfig
7777
/// <para>
7878
/// By default, the Discord gateway will only send offline members if a guild has less than a certain number
7979
/// of members (determined by <see cref="LargeThreshold"/> in this library). This behaviour is why
80-
/// sometimes a user may be missing from the WebSocket cache for collections such as
80+
/// sometimes a user may be missing from the WebSocket cache for collections such as
8181
/// <see cref="Discord.WebSocket.SocketGuild.Users"/>.
8282
/// </para>
8383
/// <para>
@@ -86,7 +86,7 @@ public class DiscordSocketConfig : DiscordRestConfig
8686
/// downloaded to the WebSocket cache.
8787
/// </para>
8888
/// <para>
89-
/// For more information, please see
89+
/// For more information, please see
9090
/// <see href="https://discordapp.com/developers/docs/topics/gateway#request-guild-members">Request Guild Members</see>
9191
/// on the official Discord API documentation.
9292
/// </para>
@@ -95,7 +95,7 @@ public class DiscordSocketConfig : DiscordRestConfig
9595
/// traffic. If you are using the command system, the default user TypeReader may fail to find the user
9696
/// due to this issue. This may be resolved at v3 of the library. Until then, you may want to consider
9797
/// overriding the TypeReader and use
98-
/// <see cref="DiscordRestClient.GetUserAsync(System.UInt64,Discord.RequestOptions)"/>
98+
/// <see cref="DiscordRestClient.GetUserAsync(System.UInt64,Discord.RequestOptions)"/>
9999
/// or <see cref="DiscordRestClient.GetGuildUserAsync"/>
100100
/// as a backup.
101101
/// </note>
@@ -119,6 +119,11 @@ public class DiscordSocketConfig : DiscordRestConfig
119119
/// </summary>
120120
public bool? ExclusiveBulkDelete { get; set; } = null;
121121

122+
/// <summary>
123+
/// Gets or sets enabling dispatching of guild subscription events e.g. presence and typing events.
124+
/// </summary>
125+
public bool GuildSubscriptions { get; set; } = true;
126+
122127
/// <summary>
123128
/// Initializes a default configuration.
124129
/// </summary>

0 commit comments

Comments
 (0)