From b2820d5b65105bf234030d67165f0effef80b17c Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:32:23 +0300 Subject: [PATCH] [Feature] Support for setting custom status (#2749) --- .../Entities/Activities/CustomStatusGame.cs | 14 ++++++++++++++ src/Discord.Net.WebSocket/BaseSocketClient.cs | 10 ++++++++++ src/Discord.Net.WebSocket/DiscordShardedClient.cs | 8 ++++++++ src/Discord.Net.WebSocket/DiscordSocketClient.cs | 14 +++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/Entities/Activities/CustomStatusGame.cs b/src/Discord.Net.Core/Entities/Activities/CustomStatusGame.cs index 7bd2664a2f..7a0cb064d8 100644 --- a/src/Discord.Net.Core/Entities/Activities/CustomStatusGame.cs +++ b/src/Discord.Net.Core/Entities/Activities/CustomStatusGame.cs @@ -11,6 +11,20 @@ public class CustomStatusGame : Game { internal CustomStatusGame() { } + /// + /// Creates a new custom status activity. + /// + /// + /// Bots can't set custom status emoji. + /// + /// The string displayed as bot's custom status. + public CustomStatusGame(string state) + { + Name = "Custom Status"; + State = state; + Type = ActivityType.CustomStatus; + } + /// /// Gets the emote, if it is set. /// diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.cs b/src/Discord.Net.WebSocket/BaseSocketClient.cs index 4d7b08c1b2..beaf67e5df 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.cs @@ -227,6 +227,16 @@ private static DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config /// A task that represents the asynchronous set operation. /// public abstract Task SetActivityAsync(IActivity activity); + + /// + /// Sets the custom status of the logged-in user. + /// + /// The string that will be displayed as status. + /// + /// A task that represents the asynchronous set operation. + /// + public abstract Task SetCustomStatusAsync(string status); + /// /// Attempts to download users into the user cache for the selected guilds. /// diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 7b58b9149f..fdccee1fc5 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -412,6 +412,14 @@ public override async Task SetActivityAsync(IActivity activity) await _shards[i].SetActivityAsync(activity).ConfigureAwait(false); } + /// + 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); diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index bc018c56c5..d589b1373f 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -694,6 +694,7 @@ public override async Task SetGameAsync(string name, string streamUrl = null, Ac Activity = null; await SendStatusAsync().ConfigureAwait(false); } + /// public override async Task SetActivityAsync(IActivity activity) { @@ -701,11 +702,20 @@ public override async Task SetActivityAsync(IActivity activity) await SendStatusAsync().ConfigureAwait(false); } + /// + 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); @@ -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)