Skip to content

Commit

Permalink
fix user cache nre
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 7, 2023
1 parent c5062a1 commit 2b70147
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DisCatSharp.CommandsNext/CommandsNextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ public CommandContext CreateFakeContext(DiscordUser actor, DiscordChannel channe
{
if (msg.Channel.Guild != null)
{
mentionedUsers = Utilities.GetUserMentions(msg).Select(xid => msg.Channel.Guild.MembersInternal.TryGetValue(xid, out var member) ? member : null).Cast<DiscordUser>().ToList();
mentionedUsers = Utilities.GetUserMentions(msg).Select(xid => msg.Channel.Guild.Members.TryGetValue(xid, out var member) ? member : null).Cast<DiscordUser>().ToList();
mentionedRoles = Utilities.GetRoleMentions(msg).Select(xid => msg.Channel.Guild.GetRole(xid)).ToList();
mentionedChannels = Utilities.GetChannelMentions(msg).Select(xid => msg.Channel.Guild.GetChannel(xid)).ToList();
}
Expand Down
23 changes: 11 additions & 12 deletions DisCatSharp/Clients/DiscordClient.Dispatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3424,13 +3424,14 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
{
Discord = this
};
this.UserCache.AddOrUpdate(usr.Id, usr, (old, @new) => @new);

interaction.ChannelId = channelId;
interaction.GuildId = guildId;
interaction.Discord = this;
interaction.Data.Discord = this;

if (member != null)
if (member is not null)
{
usr = new DiscordMember(member)
{
Expand All @@ -3439,23 +3440,21 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
if (interaction.Guild is not null)
this.UpdateUser(usr, guildId, interaction.Guild, member);
}
else
this.UserCache.AddOrUpdate(usr.Id, usr, (old, @new) => @new);

usr.Locale = interaction.Locale;
interaction.User = usr;

var resolved = interaction.Data.Resolved;
if (resolved != null)
if (resolved is not null)
{
if (resolved.Users != null)
if (resolved.Users is not null)
foreach (var c in resolved.Users)
{
c.Value.Discord = this;
this.UserCache.AddOrUpdate(c.Value.Id, c.Value, (old, @new) => @new);
}

if (resolved.Members != null)
if (resolved.Members is not null)
foreach (var c in resolved.Members)
{
c.Value.Discord = this;
Expand All @@ -3465,7 +3464,7 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
this.UserCache.AddOrUpdate(c.Value.User.Id, c.Value.User, (old, @new) => @new);
}

if (resolved.Channels != null)
if (resolved.Channels is not null)
foreach (var c in resolved.Channels)
{
c.Value.Discord = this;
Expand All @@ -3483,7 +3482,7 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
}
}

if (resolved.Roles != null)
if (resolved.Roles is not null)
foreach (var c in resolved.Roles)
{
c.Value.Discord = this;
Expand All @@ -3492,7 +3491,7 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
c.Value.GuildId = guildId.Value;
}

if (resolved.Messages != null)
if (resolved.Messages is not null)
foreach (var m in resolved.Messages)
{
m.Value.Discord = this;
Expand All @@ -3501,14 +3500,14 @@ internal async Task OnInteractionCreateAsync(ulong? guildId, ulong channelId, Tr
m.Value.GuildId = guildId.Value;
}

if (resolved.Attachments != null)
if (resolved.Attachments is not null)
foreach (var a in resolved.Attachments)
a.Value.Discord = this;
}

if (interaction.Type is InteractionType.Component || interaction.Type is InteractionType.ModalSubmit)
if (interaction.Type is InteractionType.Component or InteractionType.ModalSubmit)
{
if (interaction.Message != null)
if (interaction.Message is not null)
{
interaction.Message.Discord = this;
interaction.Message.ChannelId = interaction.ChannelId;
Expand Down

0 comments on commit 2b70147

Please sign in to comment.