Skip to content

Commit

Permalink
fix: NullReferenceException in pin/unpin audit logs (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeKzor authored Mar 10, 2021
1 parent 6ac5ea1 commit f794163
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ private MessagePinAuditLogData(ulong messageId, ulong channelId, IUser user)

internal static MessagePinAuditLogData Create(BaseDiscordClient discord, Model log, EntryModel entry)
{
var userInfo = log.Users.FirstOrDefault(x => x.Id == entry.TargetId);
return new MessagePinAuditLogData(entry.Options.MessageId.Value, entry.Options.ChannelId.Value, RestUser.Create(discord, userInfo));
RestUser user = null;
if (entry.TargetId.HasValue)
{
var userInfo = log.Users.FirstOrDefault(x => x.Id == entry.TargetId);
user = RestUser.Create(discord, userInfo);
}

return new MessagePinAuditLogData(entry.Options.MessageId.Value, entry.Options.ChannelId.Value, user);
}

/// <summary>
Expand All @@ -38,10 +44,10 @@ internal static MessagePinAuditLogData Create(BaseDiscordClient discord, Model l
/// </returns>
public ulong ChannelId { get; }
/// <summary>
/// Gets the user of the message that was pinned.
/// Gets the user of the message that was pinned if available.
/// </summary>
/// <returns>
/// A user object representing the user that created the pinned message.
/// A user object representing the user that created the pinned message or <see langword="null"/>.
/// </returns>
public IUser Target { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ private MessageUnpinAuditLogData(ulong messageId, ulong channelId, IUser user)

internal static MessageUnpinAuditLogData Create(BaseDiscordClient discord, Model log, EntryModel entry)
{
var userInfo = log.Users.FirstOrDefault(x => x.Id == entry.TargetId);
return new MessageUnpinAuditLogData(entry.Options.MessageId.Value, entry.Options.ChannelId.Value, RestUser.Create(discord, userInfo));
RestUser user = null;
if (entry.TargetId.HasValue)
{
var userInfo = log.Users.FirstOrDefault(x => x.Id == entry.TargetId);
user = RestUser.Create(discord, userInfo);
}

return new MessageUnpinAuditLogData(entry.Options.MessageId.Value, entry.Options.ChannelId.Value, user);
}

/// <summary>
Expand All @@ -38,10 +44,10 @@ internal static MessageUnpinAuditLogData Create(BaseDiscordClient discord, Model
/// </returns>
public ulong ChannelId { get; }
/// <summary>
/// Gets the user of the message that was unpinned.
/// Gets the user of the message that was unpinned if available.
/// </summary>
/// <returns>
/// A user object representing the user that created the unpinned message.
/// A user object representing the user that created the unpinned message or <see langword="null"/>.
/// </returns>
public IUser Target { get; }
}
Expand Down

0 comments on commit f794163

Please sign in to comment.