Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support for setting custom status #2749

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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