Skip to content

Commit

Permalink
feature: support guild subscription opt-out (discord-net#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-boyle authored and foxbot committed Oct 1, 2019
1 parent 5b9fae0 commit de31bf2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ internal class IdentifyParams
public int LargeThreshold { get; set; }
[JsonProperty("shard")]
public Optional<int[]> ShardingParams { get; set; }
[JsonProperty("guild_subscriptions")]
public Optional<bool> GuildSubscriptions { get; set; }
}
}
5 changes: 3 additions & 2 deletions src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
}

public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null)
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
var props = new Dictionary<string, string>
Expand All @@ -226,7 +226,8 @@ public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, i
{
Token = AuthToken,
Properties = props,
LargeThreshold = largeThreshold
LargeThreshold = largeThreshold,
GuildSubscriptions = guildSubscriptions
};
if (totalShards > 1)
msg.ShardingParams = new int[] { shardID, totalShards };
Expand Down
4 changes: 3 additions & 1 deletion src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient
private DateTimeOffset? _statusSince;
private RestApplication _applicationInfo;
private bool _isDisposed;
private bool _guildSubscriptions;

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

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

//Wait for READY
Expand Down
11 changes: 8 additions & 3 deletions src/Discord.Net.WebSocket/DiscordSocketConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class DiscordSocketConfig : DiscordRestConfig
/// <para>
/// By default, the Discord gateway will only send offline members if a guild has less than a certain number
/// of members (determined by <see cref="LargeThreshold"/> in this library). This behaviour is why
/// sometimes a user may be missing from the WebSocket cache for collections such as
/// sometimes a user may be missing from the WebSocket cache for collections such as
/// <see cref="Discord.WebSocket.SocketGuild.Users"/>.
/// </para>
/// <para>
Expand All @@ -86,7 +86,7 @@ public class DiscordSocketConfig : DiscordRestConfig
/// downloaded to the WebSocket cache.
/// </para>
/// <para>
/// For more information, please see
/// For more information, please see
/// <see href="https://discordapp.com/developers/docs/topics/gateway#request-guild-members">Request Guild Members</see>
/// on the official Discord API documentation.
/// </para>
Expand All @@ -95,7 +95,7 @@ public class DiscordSocketConfig : DiscordRestConfig
/// traffic. If you are using the command system, the default user TypeReader may fail to find the user
/// due to this issue. This may be resolved at v3 of the library. Until then, you may want to consider
/// overriding the TypeReader and use
/// <see cref="DiscordRestClient.GetUserAsync(System.UInt64,Discord.RequestOptions)"/>
/// <see cref="DiscordRestClient.GetUserAsync(System.UInt64,Discord.RequestOptions)"/>
/// or <see cref="DiscordRestClient.GetGuildUserAsync"/>
/// as a backup.
/// </note>
Expand All @@ -119,6 +119,11 @@ public class DiscordSocketConfig : DiscordRestConfig
/// </summary>
public bool? ExclusiveBulkDelete { get; set; } = null;

/// <summary>
/// Gets or sets enabling dispatching of guild subscription events e.g. presence and typing events.
/// </summary>
public bool GuildSubscriptions { get; set; } = true;

/// <summary>
/// Initializes a default configuration.
/// </summary>
Expand Down

0 comments on commit de31bf2

Please sign in to comment.