Skip to content

Commit

Permalink
fix: Rollback Activities to Game (discord-net#1702)
Browse files Browse the repository at this point in the history
  • Loading branch information
SubZero0 authored Dec 1, 2020
1 parent e800d58 commit 6dd25d2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Discord.Net.WebSocket/API/Gateway/StatusUpdateParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class StatusUpdateParams
public long? IdleSince { get; set; }
[JsonProperty("afk")]
public bool IsAFK { get; set; }
[JsonProperty("activities")]
public Game[] Activities { get; set; }
[JsonProperty("game")]
public Game Game { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
}

public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, GatewayIntents? gatewayIntents = null, (UserStatus, bool, long?, GameModel[])? presence = null, RequestOptions options = null)
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, GatewayIntents? gatewayIntents = null, (UserStatus, bool, long?, GameModel)? presence = null, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
var props = new Dictionary<string, string>
Expand Down Expand Up @@ -246,7 +246,7 @@ public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, i
Status = presence.Value.Item1,
IsAFK = presence.Value.Item2,
IdleSince = presence.Value.Item3,
Activities = presence.Value.Item4
Game = presence.Value.Item4,
};
}

Expand All @@ -268,15 +268,15 @@ public async Task SendHeartbeatAsync(int lastSeq, RequestOptions options = null)
options = RequestOptions.CreateOrClone(options);
await SendGatewayAsync(GatewayOpCode.Heartbeat, lastSeq, options: options).ConfigureAwait(false);
}
public async Task SendStatusUpdateAsync(UserStatus status, bool isAFK, long? since, GameModel[] game, RequestOptions options = null)
public async Task SendStatusUpdateAsync(UserStatus status, bool isAFK, long? since, GameModel game, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
var args = new StatusUpdateParams
{
Status = status,
IdleSince = since,
IsAFK = isAFK,
Activities = game
Game = game
};
options.BucketId = GatewayBucket.Get(GatewayBucketType.PresenceUpdate).Id;
await SendGatewayAsync(GatewayOpCode.StatusUpdate, args, options: options).ConfigureAwait(false);
Expand Down
30 changes: 17 additions & 13 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public partial class DiscordSocketClient : BaseSocketClient, IDiscordClient
/// <inheritdoc />
public override int Latency { get; protected set; }
/// <inheritdoc />
public override UserStatus Status { get; protected set; } = UserStatus.Online;
public override UserStatus Status { get => _status ?? UserStatus.Online; protected set => _status = value; }
private UserStatus? _status;
/// <inheritdoc />
public override IActivity Activity { get => _activity.GetValueOrDefault(); protected set => _activity = Optional.Create(value); }
private Optional<IActivity> _activity;
Expand Down Expand Up @@ -449,22 +450,25 @@ private async Task SendStatusAsync()
return;
CurrentUser.Presence = new SocketPresence(Status, Activity, null, null);

var presence = BuildCurrentStatus();
var presence = BuildCurrentStatus() ?? (UserStatus.Online, false, null, null);

await ApiClient.SendStatusUpdateAsync(
presence.Item1,
presence.Item2,
presence.Item3,
presence.Item4).ConfigureAwait(false);
status: presence.Item1,
isAFK: presence.Item2,
since: presence.Item3,
game: presence.Item4).ConfigureAwait(false);
}

private (UserStatus, bool, long?, GameModel[]) BuildCurrentStatus()
private (UserStatus, bool, long?, GameModel)? BuildCurrentStatus()
{
var status = Status;
var status = _status;
var statusSince = _statusSince;
var activity = _activity;

GameModel[] gameModels = null;
if (status == null && !activity.IsSpecified)
return null;

GameModel game = null;
// Discord only accepts rich presence over RPC, don't even bother building a payload

if (activity.GetValueOrDefault() != null)
Expand All @@ -476,15 +480,15 @@ await ApiClient.SendStatusUpdateAsync(
gameModel.Type = Activity.Type;
if (Activity is StreamingGame streamGame)
gameModel.StreamUrl = streamGame.Url;
gameModels = new[] { gameModel };
game = gameModel;
}
else if (activity.IsSpecified)
gameModels = new GameModel[0];
game = null;

return (status,
return (status ?? UserStatus.Online,
status == UserStatus.AFK,
statusSince != null ? _statusSince.Value.ToUnixTimeMilliseconds() : (long?)null,
gameModels);
game);
}

private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload)
Expand Down

0 comments on commit 6dd25d2

Please sign in to comment.