Skip to content

Commit

Permalink
fix: possible fix for edit setting role icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Sep 26, 2023
1 parent 6b09dc6 commit 935be1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
32 changes: 21 additions & 11 deletions DisCatSharp/Entities/Guild/DiscordRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DiscordRole : SnowflakeObject, IEquatable<DiscordRole>
/// </summary>
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; internal set; }

/// <summary>
/// Gets the version number for this role.
/// </summary>
Expand Down Expand Up @@ -188,7 +188,7 @@ public Task ModifyPositionAsync(int position, string reason = null)
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
public Task ModifyAsync(string name = null, Permissions? permissions = null, DiscordColor? color = null, bool? hoist = null, bool? mentionable = null, string reason = null)
=> this.Discord.ApiClient.ModifyGuildRoleAsync(this.GuildId, this.Id, name, permissions, color?.Value, hoist, mentionable, null, null, reason);
=> this.Discord.ApiClient.ModifyGuildRoleAsync(this.GuildId, this.Id, name, permissions, color?.Value, hoist, mentionable, Optional.None, Optional.None, reason);

/// <summary>
/// Updates this role.
Expand All @@ -211,17 +211,27 @@ public Task ModifyAsync(Action<RoleEditModel> action)
if (mdl.Icon.HasValue && mdl.Icon.Value != null)
iconb64 = ImageTool.Base64FromStream(mdl.Icon);
else if (mdl.Icon.HasValue)
iconb64 = Optional.Some<string>(null);
iconb64 = Optional.Some<string?>(null);
else
iconb64 = Optional.None;

var emoji = Optional.FromNullable<string>(null);
var emoji = Optional.FromNullable<string?>(null);

if (mdl.UnicodeEmoji.HasValue && mdl.UnicodeEmoji.Value != null)
emoji = mdl.UnicodeEmoji
.MapOrNull(e => e.Id == 0
? e.Name
: throw new ArgumentException("Emoji must be unicode"));
else if (mdl.UnicodeEmoji.HasValue)
emoji = Optional.Some<string>(null);
switch (mdl.UnicodeEmoji.HasValue)
{
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;
}

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 Down
4 changes: 2 additions & 2 deletions DisCatSharp/Net/Models/RoleEditModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal RoleEditModel()
this.Color = null;
this.Hoist = null;
this.Mentionable = null;
this.Icon = null;
this.UnicodeEmoji = null;
this.Icon = Optional.None;
this.UnicodeEmoji = Optional.None;
}
}
9 changes: 3 additions & 6 deletions DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3374,16 +3374,13 @@ internal async Task<DiscordRole> ModifyGuildRoleAsync(ulong guildId, ulong roleI
Mentionable = mentionable,
};

if (emoji.HasValue && !iconb64.HasValue)
pld.UnicodeEmoji = emoji;

if (emoji.HasValue && iconb64.HasValue)
{
pld.IconBase64 = null;
pld.UnicodeEmoji = emoji;
}

if (iconb64.HasValue)
} else if (emoji.HasValue && !iconb64.HasValue)
pld.UnicodeEmoji = emoji;
else if (iconb64.HasValue && !emoji.HasValue)
pld.IconBase64 = iconb64;

var headers = Utilities.GetBaseHeaders();
Expand Down

0 comments on commit 935be1f

Please sign in to comment.