From 75355f75ba61d81547a8a70cbb1df10075633678 Mon Sep 17 00:00:00 2001 From: Gehongyan Date: Mon, 28 Oct 2024 22:46:44 +0800 Subject: [PATCH] Fix SocketGuild.CurrentUser is null --- samples/Kook.Net.Samples.SimpleBot/Program.cs | 1 + src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs | 3 +++ src/Kook.Net.Rest/API/Rest/UserConfig.cs | 9 ++++++++ .../Entities/Guilds/RestGuild.cs | 4 ++++ .../Entities/Guilds/SocketGuild.cs | 21 ++++++++++++++++++- .../Entities/Users/SocketGuildUser.cs | 15 +++++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Kook.Net.Rest/API/Rest/UserConfig.cs diff --git a/samples/Kook.Net.Samples.SimpleBot/Program.cs b/samples/Kook.Net.Samples.SimpleBot/Program.cs index b8e53833..0a128ed5 100644 --- a/samples/Kook.Net.Samples.SimpleBot/Program.cs +++ b/samples/Kook.Net.Samples.SimpleBot/Program.cs @@ -136,6 +136,7 @@ async Task MessageReceivedAsync(SocketMessage message, SocketGuildUser author, SocketTextChannel channel) { + // Bot 永远不应该响应自己的消息 if (author.Id == client.CurrentUser?.Id) return; diff --git a/src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs b/src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs index a6452995..b720125a 100644 --- a/src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs +++ b/src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs @@ -26,4 +26,7 @@ internal class ExtendedGuild : Guild [JsonPropertyName("recommend_info")] public RecommendInfo? RecommendInfo { get; set; } + + [JsonPropertyName("user_config")] + public UserConfig? UserConfig { get; set; } } diff --git a/src/Kook.Net.Rest/API/Rest/UserConfig.cs b/src/Kook.Net.Rest/API/Rest/UserConfig.cs new file mode 100644 index 00000000..da8b62f5 --- /dev/null +++ b/src/Kook.Net.Rest/API/Rest/UserConfig.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace Kook.API.Rest; + +internal class UserConfig +{ + [JsonPropertyName("nickname")] + public string? Nickname { get; set; } +} diff --git a/src/Kook.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Kook.Net.Rest/Entities/Guilds/RestGuild.cs index 08cf3352..054f55d7 100644 --- a/src/Kook.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Kook.Net.Rest/Entities/Guilds/RestGuild.cs @@ -217,6 +217,10 @@ internal void Update(ExtendedModel model) Status = model.Status; AutoDeleteTime = model.AutoDeleteTime; RecommendInfo = model.RecommendInfo?.ToEntity(); + if (Kook.CurrentUser is null) + throw new InvalidOperationException("The current user is not set well via login."); + if (model.UserConfig is { } userConfig) + CurrentUserNickname = userConfig.Nickname == Kook.CurrentUser.Username ? null : userConfig.Nickname; } internal void Update(Model model) diff --git a/src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs index ed0c9a5c..15b1f536 100644 --- a/src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -364,6 +364,7 @@ internal void Update(ClientState state, ExtendedModel model) Status = model.Status; AutoDeleteTime = model.AutoDeleteTime; RecommendInfo = model.RecommendInfo?.ToEntity(); + AddOrUpdateCurrentUser(model); } internal void Update(ClientState state, Model model) @@ -664,9 +665,27 @@ internal SocketGuildUser AddOrUpdateUser(MemberModel model) return member; } + internal SocketGuildUser AddOrUpdateCurrentUser(ExtendedModel model) + { + if (Kook.CurrentUser is not null + && _members.TryGetValue(Kook.CurrentUser.Id, out SocketGuildUser? member)) + { + member.Update(Kook.State, model); + } + else + { + member = SocketGuildUser.Create(this, Kook.State, model); + member.GlobalUser.AddRef(); + _members[member.Id] = member; + } + + return member; + } + internal SocketGuildUser AddOrUpdateCurrentUser(RichModel model) { - if (Kook.CurrentUser is not null && _members.TryGetValue(Kook.CurrentUser.Id, out SocketGuildUser? member)) + if (Kook.CurrentUser is not null + && _members.TryGetValue(Kook.CurrentUser.Id, out SocketGuildUser? member)) { member.Update(Kook.State, model); } diff --git a/src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs b/src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs index 18741a0d..e4d288d5 100644 --- a/src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs +++ b/src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs @@ -224,6 +224,15 @@ internal static SocketGuildUser Create(SocketGuild guild, ClientState state, Mem return entity; } + internal static SocketGuildUser Create(SocketGuild guild, ClientState state, ExtendedGuild model) + { + if (guild.Kook.CurrentUser is null) + throw new InvalidOperationException("The current user is not set well via login."); + SocketGuildUser entity = new(guild, guild.Kook.CurrentUser.GlobalUser); + entity.Update(state, model); + return entity; + } + internal static SocketGuildUser Create(SocketGuild guild, ClientState state, RichGuild model) { if (guild.Kook.CurrentUser is null) @@ -248,6 +257,12 @@ internal void Update(ClientState state, MemberModel model) _roleIds = [..model.Roles]; } + internal void Update(ClientState state, ExtendedGuild guildModel) + { + if (guildModel.UserConfig is { } userConfig) + Nickname = userConfig.Nickname == Username ? null : userConfig.Nickname; + } + internal void Update(ClientState state, RichGuild guildModel) { Nickname = guildModel.CurrentUserNickname == Username ? null : guildModel.CurrentUserNickname;