Skip to content

Commit

Permalink
Fix SocketGuild.CurrentUser is null
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Oct 28, 2024
1 parent 08c0ac1 commit 75355f7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/Kook.Net.Samples.SimpleBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ async Task MessageReceivedAsync(SocketMessage message,
SocketGuildUser author,
SocketTextChannel channel)
{

// Bot 永远不应该响应自己的消息
if (author.Id == client.CurrentUser?.Id)
return;
Expand Down
3 changes: 3 additions & 0 deletions src/Kook.Net.Rest/API/Rest/ExtendedGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ internal class ExtendedGuild : Guild

[JsonPropertyName("recommend_info")]
public RecommendInfo? RecommendInfo { get; set; }

[JsonPropertyName("user_config")]
public UserConfig? UserConfig { get; set; }
}
9 changes: 9 additions & 0 deletions src/Kook.Net.Rest/API/Rest/UserConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace Kook.API.Rest;

internal class UserConfig
{
[JsonPropertyName("nickname")]
public string? Nickname { get; set; }
}
4 changes: 4 additions & 0 deletions src/Kook.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion src/Kook.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 75355f7

Please sign in to comment.