Browse Source

Add `Equals`, `GetHashCode`, `==` & `!=` operators to embed

pull/2347/head
Misha133 3 years ago
parent
commit
3cf7e7a7cd
8 changed files with 220 additions and 0 deletions
  1. +36
    -0
      src/Discord.Net.Core/Entities/Messages/Embed.cs
  2. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedAuthor.cs
  3. +82
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs
  4. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedField.cs
  5. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedFooter.cs
  6. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedImage.cs
  7. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedThumbnail.cs
  8. +17
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedVideo.cs

+ 36
- 0
src/Discord.Net.Core/Entities/Messages/Embed.cs View File

@@ -94,5 +94,41 @@ namespace Discord
/// </summary>
public override string ToString() => Title;
private string DebuggerDisplay => $"{Title} ({Type})";

public static bool operator ==(Embed left, Embed right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(Embed left, Embed right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as Embed);

public bool Equals(Embed embed)
=> GetHashCode() == embed.GetHashCode();

public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(Type);
hashCode.Add(Title);
hashCode.Add(Description);
hashCode.Add(Timestamp);
hashCode.Add(Color);
hashCode.Add(Image);
hashCode.Add(Video);
hashCode.Add(Author);
hashCode.Add(Footer);
hashCode.Add(Provider);
hashCode.Add(Thumbnail);

foreach (var field in Fields)
{
hashCode.Add(field);
}

return hashCode.ToHashCode();
}
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedAuthor.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -41,5 +42,21 @@ namespace Discord
///
/// </returns>
public override string ToString() => Name;

public static bool operator ==(EmbedAuthor? left, EmbedAuthor? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedAuthor? left, EmbedAuthor? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedAuthor?);

public bool Equals(EmbedAuthor embedAuthor)
=> GetHashCode() == embedAuthor.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Name, Url, IconUrl);
}
}

+ 82
- 0
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -481,6 +481,40 @@ namespace Discord

return new Embed(EmbedType.Rich, Title, Description, Url, Timestamp, Color, _image, null, Author?.Build(), Footer?.Build(), null, _thumbnail, fields.ToImmutable());
}

public static bool operator ==(EmbedBuilder left, EmbedBuilder right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedBuilder left, EmbedBuilder right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedBuilder);

public bool Equals(EmbedBuilder embedBuilder)
=> GetHashCode() == embedBuilder.GetHashCode();

public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(_title);
hashCode.Add(_description);
hashCode.Add(_image);
hashCode.Add(_thumbnail);
hashCode.Add(Timestamp);
hashCode.Add(Color);
hashCode.Add(Author);
hashCode.Add(Footer);
hashCode.Add(Url);

foreach (var field in _fields)
{
hashCode.Add(field);
}

return hashCode.ToHashCode();
}
}

/// <summary>
@@ -597,6 +631,22 @@ namespace Discord
/// </exception>
public EmbedField Build()
=> new EmbedField(Name, Value.ToString(), IsInline);

public static bool operator ==(EmbedFieldBuilder left, EmbedFieldBuilder right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedFieldBuilder left, EmbedFieldBuilder right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedFieldBuilder);

public bool Equals(EmbedFieldBuilder embedFieldBuilder)
=> GetHashCode() == embedFieldBuilder.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Name, Value, IsInline);
}

/// <summary>
@@ -697,6 +747,22 @@ namespace Discord
/// </returns>
public EmbedAuthor Build()
=> new EmbedAuthor(Name, Url, IconUrl, null);

public static bool operator ==(EmbedAuthorBuilder left, EmbedAuthorBuilder right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedAuthorBuilder left, EmbedAuthorBuilder right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedAuthorBuilder);

public bool Equals(EmbedAuthorBuilder embedAuthorBuilder)
=> GetHashCode() == embedAuthorBuilder.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Name, Url, IconUrl);
}

/// <summary>
@@ -777,5 +843,21 @@ namespace Discord
/// </returns>
public EmbedFooter Build()
=> new EmbedFooter(Text, IconUrl, null);

public static bool operator ==(EmbedFooterBuilder left, EmbedFooterBuilder right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedFooterBuilder left, EmbedFooterBuilder right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedFooterBuilder);

public bool Equals(EmbedFooterBuilder embedFooterBuilder)
=> GetHashCode() == embedFooterBuilder.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Text, IconUrl);
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedField.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -36,5 +37,21 @@ namespace Discord
/// A string that resolves to <see cref="EmbedField.Name"/>.
/// </returns>
public override string ToString() => Name;

public static bool operator ==(EmbedField? left, EmbedField? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedField? left, EmbedField? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedField?);

public bool Equals(EmbedField embedField)
=> GetHashCode() == embedField.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Name, Value, Inline);
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedFooter.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -43,5 +44,21 @@ namespace Discord
/// A string that resolves to <see cref="Discord.EmbedFooter.Text"/>.
/// </returns>
public override string ToString() => Text;

public static bool operator ==(EmbedFooter? left, EmbedFooter? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedFooter? left, EmbedFooter? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedFooter?);

public bool Equals(EmbedFooter embedFooter)
=> GetHashCode() == embedFooter.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Text, IconUrl, ProxyUrl);
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedImage.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -53,5 +54,21 @@ namespace Discord
/// A string that resolves to <see cref="Discord.EmbedImage.Url"/> .
/// </returns>
public override string ToString() => Url;

public static bool operator ==(EmbedImage? left, EmbedImage? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedImage? left, EmbedImage? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedImage?);

public bool Equals(EmbedImage embedImage)
=> GetHashCode() == embedImage.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Height, Width, Url, ProxyUrl);
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedThumbnail.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -53,5 +54,21 @@ namespace Discord
/// A string that resolves to <see cref="Discord.EmbedThumbnail.Url" />.
/// </returns>
public override string ToString() => Url;

public static bool operator ==(EmbedThumbnail? left, EmbedThumbnail? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedThumbnail? left, EmbedThumbnail? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedThumbnail?);

public bool Equals(EmbedThumbnail embedThumbnail)
=> GetHashCode() == embedThumbnail.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Width, Height, Url, ProxyUrl);
}
}

+ 17
- 0
src/Discord.Net.Core/Entities/Messages/EmbedVideo.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace Discord
@@ -47,5 +48,21 @@ namespace Discord
/// A string that resolves to <see cref="Url"/>.
/// </returns>
public override string ToString() => Url;

public static bool operator ==(EmbedVideo? left, EmbedVideo? right)
=> left is null ? right is null
: left.Equals(right);

public static bool operator !=(EmbedVideo? left, EmbedVideo? right)
=> !(left == right);

public override bool Equals(object obj)
=> obj is not null && GetType() == obj.GetType() && Equals(obj as EmbedVideo?);

public bool Equals(EmbedVideo embedVideo)
=> GetHashCode() == embedVideo.GetHashCode();

public override int GetHashCode()
=> HashCode.Combine(Width, Height, Url);
}
}

Loading…
Cancel
Save