Added SelfUser.ModifyStatusAsync

This commit is contained in:
RogueException
2016-10-06 03:16:14 -03:00
parent f41df1f966
commit 6c34441f4c
8 changed files with 69 additions and 11 deletions

View File

@@ -1,4 +1,6 @@
#pragma warning disable CS1591
using System;
namespace Discord.API.Rest
{
public class ModifyPresenceParams

View File

@@ -5,6 +5,7 @@
Unknown,
Online,
Idle,
AFK,
DoNotDisturb,
Invisible,
Offline

View File

@@ -38,6 +38,7 @@ namespace Discord.Net.Converters
writer.WriteValue("online");
break;
case UserStatus.Idle:
case UserStatus.AFK:
writer.WriteValue("idle");
break;
case UserStatus.DoNotDisturb:

View File

@@ -212,12 +212,14 @@ namespace Discord.API
options = RequestOptions.CreateOrClone(options);
await SendGatewayAsync(GatewayOpCode.Heartbeat, lastSeq, options: options).ConfigureAwait(false);
}
public async Task SendStatusUpdateAsync(long? idleSince, Game game, RequestOptions options = null)
public async Task SendStatusUpdateAsync(UserStatus status, bool isAFK, long? since, Game game, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
var args = new StatusUpdateParams
{
IdleSince = idleSince,
Status = status,
IdleSince = since,
IsAFK = isAFK,
Game = game
};
await SendGatewayAsync(GatewayOpCode.StatusUpdate, args, options: options).ConfigureAwait(false);

View File

@@ -6,8 +6,12 @@ namespace Discord.API.Gateway
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class StatusUpdateParams
{
[JsonProperty("idle_since"), Int53]
[JsonProperty("status")]
public UserStatus Status { get; set; }
[JsonProperty("since"), Int53]
public long? IdleSince { get; set; }
[JsonProperty("afk")]
public bool IsAFK { get; set; }
[JsonProperty("game")]
public Game Game { get; set; }
}

View File

@@ -376,7 +376,7 @@ namespace Discord.WebSocket
{
var user = SocketGlobalUser.Create(this, state, model);
user.GlobalUser.AddRef();
user.Presence = new SocketPresence(null, UserStatus.Online);
user.Presence = new SocketPresence(UserStatus.Online, null);
return user;
});
}
@@ -1311,7 +1311,7 @@ namespace Discord.WebSocket
}
else
{
before = new SocketPresence(null, UserStatus.Offline);
before = new SocketPresence(UserStatus.Offline, null);
user = guild.AddOrUpdateUser(data);
}

View File

@@ -3,6 +3,7 @@ using Discord.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using GameEntity = Discord.Game;
using Model = Discord.API.User;
namespace Discord.WebSocket
@@ -10,6 +11,8 @@ 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; }
@@ -44,12 +47,57 @@ namespace Discord.WebSocket
IsMfaEnabled = model.MfaEnabled.Value;
}
public Task ModifyAsync(Action<ModifyCurrentUserParams> func)
=> UserHelper.ModifyAsync(this, Discord, func);
public Task ModifyAsync(Action<ModifyCurrentUserParams> func, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, func, options);
public async Task ModifyStatusAsync(Action<ModifyPresenceParams> 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);
}
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);
}
internal new SocketSelfUser Clone() => MemberwiseClone() as SocketSelfUser;
//ISelfUser
Task ISelfUser.ModifyStatusAsync(Action<ModifyPresenceParams> func) { throw new NotSupportedException(); }
}
}

View File

@@ -12,7 +12,7 @@ namespace Discord.WebSocket
public override string Username { get; internal set; }
public override ushort DiscriminatorValue { get; internal set; }
public override string AvatarId { get; internal set; }
internal override SocketPresence Presence { get { return new SocketPresence(null, UserStatus.Offline); } set { } }
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null); } set { } }
internal override SocketGlobalUser GlobalUser { get { throw new NotSupportedException(); } }