diff --git a/src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs b/src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs index af16f22f5..e9e914b67 100644 --- a/src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs +++ b/src/Discord.Net.WebSocket/API/Gateway/IdentifyParams.cs @@ -1,4 +1,4 @@ -#pragma warning disable CS1591 +#pragma warning disable CS1591 using Newtonsoft.Json; using System.Collections.Generic; @@ -15,5 +15,7 @@ namespace Discord.API.Gateway public int LargeThreshold { get; set; } [JsonProperty("shard")] public Optional ShardingParams { get; set; } + [JsonProperty("presence")] + public Optional Presence { get; set; } } } diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.cs b/src/Discord.Net.WebSocket/BaseSocketClient.cs index 548bb75bf..609bef39b 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.cs @@ -171,7 +171,7 @@ namespace Discord.WebSocket /// public abstract RestVoiceRegion GetVoiceRegion(string id); /// - public abstract Task StartAsync(); + public abstract Task StartAsync(IActivity activity = null, UserStatus status = UserStatus.Online); /// public abstract Task StopAsync(); /// diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 0877abfd9..f1d4453cd 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -131,8 +131,13 @@ namespace Discord.WebSocket } /// - public override async Task StartAsync() - => await Task.WhenAll(_shards.Select(x => x.StartAsync())).ConfigureAwait(false); + public override async Task StartAsync(IActivity activity = null, UserStatus status = UserStatus.Online) + { + Activity = activity; + Status = status; + await Task.WhenAll(_shards.Select(x => x.StartAsync())).ConfigureAwait(false); + } + /// public override async Task StopAsync() => await Task.WhenAll(_shards.Select(x => x.StopAsync())).ConfigureAwait(false); diff --git a/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs b/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs index 88ef1134e..d80cbc0d8 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs @@ -215,8 +215,25 @@ namespace Discord.API 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(IActivity activity = null, UserStatus status = UserStatus.Online, int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null) { + var gameModel = new Game(); + // Discord only accepts rich presence over RPC, don't even bother building a payload + if (activity is RichGame) + throw new NotSupportedException("Outgoing Rich Presences are not supported via WebSocket."); + + if (activity != null) + { + gameModel.Name = activity.Name; + gameModel.Type = activity.Type; + if (activity is StreamingGame streamGame) + gameModel.StreamUrl = streamGame.Url; + } + var args = new StatusUpdateParams + { + Status = status, + Game = gameModel + }; options = RequestOptions.CreateOrClone(options); var props = new Dictionary { @@ -226,13 +243,15 @@ namespace Discord.API { Token = AuthToken, Properties = props, - LargeThreshold = largeThreshold + LargeThreshold = largeThreshold, + Presence = args }; if (totalShards > 1) msg.ShardingParams = new int[] { shardID, totalShards }; await SendGatewayAsync(GatewayOpCode.Identify, msg, options: options).ConfigureAwait(false); } + public async Task SendResumeAsync(string sessionId, int lastSeq, RequestOptions options = null) { options = RequestOptions.CreateOrClone(options); diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index b25f61156..c1f8ac34d 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -217,8 +217,12 @@ namespace Discord.WebSocket } /// - public override async Task StartAsync() - => await _connection.StartAsync().ConfigureAwait(false); + public override async Task StartAsync(IActivity activity = null, UserStatus status = UserStatus.Online) + { + Activity = activity; + Status = status; + await _connection.StartAsync().ConfigureAwait(false); + } /// public override async Task StopAsync() => await _connection.StopAsync().ConfigureAwait(false); @@ -240,14 +244,14 @@ namespace Discord.WebSocket else { await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false); - await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); + await ApiClient.SendIdentifyAsync(Activity, Status, shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); } //Wait for READY await _connection.WaitAsync().ConfigureAwait(false); - await _gatewayLogger.DebugAsync("Sending Status").ConfigureAwait(false); - await SendStatusAsync().ConfigureAwait(false); + //await _gatewayLogger.DebugAsync("Sending Status").ConfigureAwait(false); + //await SendStatusAsync().ConfigureAwait(false); } finally { @@ -503,7 +507,7 @@ namespace Discord.WebSocket _sessionId = null; _lastSeq = 0; - await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); + await ApiClient.SendIdentifyAsync(Activity, Status, shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); } break; case GatewayOpCode.Reconnect: