From 72a5f081e59e8609e46cf01bcc24c65b87923041 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Tue, 17 Oct 2023 12:54:29 +0200 Subject: [PATCH] role stuff --- DisCatSharp/Entities/Guild/DiscordRole.cs | 49 +++++++++-------------- DisCatSharp/GlobalSuppressions.cs | 1 + 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/DisCatSharp/Entities/Guild/DiscordRole.cs b/DisCatSharp/Entities/Guild/DiscordRole.cs index 6381e8846d..5b9a511bac 100644 --- a/DisCatSharp/Entities/Guild/DiscordRole.cs +++ b/DisCatSharp/Entities/Guild/DiscordRole.cs @@ -148,7 +148,7 @@ public string Mention public IReadOnlyList> Members => this.Guild.Members.Where(x => x.Value.RoleIds.Any(x => x == this.Id)).ToList(); - #region Methods +#region Methods /// /// Modifies this role's position. @@ -168,7 +168,9 @@ public Task ModifyPositionAsync(int position, string reason = null) RoleId = x.Id, Position = x.Id == this.Id ? position - : x.Position <= position ? x.Position - 1 : x.Position + : x.Position <= position + ? x.Position - 1 + : x.Position }); return this.Discord.ApiClient.ModifyGuildRolePositionAsync(this.GuildId, roles, reason); @@ -194,44 +196,33 @@ public Task ModifyAsync(string name = null, Permissions? permissions = null, Dis /// Updates this role. /// /// The action. - /// Thrown when the client does not have the permission. + /// Thrown when the client does not have the permission. /// Thrown when the role does not exist. /// Thrown when an invalid parameter was provided. /// Thrown when Discord is unable to process the request. + /// A delegate callback throws an exception. public Task ModifyAsync(Action action) { var mdl = new RoleEditModel(); action(mdl); var canContinue = true; - if ((mdl.Icon.HasValue && mdl.Icon.Value != null) || (mdl.UnicodeEmoji.HasValue && mdl.UnicodeEmoji.Value != null)) + if (mdl.Icon is { HasValue: true, Value: not null } || mdl.UnicodeEmoji is { HasValue: true, Value: not null }) canContinue = this.Guild.Features.HasFeature(GuildFeaturesEnum.CanSetRoleIcons); - var iconb64 = Optional.FromNullable(null); - if (mdl.Icon.HasValue && mdl.Icon.Value != null) - iconb64 = ImageTool.Base64FromStream(mdl.Icon); - else if (mdl.Icon.HasValue) - iconb64 = Optional.Some(null); - else - iconb64 = Optional.None; - - var emoji = Optional.FromNullable(null); + var iconb64 = mdl.Icon.HasValue switch + { + true when mdl.Icon.Value != null => ImageTool.Base64FromStream(mdl.Icon), + true => Optional.Some(null), + _ => (Optional)Optional.None + }; - switch (mdl.UnicodeEmoji.HasValue) + var emoji = mdl.UnicodeEmoji.HasValue switch { - case true when mdl.UnicodeEmoji.Value != null: - emoji = mdl.UnicodeEmoji - .MapOrNull(e => e.Id == 0 - ? e.Name - : throw new ArgumentException("Emoji must be unicode")); - break; - case true: - emoji = Optional.Some(null); - break; - case false: - emoji = Optional.None; - break; - } + true when mdl.UnicodeEmoji.Value != null => mdl.UnicodeEmoji.MapOrNull(e => e.Id == 0 ? e.Name : throw new ArgumentException("Emoji must be unicode")), + true => Optional.Some(null), + false => Optional.None + }; return canContinue ? this.Discord.ApiClient.ModifyGuildRoleAsync(this.GuildId, this.Id, mdl.Name, mdl.Permissions, mdl.Color?.Value, mdl.Hoist, mdl.Mentionable, iconb64, emoji, mdl.AuditLogReason) : throw new NotSupportedException($"Cannot modify role icon. Guild needs boost tier two."); } @@ -248,7 +239,7 @@ public Task ModifyAsync(Action action) public Task DeleteAsync(string reason = null) => this.Discord.ApiClient.DeleteRoleAsync(this.GuildId, this.Id, reason); - #endregion +#endregion /// /// Initializes a new instance of the class. @@ -306,7 +297,7 @@ public override int GetHashCode() /// Whether the two roles are equal. public static bool operator ==(DiscordRole e1, DiscordRole e2) => e1 is null == e2 is null - && ((e1 is null && e2 is null) || e1.Id == e2.Id); + && (e1 is null && e2 is null || e1.Id == e2.Id); /// /// Gets whether the two objects are not equal. diff --git a/DisCatSharp/GlobalSuppressions.cs b/DisCatSharp/GlobalSuppressions.cs index aa23ef6025..ff710c3729 100644 --- a/DisCatSharp/GlobalSuppressions.cs +++ b/DisCatSharp/GlobalSuppressions.cs @@ -82,3 +82,4 @@ [assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.PublicUpdatesChannel")] [assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.RulesChannel")] [assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.SafetyAltersChannel")] +[assembly: SuppressMessage("Style", "IDE0048:Add parentheses for clarity", Justification = "", Scope = "member", Target = "~M:DisCatSharp.Entities.DiscordRole.op_Equality(DisCatSharp.Entities.DiscordRole,DisCatSharp.Entities.DiscordRole)~System.Boolean")]