Browse Source

Added ISelfUser.ModifyStatusAsync

tags/1.0-rc
RogueException 9 years ago
parent
commit
9e8b897589
5 changed files with 60 additions and 1 deletions
  1. +9
    -0
      src/Discord.Net/API/DiscordAPIClient.cs
  2. +12
    -0
      src/Discord.Net/API/Gateway/StatusUpdateParams.cs
  3. +8
    -0
      src/Discord.Net/API/Rest/ModifyPresenceParams.cs
  4. +1
    -0
      src/Discord.Net/Entities/Users/ISelfUser.cs
  5. +30
    -1
      src/Discord.Net/Entities/Users/SelfUser.cs

+ 9
- 0
src/Discord.Net/API/DiscordAPIClient.cs View File

@@ -394,6 +394,15 @@ namespace Discord.API
{
await SendGatewayAsync(GatewayOpCode.Heartbeat, lastSeq, options: options).ConfigureAwait(false);
}
public async Task SendStatusUpdateAsync(long? idleSince, Game game, RequestOptions options = null)
{
var args = new StatusUpdateParams
{
IdleSince = idleSince,
Game = game
};
await SendGatewayAsync(GatewayOpCode.StatusUpdate, args, options: options).ConfigureAwait(false);
}
public async Task SendRequestMembersAsync(IEnumerable<ulong> guildIds, RequestOptions options = null)
{
await SendGatewayAsync(GatewayOpCode.RequestGuildMembers, new RequestMembersParams { GuildIds = guildIds, Query = "", Limit = 0 }, options: options).ConfigureAwait(false);


+ 12
- 0
src/Discord.Net/API/Gateway/StatusUpdateParams.cs View File

@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Discord.API.Gateway
{
public class StatusUpdateParams
{
[JsonProperty("idle_since"), Int53]
public long? IdleSince { get; set; }
[JsonProperty("game")]
public Game Game { get; set; }
}
}

+ 8
- 0
src/Discord.Net/API/Rest/ModifyPresenceParams.cs View File

@@ -0,0 +1,8 @@
namespace Discord.API.Rest
{
public class ModifyPresenceParams
{
public Optional<UserStatus> Status { get; set; }
public Optional<Discord.Game> Game { get; set; }
}
}

+ 1
- 0
src/Discord.Net/Entities/Users/ISelfUser.cs View File

@@ -14,5 +14,6 @@ namespace Discord
bool IsMfaEnabled { get; }

Task ModifyAsync(Action<ModifyCurrentUserParams> func);
Task ModifyStatusAsync(Action<ModifyPresenceParams> func);
}
}

+ 30
- 1
src/Discord.Net/Entities/Users/SelfUser.cs View File

@@ -6,11 +6,18 @@ using Model = Discord.API.User;
namespace Discord
{
internal class SelfUser : User, ISelfUser
{
{
private long _idleSince;
private UserStatus _status;
private Game _game;

public string Email { get; private set; }
public bool IsVerified { get; private set; }
public bool IsMfaEnabled { get; private set; }

public override UserStatus Status => _status;
public override Game Game => _game;

public override DiscordClient Discord { get; }

public SelfUser(DiscordClient discord, Model model)
@@ -49,5 +56,27 @@ namespace Discord
var model = await Discord.ApiClient.ModifySelfAsync(args).ConfigureAwait(false);
Update(model, UpdateSource.Rest);
}
public async Task ModifyStatusAsync(Action<ModifyPresenceParams> func)
{
if (func == null) throw new NullReferenceException(nameof(func));
var args = new ModifyPresenceParams();
func(args);

var game = args.Game.GetValueOrDefault(_game);
var status = args.Status.GetValueOrDefault(_status);

long idleSince = _idleSince;
if (status == UserStatus.Idle && _status != UserStatus.Idle)
idleSince = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var apiGame = new API.Game { Name = game.Name, StreamType = game.StreamType, StreamUrl = game.StreamUrl };

await Discord.ApiClient.SendStatusUpdateAsync(status == UserStatus.Idle ? _idleSince : (long?)null, apiGame).ConfigureAwait(false);
//Save values
_idleSince = idleSince;
_game = game;
_status = status;
}
}
}

Loading…
Cancel
Save