Skip to content

Commit

Permalink
feature: Support Gateway Intents (#1566)
Browse files Browse the repository at this point in the history
* Support Gateway Intents

Allows supplying gateway intents through DiscordSocketConfig which will be passed through the IDENTIFY payload, in order to choose what gateway events you want to receive.

* Fixing enum casing

* Feedback

* Updating comment for GuildSubscriptions

* Comment update
  • Loading branch information
moiph authored Jun 18, 2020
1 parent 5227241 commit d5d10d3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/Discord.Net.Core/GatewayIntents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace Discord
{
[Flags]
public enum GatewayIntents
{
/// <summary> This intent includes no events </summary>
None = 0,
/// <summary> This intent includes GUILD_CREATE, GUILD_UPDATE, GUILD_DELETE, GUILD_ROLE_CREATE, GUILD_ROLE_UPDATE, GUILD_ROLE_DELETE, CHANNEL_CREATE, CHANNEL_UPDATE, CHANNEL_DELETE, CHANNEL_PINS_UPDATE </summary>
Guilds = 1 << 0,
/// <summary> This intent includes GUILD_MEMBER_ADD, GUILD_MEMBER_UPDATE, GUILD_MEMBER_REMOVE </summary>
GuildMembers = 1 << 1,
/// <summary> This intent includes GUILD_BAN_ADD, GUILD_BAN_REMOVE </summary>
GuildBans = 1 << 2,
/// <summary> This intent includes GUILD_EMOJIS_UPDATE </summary>
GuildEmojis = 1 << 3,
/// <summary> This intent includes GUILD_INTEGRATIONS_UPDATE </summary>
GuildIntegrations = 1 << 4,
/// <summary> This intent includes WEBHOOKS_UPDATE </summary>
GuildWebhooks = 1 << 5,
/// <summary> This intent includes INVITE_CREATE, INVITE_DELETE </summary>
GuildInvites = 1 << 6,
/// <summary> This intent includes VOICE_STATE_UPDATE </summary>
GuildVoiceStates = 1 << 7,
/// <summary> This intent includes PRESENCE_UPDATE </summary>
GuildPresences = 1 << 8,
/// <summary> This intent includes MESSAGE_CREATE, MESSAGE_UPDATE, MESSAGE_DELETE, MESSAGE_DELETE_BULK </summary>
GuildMessages = 1 << 9,
/// <summary> This intent includes MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE, MESSAGE_REACTION_REMOVE_ALL, MESSAGE_REACTION_REMOVE_EMOJI </summary>
GuildMessageReactions = 1 << 10,
/// <summary> This intent includes TYPING_START </summary>
GuildMessageTyping = 1 << 11,
/// <summary> This intent includes CHANNEL_CREATE, MESSAGE_CREATE, MESSAGE_UPDATE, MESSAGE_DELETE, CHANNEL_PINS_UPDATE </summary>
DirectMessages = 1 << 12,
/// <summary> This intent includes MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE, MESSAGE_REACTION_REMOVE_ALL, MESSAGE_REACTION_REMOVE_EMOJI </summary>
DirectMessageReactions = 1 << 13,
/// <summary> This intent includes TYPING_START </summary>
DirectMessageTyping = 1 << 14,
}
}
4 changes: 3 additions & 1 deletion src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
using Newtonsoft.Json;
using System.Collections.Generic;

Expand All @@ -17,5 +17,7 @@ internal class IdentifyParams
public Optional<int[]> ShardingParams { get; set; }
[JsonProperty("guild_subscriptions")]
public Optional<bool> GuildSubscriptions { get; set; }
[JsonProperty("intents")]
public Optional<int> Intents { get; set; }
}
}
10 changes: 7 additions & 3 deletions src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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, bool guildSubscriptions = true, RequestOptions options = null)
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, GatewayIntents? gatewayIntents = null, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
var props = new Dictionary<string, string>
Expand All @@ -220,12 +220,16 @@ public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, i
{
Token = AuthToken,
Properties = props,
LargeThreshold = largeThreshold,
GuildSubscriptions = guildSubscriptions
LargeThreshold = largeThreshold
};
if (totalShards > 1)
msg.ShardingParams = new int[] { shardID, totalShards };

if (gatewayIntents.HasValue)
msg.Intents = (int)gatewayIntents.Value;
else
msg.GuildSubscriptions = guildSubscriptions;

await SendGatewayAsync(GatewayOpCode.Identify, msg, options: options).ConfigureAwait(false);
}
public async Task SendResumeAsync(string sessionId, int lastSeq, RequestOptions options = null)
Expand Down
6 changes: 4 additions & 2 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient
private RestApplication _applicationInfo;
private bool _isDisposed;
private bool _guildSubscriptions;
private GatewayIntents? _gatewayIntents;

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

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

//Wait for READY
Expand Down Expand Up @@ -517,7 +519,7 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
_sessionId = null;
_lastSeq = 0;

await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false);
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions, gatewayIntents: _gatewayIntents).ConfigureAwait(false);
}
break;
case GatewayOpCode.Reconnect:
Expand Down
11 changes: 11 additions & 0 deletions src/Discord.Net.WebSocket/DiscordSocketConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,20 @@ public class DiscordSocketConfig : DiscordRestConfig

/// <summary>
/// Gets or sets enabling dispatching of guild subscription events e.g. presence and typing events.
/// This is not used if <see cref="GatewayIntents"/> are provided.
/// </summary>
public bool GuildSubscriptions { get; set; } = true;

/// <summary>
/// Gets or sets gateway intents to limit what events are sent from Discord. Allows for more granular control than the <see cref="GuildSubscriptions"/> property.
/// </summary>
/// <remarks>
/// For more information, please see
/// <see href="https://discord.com/developers/docs/topics/gateway#gateway-intents">GatewayIntents</see>
/// on the official Discord API documentation.
/// </remarks>
public GatewayIntents? GatewayIntents { get; set; }

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

0 comments on commit d5d10d3

Please sign in to comment.