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;