Skip to content

Commit

Permalink
fix weird nre
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Sep 10, 2023
1 parent df7ca9d commit b4dcf77
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public RequireReferencedMessageAttribute()
{ }

public override Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
=> Task.FromResult(help || ctx.Message.ReferencedMessage != null);
=> Task.FromResult(help || ctx.Message.ReferencedMessage is not null);
}
10 changes: 5 additions & 5 deletions DisCatSharp/Clients/DiscordClient.Dispatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2295,10 +2295,10 @@ internal async Task OnMessageCreateEventAsync(DiscordMessage message, TransportU
this.PopulateMessageReactionsAndCache(message, author, member);
message.PopulateMentions();

if (message.Channel == null && message.ChannelId == default)
if (message.Channel is null && message.ChannelId == default)
this.Logger.LogWarning(LoggerEvents.WebSocketReceive, "Channel which the last message belongs to is not in cache - cache state might be invalid!");

if (message.ReferencedMessage != null)
if (message.ReferencedMessage is not null)
{
message.ReferencedMessage.Discord = this;
this.PopulateMessageReactionsAndCache(message.ReferencedMessage, referenceAuthor, referenceMember);
Expand Down Expand Up @@ -2336,14 +2336,14 @@ internal async Task OnMessageUpdateEventAsync(DiscordMessage message, TransportU

DiscordMessage oldmsg = null;
if (this.Configuration.MessageCacheSize == 0
|| this.MessageCache == null
|| !this.MessageCache.TryGet(xm => xm.Id == eventMessage.Id && xm.ChannelId == eventMessage.ChannelId, out message))
|| this.MessageCache == null
|| !this.MessageCache.TryGet(xm => xm.Id == eventMessage.Id && xm.ChannelId == eventMessage.ChannelId, out message))
{
message = eventMessage;
this.PopulateMessageReactionsAndCache(message, author, member);
guild = message.Channel?.Guild;

if (message.ReferencedMessage != null)
if (message.ReferencedMessage is not null)
{
message.ReferencedMessage.Discord = this;
this.PopulateMessageReactionsAndCache(message.ReferencedMessage, referenceAuthor, referenceMember);
Expand Down
6 changes: 5 additions & 1 deletion DisCatSharp/Entities/Channel/DiscordChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,11 @@ public override int GetHashCode()
/// <param name="e2">Second channel to compare.</param>
/// <returns>Whether the two channels are equal.</returns>
public static bool operator ==(DiscordChannel? e1, DiscordChannel? e2)
=> (e1 == null && e2 == null) || e1?.Id == e2?.Id; // is that even right??
{
var o1 = e1 as object;
var o2 = e2 as object;
return (o1 != null || o2 == null) && (o1 == null || o2 != null) && ((o1 == null && o2 == null) || e1.Id == e2.Id);
}

/// <summary>
/// Gets whether the two <see cref="DiscordChannel"/> objects are not equal.
Expand Down
11 changes: 6 additions & 5 deletions DisCatSharp/Entities/Message/DiscordMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public DiscordGuild? Guild
/// <summary>
/// Gets the message object for the referenced message
/// </summary>
[JsonProperty("referenced_message", NullValueHandling = NullValueHandling.Ignore)]
[JsonProperty("referenced_message", NullValueHandling = NullValueHandling.Include)]
public DiscordMessage? ReferencedMessage { get; internal set; }

/// <summary>
Expand Down Expand Up @@ -499,13 +499,13 @@ private List<IMention> GetMentions()
{
var mentions = new List<IMention>();

if (this.ReferencedMessage != null && this.MentionedUsersInternal.Contains(this.ReferencedMessage.Author))
if (this.ReferencedMessage is not null && this.MentionedUsersInternal != null && this.MentionedUsersInternal.Contains(this.ReferencedMessage.Author))
mentions.Add(new RepliedUserMention());

if (this.MentionedUsersInternal.Any())
if (this.MentionedUsersInternal != null && this.MentionedUsersInternal.Any())
mentions.AddRange(this.MentionedUsersInternal.Select(m => (IMention)new UserMention(m)));

if (this.MentionedRoleIds.Any())
if (this.MentionedRoleIds != null && this.MentionedRoleIds.Any())
mentions.AddRange(this.MentionedRoleIds.Select(r => (IMention)new RoleMention(r)));

return mentions;
Expand Down Expand Up @@ -543,7 +543,8 @@ internal void PopulateMentions()
if (guild != null)
{
//this._mentionedRoles = this._mentionedRoles.Union(Utilities.GetRoleMentions(this).Select(xid => guild.GetRole(xid))).ToList();
this.MentionedRolesInternal = this.MentionedRolesInternal.Union(this.MentionedRoleIds.Select(xid => guild.GetRole(xid))).ToList();
if (this.MentionedRoleIds != null)
this.MentionedRolesInternal = this.MentionedRolesInternal.Union(this.MentionedRoleIds.Select(xid => guild.GetRole(xid))).ToList();
this.MentionedChannelsInternal = this.MentionedChannelsInternal.Union(Utilities.GetChannelMentions(this).Select(xid => guild.GetChannel(xid))).ToList();
}

Expand Down
8 changes: 4 additions & 4 deletions DisCatSharp/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ internal static IEnumerable<ulong> GetUserMentions(DiscordMessage message)
var regex = new Regex(@"<@!?(\d+)>", RegexOptions.ECMAScript | RegexOptions.Compiled);
var matches = regex.Matches(message.Content);
return from Match match in matches
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
}

/// <summary>
Expand All @@ -254,7 +254,7 @@ internal static IEnumerable<ulong> GetRoleMentions(DiscordMessage message)
var regex = new Regex(@"<@&(\d+)>", RegexOptions.ECMAScript);
var matches = regex.Matches(message.Content);
return from Match match in matches
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
}

/// <summary>
Expand All @@ -267,7 +267,7 @@ internal static IEnumerable<ulong> GetChannelMentions(DiscordMessage message)
var regex = new Regex(@"<#(\d+)>", RegexOptions.ECMAScript);
var matches = regex.Matches(message.Content);
return from Match match in matches
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
select ulong.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
}

/// <summary>
Expand All @@ -280,7 +280,7 @@ internal static IEnumerable<ulong> GetEmojis(DiscordMessage message)
var regex = new Regex(@"<a?:([a-zA-Z0-9_]+):(\d+)>", RegexOptions.ECMAScript);
var matches = regex.Matches(message.Content);
return from Match match in matches
select ulong.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
select ulong.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
}

/// <summary>
Expand Down

0 comments on commit b4dcf77

Please sign in to comment.