From eb8dcc40ff4923b77186209d11493714cdcefb74 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 16 Dec 2024 12:24:25 +0100 Subject: [PATCH] chore: more cleanup and more fixes --- .../Entities/Core/DisCatSharpBuilder.cs | 53 ++++++++++++++++-- .../DiscordFollowupMessageBuilder.cs | 41 +------------- .../DiscordInteractionResponseBuilder.cs | 54 +++---------------- .../Entities/Message/DiscordMessage.cs | 4 +- .../Entities/Message/DiscordMessageBuilder.cs | 21 +------- .../Entities/Webhook/DiscordWebhookBuilder.cs | 30 ++--------- DisCatSharp/Net/Rest/DiscordApiClient.cs | 2 +- 7 files changed, 66 insertions(+), 139 deletions(-) diff --git a/DisCatSharp/Entities/Core/DisCatSharpBuilder.cs b/DisCatSharp/Entities/Core/DisCatSharpBuilder.cs index dc104a3d2d..bee7fa33e3 100644 --- a/DisCatSharp/Entities/Core/DisCatSharpBuilder.cs +++ b/DisCatSharp/Entities/Core/DisCatSharpBuilder.cs @@ -77,6 +77,48 @@ public IReadOnlyList Attachments public IReadOnlyList Files => this.FilesInternal; + /// + /// Gets the Allowed Mentions for the message to be sent. + /// + public List? Mentions { get; internal set; } + + /// + /// Whether to send as voice message. + /// You can't use that on your own, it needs DisCatSharp.Experimental. + /// + internal bool IsVoiceMessage + { + get => this.VOICE_MSG; + set + { + this.VOICE_MSG = value; + this.FlagsChanged = true; + } + } + + /// + /// Whether to send as voice message. + /// + private bool VOICE_MSG { get; set; } + + /// + /// Whether to send as silent message. + /// + public bool NotificationsSuppressed + { + get => this.NOTIFICATIONS_SUPPRESSED; + set + { + this.NOTIFICATIONS_SUPPRESSED = value; + this.FlagsChanged = true; + } + } + + /// + /// Whether to send as silent message. + /// + private bool NOTIFICATIONS_SUPPRESSED { get; set; } + /// /// Whether this message should be using UI Kit. /// @@ -100,10 +142,10 @@ public bool IsUIKit /// public bool EmbedsSuppressed { - get => this.EMB_SUP; + get => this.EMBEDS_SUPPRESSED; set { - this.EMB_SUP = value; + this.EMBEDS_SUPPRESSED = value; this.FlagsChanged = true; } } @@ -111,7 +153,7 @@ public bool EmbedsSuppressed /// /// Whether embeds are suppressed. /// - private bool EMB_SUP { get; set; } + private bool EMBEDS_SUPPRESSED { get; set; } /// /// Clears the components on this builder. @@ -129,7 +171,12 @@ public virtual void Clear() this.EmbedsInternal.Clear(); this.AttachmentsInternal.Clear(); this.ComponentsInternal.Clear(); + this.IsVoiceMessage = false; + this.IsUIKit = false; + this.EmbedsSuppressed = false; + this.NotificationsSuppressed = false; this.FlagsChanged = false; + this.Mentions = null; } /// diff --git a/DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs b/DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs index 0d867f2fa8..dbe8d09c4e 100644 --- a/DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs +++ b/DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs @@ -32,42 +32,6 @@ public bool IsEphemeral private bool EPH { get; set; } - /// - /// Whether to send as voice message. - /// You can't use that on your own, it needs DisCatSharp.Experimental. - /// - internal bool IsVoiceMessage - { - get => this.VOICE_MSG; - set - { - this.VOICE_MSG = value; - this.FlagsChanged = true; - } - } - - private bool VOICE_MSG { get; set; } - - /// - /// Whether to send as silent message. - /// - public bool NotificationsSuppressed - { - get => this.NOTI_SUP; - set - { - this.NOTI_SUP = value; - this.FlagsChanged = true; - } - } - - private bool NOTI_SUP { get; set; } - - /// - /// Mentions to send on this followup message. - /// - public List? Mentions { get; private set; } - /// /// Gets the poll for this message. /// @@ -180,6 +144,7 @@ public DiscordFollowupMessageBuilder WithContent(string content) /// The builder to chain calls with. public DiscordFollowupMessageBuilder AddEmbed(DiscordEmbed embed) { + ArgumentNullException.ThrowIfNull(embed, nameof(embed)); this.EmbedsInternal.Add(embed); return this; } @@ -309,7 +274,6 @@ public DiscordFollowupMessageBuilder AddMentions(IEnumerable mentions) /// public DiscordFollowupMessageBuilder AsEphemeral() { - this.FlagsChanged = true; this.IsEphemeral = true; return this; } @@ -319,7 +283,6 @@ public DiscordFollowupMessageBuilder AsEphemeral() /// public DiscordFollowupMessageBuilder SuppressEmbeds() { - this.FlagsChanged = true; this.EmbedsSuppressed = true; return this; } @@ -329,7 +292,6 @@ public DiscordFollowupMessageBuilder SuppressEmbeds() /// internal DiscordFollowupMessageBuilder AsVoiceMessage(bool asVoiceMessage = true) { - this.FlagsChanged = true; this.IsVoiceMessage = asVoiceMessage; return this; } @@ -339,7 +301,6 @@ internal DiscordFollowupMessageBuilder AsVoiceMessage(bool asVoiceMessage = true /// public DiscordFollowupMessageBuilder AsSilentMessage() { - this.FlagsChanged = true; this.NotificationsSuppressed = true; return this; } diff --git a/DisCatSharp/Entities/Interaction/DiscordInteractionResponseBuilder.cs b/DisCatSharp/Entities/Interaction/DiscordInteractionResponseBuilder.cs index ed8818541a..883466e641 100644 --- a/DisCatSharp/Entities/Interaction/DiscordInteractionResponseBuilder.cs +++ b/DisCatSharp/Entities/Interaction/DiscordInteractionResponseBuilder.cs @@ -25,12 +25,16 @@ public DiscordInteractionResponseBuilder() /// . /// /// The builder to copy. - public DiscordInteractionResponseBuilder(DiscordMessageBuilder builder) + public DiscordInteractionResponseBuilder(DisCatSharpBuilder builder) { this.Content = builder.Content; this.Mentions = builder.Mentions; this.EmbedsInternal.AddRange(builder.Embeds); this.ComponentsInternal.AddRange(builder.Components); + this.EmbedsSuppressed = builder.EmbedsSuppressed; + this.IsUIKit = builder.IsUIKit; + this.FilesInternal.AddRange(builder.Files); + this.AttachmentsInternal.AddRange(builder.Attachments); } internal List ChoicesInternal { get; } = []; @@ -55,48 +59,12 @@ public bool IsEphemeral private bool EPH { get; set; } - /// - /// Whether to send as silent message. - /// - public bool NotificationsSuppressed - { - get => this.NOTI_SUP; - set - { - this.NOTI_SUP = value; - this.FlagsChanged = true; - } - } - - private bool NOTI_SUP { get; set; } - - /// - /// Whether to send as voice message. - /// You can't use that on your own, it needs DisCatSharp.Experimental. - /// - internal bool IsVoiceMessage - { - get => this.VOICE_MSG; - set - { - this.VOICE_MSG = value; - this.FlagsChanged = true; - } - } - - private bool VOICE_MSG { get; set; } - /// /// The choices to send on this interaction response. /// Mutually exclusive with content, embed, and components. /// public IReadOnlyList Choices => this.ChoicesInternal; - /// - /// Mentions to send on this interaction response. - /// - public List? Mentions { get; private set; } - /// /// The hints to send on this interaction response. /// @@ -222,7 +190,6 @@ public DiscordInteractionResponseBuilder WithTts(bool tts) /// The current builder to chain calls with. public DiscordInteractionResponseBuilder AsEphemeral() { - this.FlagsChanged = true; this.IsEphemeral = true; return this; } @@ -233,7 +200,6 @@ public DiscordInteractionResponseBuilder AsEphemeral() /// The current builder to chain calls with. public DiscordInteractionResponseBuilder AsUIKitMessage() { - this.FlagsChanged = true; this.IsUIKit = true; return this; } @@ -244,7 +210,6 @@ public DiscordInteractionResponseBuilder AsUIKitMessage() /// The current builder to chain calls with. public DiscordInteractionResponseBuilder SuppressEmbeds() { - this.FlagsChanged = true; this.EmbedsSuppressed = true; return this; } @@ -255,7 +220,6 @@ public DiscordInteractionResponseBuilder SuppressEmbeds() /// The current builder to chain calls with. public DiscordInteractionResponseBuilder AsSilentMessage() { - this.FlagsChanged = true; this.NotificationsSuppressed = true; return this; } @@ -265,7 +229,6 @@ public DiscordInteractionResponseBuilder AsSilentMessage() /// internal DiscordInteractionResponseBuilder AsVoiceMessage(bool asVoiceMessage = true) { - this.FlagsChanged = true; this.IsVoiceMessage = asVoiceMessage; return this; } @@ -288,8 +251,8 @@ public DiscordInteractionResponseBuilder WithContent(string content) /// The current builder to chain calls with. public DiscordInteractionResponseBuilder AddEmbed(DiscordEmbed embed) { - if (embed != null) - this.EmbedsInternal.Add(embed); // Interactions will 400 silently // + ArgumentNullException.ThrowIfNull(embed, nameof(embed)); + this.EmbedsInternal.Add(embed); return this; } @@ -456,9 +419,6 @@ public override void Clear() this.Mentions = null; this.ChoicesInternal.Clear(); this.Poll = null; - this.IsVoiceMessage = false; - this.NotificationsSuppressed = false; - this.EmbedsSuppressed = false; base.Clear(); } } diff --git a/DisCatSharp/Entities/Message/DiscordMessage.cs b/DisCatSharp/Entities/Message/DiscordMessage.cs index 39260fbe42..e7efaeb88f 100644 --- a/DisCatSharp/Entities/Message/DiscordMessage.cs +++ b/DisCatSharp/Entities/Message/DiscordMessage.cs @@ -699,7 +699,7 @@ public Task ModifyAsync(Optional content, Optional ModifyAsync(DiscordMessageBuilder builder) { builder.Validate(true); - return await this.Discord.ApiClient.EditMessageAsync(this.ChannelId, this.Id, builder.Content, Optional.Some(builder.Embeds.AsEnumerable()), builder.Mentions, builder.Components, builder.Suppressed, builder.Files, builder.Attachments.Count > 0 + return await this.Discord.ApiClient.EditMessageAsync(this.ChannelId, this.Id, builder.Content, Optional.Some(builder.Embeds.AsEnumerable()), builder.Mentions, builder.Components, builder.EmbedsSuppressed, builder.Files, builder.Attachments.Count > 0 ? Optional.Some(builder.Attachments.AsEnumerable()) : builder.KeepAttachmentsInternal.HasValue ? builder.KeepAttachmentsInternal.Value && this.Attachments is not null ? Optional.Some(this.Attachments.AsEnumerable()) : Array.Empty() @@ -737,7 +737,7 @@ public async Task ModifyAsync(Action acti var builder = new DiscordMessageBuilder(); action(builder); builder.Validate(true); - return await this.Discord.ApiClient.EditMessageAsync(this.ChannelId, this.Id, builder.Content, Optional.Some(builder.Embeds.AsEnumerable()), builder.Mentions, builder.Components, builder.Suppressed, builder.Files, builder.Attachments.Count > 0 + return await this.Discord.ApiClient.EditMessageAsync(this.ChannelId, this.Id, builder.Content, Optional.Some(builder.Embeds.AsEnumerable()), builder.Mentions, builder.Components, builder.EmbedsSuppressed, builder.Files, builder.Attachments.Count > 0 ? Optional.Some(builder.Attachments.AsEnumerable()) : builder.KeepAttachmentsInternal.HasValue ? builder.KeepAttachmentsInternal.Value && this.Attachments is not null ? Optional.Some(this.Attachments.AsEnumerable()) : Array.Empty() diff --git a/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs b/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs index 9d7c37b397..745bec5df1 100644 --- a/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs +++ b/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs @@ -39,11 +39,6 @@ public sealed class DiscordMessageBuilder : DisCatSharpBuilder /// public bool IsVoiceMessage { get; private set; } - /// - /// Gets the Allowed Mentions for the message to be sent. - /// - public List? Mentions { get; private set; } - /// /// Gets the Reply Message ID. /// @@ -54,11 +49,6 @@ public sealed class DiscordMessageBuilder : DisCatSharpBuilder /// public bool MentionOnReply { get; private set; } - /// - /// Gets if the embeds should be suppressed. - /// - public bool Suppressed { get; private set; } - /// /// Gets if the Reply will error if the Reply Message Id does not reference a valid message. /// If set to false, invalid replies are send as a regular message. @@ -228,7 +218,7 @@ public DiscordMessageBuilder HasTts(bool isTts) /// public DiscordMessageBuilder SuppressEmbeds(bool suppress = true) { - this.Suppressed = suppress; + this.EmbedsSuppressed = suppress; return this; } @@ -238,7 +228,6 @@ public DiscordMessageBuilder SuppressEmbeds(bool suppress = true) /// The current builder to chain calls with. public DiscordMessageBuilder AsUIKitMessage() { - this.FlagsChanged = true; this.IsUIKit = true; return this; } @@ -268,9 +257,7 @@ public DiscordMessageBuilder AsSilentMessage(bool silent = true) /// The current builder to be chained. public DiscordMessageBuilder AddEmbed(DiscordEmbed embed) { - if (embed == null) - return this; //Providing null embeds will produce a 400 response from Discord.// - + ArgumentNullException.ThrowIfNull(embed, nameof(embed)); this.EmbedsInternal.Add(embed); return this; } @@ -472,17 +459,13 @@ public void ClearPoll() public override void Clear() { this.IsTts = false; - this.Mentions = null; this.ReplyId = null; this.MentionOnReply = false; - this.Suppressed = false; this.Sticker = null!; this.KeepAttachmentsInternal = false; this.Nonce = null; this.EnforceNonce = false; this.Poll = null; - this.IsVoiceMessage = false; - this.IsUIKit = false; base.Clear(); } diff --git a/DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs b/DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs index 29bd37bca2..5d4fef36d9 100644 --- a/DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs +++ b/DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs @@ -25,7 +25,7 @@ public sealed class DiscordWebhookBuilder : DisCatSharpBuilder /// Constructs a new empty webhook request builder. /// public DiscordWebhookBuilder() - { } // I still see no point in initializing collections with empty collections. // + { } /// /// Username to use for this webhook request. @@ -42,28 +42,12 @@ public DiscordWebhookBuilder() /// public bool IsTts { get; private set; } - /// - /// Whether to send as silent message. - /// - public bool NotificationsSuppressed { get; private set; } - - /// - /// Whether to send as voice message. - /// You can't use that on your own, it needs DisCatSharp.Experimental. - /// - public bool IsVoiceMessage { get; private set; } - /// /// Name of the new thread. /// Only works if the webhook is send in a . /// public string? ThreadName { get; set; } - /// - /// Mentions to send on this webhook request. - /// - public List? Mentions { get; private set; } - /// /// Forum post tags to send on this webhook request. /// @@ -79,7 +63,6 @@ public DiscordWebhookBuilder() /// public DiscordWebhookBuilder SuppressEmbeds() { - this.FlagsChanged = true; this.EmbedsSuppressed = true; return this; } @@ -89,7 +72,6 @@ public DiscordWebhookBuilder SuppressEmbeds() /// public DiscordWebhookBuilder AsSilentMessage() { - this.FlagsChanged = true; this.NotificationsSuppressed = true; return this; } @@ -99,7 +81,6 @@ public DiscordWebhookBuilder AsSilentMessage() /// public DiscordWebhookBuilder AsVoiceMessage() { - this.FlagsChanged = true; this.IsVoiceMessage = true; return this; } @@ -250,9 +231,8 @@ public DiscordWebhookBuilder WithThreadName(string name) /// Embed to add. public DiscordWebhookBuilder AddEmbed(DiscordEmbed embed) { - if (embed != null) - this.EmbedsInternal.Add(embed); - + ArgumentNullException.ThrowIfNull(embed, nameof(embed)); + this.EmbedsInternal.Add(embed); return this; } @@ -473,10 +453,6 @@ public override void Clear() this.KeepAttachmentsInternal = false; this.ThreadName = null; this.Poll = null; - this.FlagsChanged = false; - this.NotificationsSuppressed = false; - this.IsTts = false; - this.IsVoiceMessage = false; base.Clear(); } diff --git a/DisCatSharp/Net/Rest/DiscordApiClient.cs b/DisCatSharp/Net/Rest/DiscordApiClient.cs index e53466a84c..5ef5aceb7f 100644 --- a/DisCatSharp/Net/Rest/DiscordApiClient.cs +++ b/DisCatSharp/Net/Rest/DiscordApiClient.cs @@ -3109,7 +3109,7 @@ internal async Task CreateMessageAsync(ulong channelId, DiscordM embed.Timestamp = embed.Timestamp.Value.ToUniversalTime(); var flags = MessageFlags.None; - if (builder.Suppressed) + if (builder.EmbedsSuppressed) flags |= MessageFlags.SuppressedEmbeds; if (builder.Silent) flags |= MessageFlags.SuppressNotifications;