Skip to content

Commit

Permalink
role stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Oct 17, 2023
1 parent 29f9a4e commit 72a5f08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
49 changes: 20 additions & 29 deletions DisCatSharp/Entities/Guild/DiscordRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public string Mention
public IReadOnlyList<KeyValuePair<ulong, DiscordMember>> Members
=> this.Guild.Members.Where(x => x.Value.RoleIds.Any(x => x == this.Id)).ToList();

#region Methods
#region Methods

/// <summary>
/// Modifies this role's position.
Expand All @@ -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);
Expand All @@ -194,44 +196,33 @@ public Task ModifyAsync(string name = null, Permissions? permissions = null, Dis
/// Updates this role.
/// </summary>
/// <param name="action">The action.</param>
/// <exception cref = "Exceptions.UnauthorizedException" > Thrown when the client does not have the<see cref="Permissions.ManageRoles"/> permission.</exception>
/// <exception cref="UnauthorizedException"> Thrown when the client does not have the<see cref="Permissions.ManageRoles"/> permission.</exception>
/// <exception cref="NotFoundException">Thrown when the role does not exist.</exception>
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
/// <exception cref="Exception">A delegate callback throws an exception.</exception>
public Task ModifyAsync(Action<RoleEditModel> 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<string>(null);
if (mdl.Icon.HasValue && mdl.Icon.Value != null)
iconb64 = ImageTool.Base64FromStream(mdl.Icon);
else if (mdl.Icon.HasValue)
iconb64 = Optional.Some<string?>(null);
else
iconb64 = Optional.None;

var emoji = Optional.FromNullable<string?>(null);
var iconb64 = mdl.Icon.HasValue switch
{
true when mdl.Icon.Value != null => ImageTool.Base64FromStream(mdl.Icon),
true => Optional.Some<string?>(null),
_ => (Optional<string>)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<string?>(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<string?>(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.");
}
Expand All @@ -248,7 +239,7 @@ public Task ModifyAsync(Action<RoleEditModel> action)
public Task DeleteAsync(string reason = null)
=> this.Discord.ApiClient.DeleteRoleAsync(this.GuildId, this.Id, reason);

#endregion
#endregion

/// <summary>
/// Initializes a new instance of the <see cref="DiscordRole"/> class.
Expand Down Expand Up @@ -306,7 +297,7 @@ public override int GetHashCode()
/// <returns>Whether the two roles are equal.</returns>
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);

/// <summary>
/// Gets whether the two <see cref="DiscordRole"/> objects are not equal.
Expand Down
1 change: 1 addition & 0 deletions DisCatSharp/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@
[assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "<Pending>", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.PublicUpdatesChannel")]
[assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "<Pending>", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.RulesChannel")]
[assembly: SuppressMessage("Usage", "DCS0200:[Discord] Requires Features", Justification = "<Pending>", Scope = "member", Target = "~P:DisCatSharp.Entities.DiscordGuild.SafetyAltersChannel")]
[assembly: SuppressMessage("Style", "IDE0048:Add parentheses for clarity", Justification = "<Pending>", Scope = "member", Target = "~M:DisCatSharp.Entities.DiscordRole.op_Equality(DisCatSharp.Entities.DiscordRole,DisCatSharp.Entities.DiscordRole)~System.Boolean")]

0 comments on commit 72a5f08

Please sign in to comment.