| @@ -1,4 +1,4 @@ | |||||
| #pragma warning disable CS1591 | |||||
| #pragma warning disable CS1591 | |||||
| using Newtonsoft.Json; | using Newtonsoft.Json; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| @@ -15,5 +15,7 @@ namespace Discord.API.Gateway | |||||
| public int LargeThreshold { get; set; } | public int LargeThreshold { get; set; } | ||||
| [JsonProperty("shard")] | [JsonProperty("shard")] | ||||
| public Optional<int[]> ShardingParams { get; set; } | public Optional<int[]> ShardingParams { get; set; } | ||||
| [JsonProperty("presence")] | |||||
| public Optional<StatusUpdateParams> Presence { get; set; } | |||||
| } | } | ||||
| } | } | ||||
| @@ -171,7 +171,7 @@ namespace Discord.WebSocket | |||||
| /// </returns> | /// </returns> | ||||
| public abstract RestVoiceRegion GetVoiceRegion(string id); | public abstract RestVoiceRegion GetVoiceRegion(string id); | ||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public abstract Task StartAsync(); | |||||
| public abstract Task StartAsync(IActivity activity = null, UserStatus status = UserStatus.Online); | |||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public abstract Task StopAsync(); | public abstract Task StopAsync(); | ||||
| /// <summary> | /// <summary> | ||||
| @@ -131,8 +131,13 @@ namespace Discord.WebSocket | |||||
| } | } | ||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| 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); | |||||
| } | |||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public override async Task StopAsync() | public override async Task StopAsync() | ||||
| => await Task.WhenAll(_shards.Select(x => x.StopAsync())).ConfigureAwait(false); | => await Task.WhenAll(_shards.Select(x => x.StopAsync())).ConfigureAwait(false); | ||||
| @@ -215,8 +215,25 @@ namespace Discord.API | |||||
| await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); | 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); | options = RequestOptions.CreateOrClone(options); | ||||
| var props = new Dictionary<string, string> | var props = new Dictionary<string, string> | ||||
| { | { | ||||
| @@ -226,13 +243,15 @@ namespace Discord.API | |||||
| { | { | ||||
| Token = AuthToken, | Token = AuthToken, | ||||
| Properties = props, | Properties = props, | ||||
| LargeThreshold = largeThreshold | |||||
| LargeThreshold = largeThreshold, | |||||
| Presence = args | |||||
| }; | }; | ||||
| if (totalShards > 1) | if (totalShards > 1) | ||||
| msg.ShardingParams = new int[] { shardID, totalShards }; | msg.ShardingParams = new int[] { shardID, totalShards }; | ||||
| await SendGatewayAsync(GatewayOpCode.Identify, msg, options: options).ConfigureAwait(false); | await SendGatewayAsync(GatewayOpCode.Identify, msg, options: options).ConfigureAwait(false); | ||||
| } | } | ||||
| public async Task SendResumeAsync(string sessionId, int lastSeq, RequestOptions options = null) | public async Task SendResumeAsync(string sessionId, int lastSeq, RequestOptions options = null) | ||||
| { | { | ||||
| options = RequestOptions.CreateOrClone(options); | options = RequestOptions.CreateOrClone(options); | ||||
| @@ -217,8 +217,12 @@ namespace Discord.WebSocket | |||||
| } | } | ||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| 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); | |||||
| } | |||||
| /// <inheritdoc /> | /// <inheritdoc /> | ||||
| public override async Task StopAsync() | public override async Task StopAsync() | ||||
| => await _connection.StopAsync().ConfigureAwait(false); | => await _connection.StopAsync().ConfigureAwait(false); | ||||
| @@ -240,14 +244,14 @@ namespace Discord.WebSocket | |||||
| else | else | ||||
| { | { | ||||
| await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false); | 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 | //Wait for READY | ||||
| await _connection.WaitAsync().ConfigureAwait(false); | 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 | finally | ||||
| { | { | ||||
| @@ -503,7 +507,7 @@ namespace Discord.WebSocket | |||||
| _sessionId = null; | _sessionId = null; | ||||
| _lastSeq = 0; | _lastSeq = 0; | ||||
| await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); | |||||
| await ApiClient.SendIdentifyAsync(Activity, Status, shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); | |||||
| } | } | ||||
| break; | break; | ||||
| case GatewayOpCode.Reconnect: | case GatewayOpCode.Reconnect: | ||||