From 043f8ded85654ec5c2bf607ba7fddcc1050f167f Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 21 Oct 2016 11:22:26 -0300 Subject: [PATCH] Moved ISelfUser.ModifyStatusAsync to DiscordSocketClient.SetStatus/SetGame. Resend status on reconnect. --- .../API/Rest/ModifyPresenceParams.cs | 11 ---- .../Entities/Users/ISelfUser.cs | 1 - .../Entities/Users/RestSelfUser.cs | 2 - .../DiscordSocketClient.cs | 55 ++++++++++++++++++- .../Entities/Users/SocketSelfUser.cs | 51 ----------------- 5 files changed, 54 insertions(+), 66 deletions(-) delete mode 100644 src/Discord.Net.Core/API/Rest/ModifyPresenceParams.cs diff --git a/src/Discord.Net.Core/API/Rest/ModifyPresenceParams.cs b/src/Discord.Net.Core/API/Rest/ModifyPresenceParams.cs deleted file mode 100644 index ac55e2491..000000000 --- a/src/Discord.Net.Core/API/Rest/ModifyPresenceParams.cs +++ /dev/null @@ -1,11 +0,0 @@ -#pragma warning disable CS1591 -using System; - -namespace Discord.API.Rest -{ - public class ModifyPresenceParams - { - public Optional Status { get; set; } - public Optional Game { get; set; } - } -} diff --git a/src/Discord.Net.Core/Entities/Users/ISelfUser.cs b/src/Discord.Net.Core/Entities/Users/ISelfUser.cs index a6e8f80e8..782ea22c7 100644 --- a/src/Discord.Net.Core/Entities/Users/ISelfUser.cs +++ b/src/Discord.Net.Core/Entities/Users/ISelfUser.cs @@ -14,6 +14,5 @@ namespace Discord bool IsMfaEnabled { get; } Task ModifyAsync(Action func, RequestOptions options = null); - Task ModifyStatusAsync(Action func, RequestOptions options = null); } } \ No newline at end of file diff --git a/src/Discord.Net.Rest/Entities/Users/RestSelfUser.cs b/src/Discord.Net.Rest/Entities/Users/RestSelfUser.cs index 368d8c798..c97443522 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestSelfUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestSelfUser.cs @@ -50,7 +50,5 @@ namespace Discord.Rest var model = await UserHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false); Update(model); } - - Task ISelfUser.ModifyStatusAsync(Action func, RequestOptions options) { throw new NotSupportedException(); } } } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 1d8d696b6..27825e64b 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -16,6 +16,7 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using GameModel = Discord.API.Game; namespace Discord.WebSocket { @@ -36,6 +37,7 @@ namespace Discord.WebSocket private long _lastGuildAvailableTime; private int _nextAudioId; private bool _canReconnect; + private DateTimeOffset? _statusSince; /// Gets the shard of of this client. public int ShardId { get; } @@ -43,6 +45,8 @@ namespace Discord.WebSocket public ConnectionState ConnectionState { get; private set; } /// Gets the estimated round-trip latency, in milliseconds, to the gateway server. public int Latency { get; private set; } + internal UserStatus Status { get; private set; } + internal Game? Game { get; private set; } //From DiscordSocketConfig internal int TotalShards { get; private set; } @@ -183,8 +187,12 @@ namespace Discord.WebSocket await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); } - await _gatewayLogger.DebugAsync("Raising Event").ConfigureAwait(false); await _connectTask.Task.ConfigureAwait(false); + + await _gatewayLogger.DebugAsync("Sending Status").ConfigureAwait(false); + await SendStatus().ConfigureAwait(false); + + await _gatewayLogger.DebugAsync("Raising Event").ConfigureAwait(false); if (!isReconnecting) _canReconnect = true; ConnectionState = ConnectionState.Connected; @@ -448,6 +456,51 @@ namespace Discord.WebSocket } } + public async Task SetStatus(UserStatus status) + { + Status = status; + if (status == UserStatus.AFK) + _statusSince = DateTimeOffset.UtcNow; + else + _statusSince = null; + await SendStatus().ConfigureAwait(false); + } + public async Task SetGame(string name, string streamUrl = null, StreamType streamType = StreamType.NotStreaming) + { + if (name != null) + Game = new Game(name, streamUrl, streamType); + else + Game = null; + CurrentUser.Presence = new SocketPresence(Status, Game); + await SendStatus().ConfigureAwait(false); + } + private async Task SendStatus() + { + var game = Game; + var status = Status; + var statusSince = _statusSince; + CurrentUser.Presence = new SocketPresence(status, game); + + GameModel gameModel; + if (game != null) + { + gameModel = new API.Game + { + Name = game.Value.Name, + StreamType = game.Value.StreamType, + StreamUrl = game.Value.StreamUrl + }; + } + else + gameModel = null; + + await ApiClient.SendStatusUpdateAsync( + status, + status == UserStatus.AFK, + statusSince != null ? _statusSince.Value.ToUnixTimeMilliseconds() : (long?)null, + gameModel).ConfigureAwait(false); + } + private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload) { if (seq != null) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketSelfUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketSelfUser.cs index 6e230c317..9b85e15e0 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketSelfUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketSelfUser.cs @@ -3,7 +3,6 @@ using Discord.Rest; using System; using System.Diagnostics; using System.Threading.Tasks; -using GameEntity = Discord.Game; using Model = Discord.API.User; namespace Discord.WebSocket @@ -11,8 +10,6 @@ namespace Discord.WebSocket [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class SocketSelfUser : SocketUser, ISelfUser { - private DateTimeOffset? _statusSince; - public string Email { get; private set; } public bool IsVerified { get; private set; } public bool IsMfaEnabled { get; private set; } @@ -49,54 +46,6 @@ namespace Discord.WebSocket public Task ModifyAsync(Action func, RequestOptions options = null) => UserHelper.ModifyAsync(this, Discord, func, options); - public async Task ModifyStatusAsync(Action func, RequestOptions options = null) - { - var args = new ModifyPresenceParams(); - func(args); - - UserStatus status; - if (args.Status.IsSpecified) - { - status = args.Status.Value; - if (status == UserStatus.AFK) - _statusSince = DateTimeOffset.UtcNow; - else - _statusSince = null; - } - else - status = Status; - - GameEntity? game; - if (args.Game.IsSpecified) - { - var model = args.Game.Value; - if (model != null) - game = GameEntity.Create(model); - else - game = null; - } - else - game = Game; - - Presence = new SocketPresence(status, game); - - await SendStatus(status, game).ConfigureAwait(false); - } - internal async Task SendStatus(UserStatus status, GameEntity? game) - { - var gameModel = game != null ? new API.Game - { - Name = game.Value.Name, - StreamType = game.Value.StreamType, - StreamUrl = game.Value.StreamUrl - } : null; - - await Discord.ApiClient.SendStatusUpdateAsync( - status, - status == UserStatus.AFK, - _statusSince != null ? _statusSince.Value.ToUnixTimeMilliseconds() : (long?)null, - gameModel).ConfigureAwait(false); - } internal new SocketSelfUser Clone() => MemberwiseClone() as SocketSelfUser; }