From 4dbb5949fc953536da61899af664fc0f76958dd1 Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:37:02 +0300 Subject: [PATCH] [Fix] Forum channel related issues. --- .../Channels/ForumChannelProperties.cs | 2 +- .../Entities/ForumTags/ForumTag.cs | 113 +++++++++--------- .../Entities/ForumTags/ForumTagBuilder.cs | 4 +- .../Entities/ForumTags/ForumTagProperties.cs | 7 +- .../Entities/ForumTags/IForumTag.cs | 8 ++ .../API/Rest/CreateMultipartPostAsync.cs | 4 +- .../Entities/Channels/ForumHelper.cs | 3 +- .../Entities/Channels/ThreadHelper.cs | 3 +- .../Entities/Guilds/GuildHelper.cs | 2 +- 9 files changed, 76 insertions(+), 70 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Channels/ForumChannelProperties.cs b/src/Discord.Net.Core/Entities/Channels/ForumChannelProperties.cs index 1704dc32..eb18d392 100644 --- a/src/Discord.Net.Core/Entities/Channels/ForumChannelProperties.cs +++ b/src/Discord.Net.Core/Entities/Channels/ForumChannelProperties.cs @@ -31,7 +31,7 @@ public class ForumChannelProperties : TextChannelProperties /// /// Gets or sets a collection of tags inside of this forum channel. /// - public Optional> Tags { get; set; } + public Optional> Tags { get; set; } /// /// Gets or sets a new default reaction emoji in this forum channel. diff --git a/src/Discord.Net.Core/Entities/ForumTags/ForumTag.cs b/src/Discord.Net.Core/Entities/ForumTags/ForumTag.cs index 1527356a..87d306d3 100644 --- a/src/Discord.Net.Core/Entities/ForumTags/ForumTag.cs +++ b/src/Discord.Net.Core/Entities/ForumTags/ForumTag.cs @@ -1,67 +1,62 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - #nullable enable -namespace Discord +namespace Discord; + +/// +/// A struct representing a forum channel tag. +/// +public readonly struct ForumTag : ISnowflakeEntity, IForumTag { - /// - /// A struct representing a forum channel tag. - /// - public struct ForumTag : ISnowflakeEntity, IForumTag + /// + public ulong Id { get; } + + /// + public string Name { get; } + + /// + public IEmote? Emoji { get; } + + /// + public bool IsModerated { get; } + + /// + public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); + + internal ForumTag(ulong id, string name, ulong? emojiId = null, string? emojiName = null, bool moderated = false) { - /// - /// Gets the Id of the tag. - /// - public ulong Id { get; } + if (emojiId.HasValue && emojiId.Value != 0) + Emoji = new Emote(emojiId.Value, null, false); + else if (emojiName != null) + Emoji = new Emoji(emojiName); + else + Emoji = null; - /// - public string Name { get; } - - /// - public IEmote? Emoji { get; } - - /// - public bool IsModerated { get; } - - /// - public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); - - internal ForumTag(ulong id, string name, ulong? emojiId = null, string? emojiName = null, bool moderated = false) - { - if (emojiId.HasValue && emojiId.Value != 0) - Emoji = new Emote(emojiId.Value, null, false); - else if (emojiName != null) - Emoji = new Emoji(emojiName); - else - Emoji = null; - - Id = id; - Name = name; - IsModerated = moderated; - } - - public override int GetHashCode() => (Id, Name, Emoji, IsModerated).GetHashCode(); - - public override bool Equals(object? obj) - => obj is ForumTag tag && Equals(tag); - - /// - /// Gets whether supplied tag is equals to the current one. - /// - public bool Equals(ForumTag tag) - => Id == tag.Id && - Name == tag.Name && - (Emoji is Emoji emoji && tag.Emoji is Emoji otherEmoji && emoji.Equals(otherEmoji) || - Emoji is Emote emote && tag.Emoji is Emote otherEmote && emote.Equals(otherEmote)) && - IsModerated == tag.IsModerated; - - public static bool operator ==(ForumTag? left, ForumTag? right) - => left?.Equals(right) ?? right is null; - - public static bool operator !=(ForumTag? left, ForumTag? right) => !(left == right); + Id = id; + Name = name; + IsModerated = moderated; } + + public override int GetHashCode() => (Id, Name, Emoji, IsModerated).GetHashCode(); + + public override bool Equals(object? obj) + => obj is ForumTag tag && Equals(tag); + + /// + /// Gets whether supplied tag is equals to the current one. + /// + public bool Equals(ForumTag tag) + => Id == tag.Id && + Name == tag.Name && + (Emoji is Emoji emoji && tag.Emoji is Emoji otherEmoji && emoji.Equals(otherEmoji) || + Emoji is Emote emote && tag.Emoji is Emote otherEmote && emote.Equals(otherEmote)) && + IsModerated == tag.IsModerated; + + public static bool operator ==(ForumTag? left, ForumTag? right) + => left?.Equals(right) ?? right is null; + + public static bool operator !=(ForumTag? left, ForumTag? right) => !(left == right); + + /// + readonly ulong? IForumTag.Id => Id; } diff --git a/src/Discord.Net.Core/Entities/ForumTags/ForumTagBuilder.cs b/src/Discord.Net.Core/Entities/ForumTags/ForumTagBuilder.cs index 8ba3eff6..f0a21887 100644 --- a/src/Discord.Net.Core/Entities/ForumTags/ForumTagBuilder.cs +++ b/src/Discord.Net.Core/Entities/ForumTags/ForumTagBuilder.cs @@ -124,8 +124,8 @@ public class ForumTagBuilder public ForumTagProperties Build() { if (_name is null) - throw new ArgumentNullException(nameof(Name), "Name must be set to build the tag"); - return new ForumTagProperties(_name!, _emoji, _moderated); + throw new ArgumentNullException(nameof(Name), "Name must be set to build the tag."); + return new ForumTagProperties(_id, _name, _emoji, _moderated); } /// diff --git a/src/Discord.Net.Core/Entities/ForumTags/ForumTagProperties.cs b/src/Discord.Net.Core/Entities/ForumTags/ForumTagProperties.cs index 6ded4920..3dd7bdab 100644 --- a/src/Discord.Net.Core/Entities/ForumTags/ForumTagProperties.cs +++ b/src/Discord.Net.Core/Entities/ForumTags/ForumTagProperties.cs @@ -7,7 +7,7 @@ public class ForumTagProperties : IForumTag /// /// Gets the Id of the tag. /// - public ulong Id { get; } + public ulong? Id { get; } /// public string Name { get; } @@ -18,11 +18,12 @@ public class ForumTagProperties : IForumTag /// public bool IsModerated { get; } - internal ForumTagProperties(string name, IEmote? emoji = null, bool isMmoderated = false) + internal ForumTagProperties(ulong? id, string name, IEmote? emoji = null, bool isModerated = false) { + Id = id; Name = name; Emoji = emoji; - IsModerated = isMmoderated; + IsModerated = isModerated; } public override int GetHashCode() => (Id, Name, Emoji, IsModerated).GetHashCode(); diff --git a/src/Discord.Net.Core/Entities/ForumTags/IForumTag.cs b/src/Discord.Net.Core/Entities/ForumTags/IForumTag.cs index 8b8b866b..4e10ff31 100644 --- a/src/Discord.Net.Core/Entities/ForumTags/IForumTag.cs +++ b/src/Discord.Net.Core/Entities/ForumTags/IForumTag.cs @@ -7,6 +7,14 @@ namespace Discord; /// public interface IForumTag { + /// + /// Gets the Id of the tag. + /// + /// + /// This property may be if the object is . + /// + ulong? Id { get; } + /// /// Gets the name of the tag. /// diff --git a/src/Discord.Net.Rest/API/Rest/CreateMultipartPostAsync.cs b/src/Discord.Net.Rest/API/Rest/CreateMultipartPostAsync.cs index bb10a468..14e5f538 100644 --- a/src/Discord.Net.Rest/API/Rest/CreateMultipartPostAsync.cs +++ b/src/Discord.Net.Rest/API/Rest/CreateMultipartPostAsync.cs @@ -46,6 +46,8 @@ namespace Discord.API.Rest if (Slowmode.IsSpecified) payload["rate_limit_per_user"] = Slowmode.Value; + if (TagIds.IsSpecified) + payload["applied_tags"] = TagIds.Value; // message if (Content.IsSpecified) @@ -60,8 +62,6 @@ namespace Discord.API.Rest message["sticker_ids"] = Stickers.Value; if (Flags.IsSpecified) message["flags"] = Flags.Value; - if (TagIds.IsSpecified) - message["applied_tags"] = TagIds.Value; List attachments = new(); diff --git a/src/Discord.Net.Rest/Entities/Channels/ForumHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ForumHelper.cs index 6ad0d49f..5f239890 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ForumHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ForumHelper.cs @@ -15,7 +15,7 @@ internal static class ForumHelper var args = new ForumChannelProperties(); func(args); - Preconditions.AtMost(args.Tags.IsSpecified ? args.Tags.Value.Count() : 0, 5, nameof(args.Tags), "Forum channel can have max 20 tags."); + Preconditions.AtMost(args.Tags.IsSpecified ? args.Tags.Value.Count() : 0, 20, nameof(args.Tags), "Forum channel can have max 20 tags."); var apiArgs = new API.Rest.ModifyForumChannelParams() { @@ -36,6 +36,7 @@ internal static class ForumHelper Tags = args.Tags.IsSpecified ? args.Tags.Value.Select(tag => new API.ModifyForumTagParams { + Id = tag.Id ?? Optional.Unspecified, Name = tag.Name, EmojiId = tag.Emoji is Emote emote ? emote.Id diff --git a/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs index 4c9a50bc..0108d3dc 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ThreadHelper.cs @@ -229,7 +229,8 @@ namespace Discord.Rest MessageComponent = components?.Components?.Any() ?? false ? components.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional.Unspecified, Slowmode = slowmode, Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional.Unspecified, - Title = title + Title = title, + TagIds = tagIds }; var model = await client.ApiClient.CreatePostAsync(channel.Id, args, options); diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 0314e5e6..cae86f13 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -376,7 +376,7 @@ namespace Discord.Rest AvailableTags = props.Tags.GetValueOrDefault(Array.Empty()).Select( x => new ModifyForumTagParams { - Id = x.Id, + Id = x.Id ?? Optional.Unspecified, Name = x.Name, EmojiId = x.Emoji is Emote emote ? emote.Id