Skip to content

Commit

Permalink
[Feature] Support for setting custom status (#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Aug 10, 2023
1 parent b1c8326 commit b2820d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Entities/Activities/CustomStatusGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public class CustomStatusGame : Game
{
internal CustomStatusGame() { }

/// <summary>
/// Creates a new custom status activity.
/// </summary>
/// <remarks>
/// Bots can't set custom status emoji.
/// </remarks>
/// <param name="state">The string displayed as bot's custom status.</param>
public CustomStatusGame(string state)
{
Name = "Custom Status";
State = state;
Type = ActivityType.CustomStatus;
}

/// <summary>
/// Gets the emote, if it is set.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Discord.Net.WebSocket/BaseSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ private static DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config
/// A task that represents the asynchronous set operation.
/// </returns>
public abstract Task SetActivityAsync(IActivity activity);

/// <summary>
/// Sets the custom status of the logged-in user.
/// </summary>
/// <param name="status">The string that will be displayed as status.</param>
/// <returns>
/// A task that represents the asynchronous set operation.
/// </returns>
public abstract Task SetCustomStatusAsync(string status);

/// <summary>
/// Attempts to download users into the user cache for the selected guilds.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Discord.Net.WebSocket/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ public override async Task SetActivityAsync(IActivity activity)
await _shards[i].SetActivityAsync(activity).ConfigureAwait(false);
}

/// <inheritdoc />
public override async Task SetCustomStatusAsync(string status)
{
var statusGame = new CustomStatusGame(status);
for (int i = 0; i < _shards.Length; i++)
await _shards[i].SetActivityAsync(statusGame).ConfigureAwait(false);
}

private void RegisterEvents(DiscordSocketClient client, bool isPrimary)
{
client.Log += (msg) => _logEvent.InvokeAsync(msg);
Expand Down
14 changes: 13 additions & 1 deletion src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,18 +694,28 @@ public override async Task SetGameAsync(string name, string streamUrl = null, Ac
Activity = null;
await SendStatusAsync().ConfigureAwait(false);
}

/// <inheritdoc />
public override async Task SetActivityAsync(IActivity activity)
{
Activity = activity;
await SendStatusAsync().ConfigureAwait(false);
}

/// <inheritdoc />
public override async Task SetCustomStatusAsync(string status)
{
var statusGame = new CustomStatusGame(status);
await SetActivityAsync(statusGame);
}

private async Task SendStatusAsync()
{
if (CurrentUser == null)
return;
var activities = _activity.IsSpecified ? ImmutableList.Create(_activity.Value) : null;
var activities = _activity.IsSpecified
? ImmutableList.Create(_activity.Value)
: null;
CurrentUser.Presence = new SocketPresence(Status, null, activities);

var presence = BuildCurrentStatus() ?? (UserStatus.Online, false, null, null);
Expand Down Expand Up @@ -738,6 +748,8 @@ await ApiClient.SendPresenceUpdateAsync(
gameModel.Type = Activity.Type;
if (Activity is StreamingGame streamGame)
gameModel.StreamUrl = streamGame.Url;
if (Activity is CustomStatusGame customStatus)
gameModel.State = customStatus.State;
game = gameModel;
}
else if (activity.IsSpecified)
Expand Down

0 comments on commit b2820d5

Please sign in to comment.