2024-10-09 17:33:59 +02:00
|
|
|
using System.Drawing;
|
2024-08-16 22:28:05 +02:00
|
|
|
using Humanizer;
|
2024-08-14 16:05:43 +02:00
|
|
|
using OneOf;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2024-08-14 16:05:43 +02:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
|
using Remora.Discord.API.Objects;
|
2024-09-02 15:06:10 +02:00
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
|
using Remora.Discord.Commands.Extensions;
|
|
|
|
|
using Remora.Discord.Commands.Services;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Rest.Core;
|
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class DiscordExtensions
|
|
|
|
|
{
|
|
|
|
|
public static string Tag(this IPartialUser user)
|
|
|
|
|
{
|
|
|
|
|
var discriminator = user.Discriminator.OrDefault();
|
2024-10-09 17:35:11 +02:00
|
|
|
return discriminator == 0
|
|
|
|
|
? user.Username.Value
|
|
|
|
|
: $"{user.Username.Value}#{discriminator:0000}";
|
2024-08-13 13:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string AvatarUrl(this IUser user, int size = 256)
|
|
|
|
|
{
|
|
|
|
|
if (user.Avatar != null)
|
|
|
|
|
{
|
|
|
|
|
var ext = user.Avatar.HasGif ? ".gif" : ".webp";
|
|
|
|
|
return $"https://cdn.discordapp.com/avatars/{user.ID}/{user.Avatar.Value}{ext}?size={size}";
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
var avatarIndex =
|
|
|
|
|
user.Discriminator == 0 ? (int)((user.ID.Value >> 22) % 6) : user.Discriminator % 5;
|
2024-08-13 13:08:50 +02:00
|
|
|
return $"https://cdn.discordapp.com/embed/avatars/{avatarIndex}.png?size={size}";
|
|
|
|
|
}
|
2024-08-16 17:03:26 +02:00
|
|
|
|
2024-08-13 16:48:54 +02:00
|
|
|
public static string? IconUrl(this IGuild guild, int size = 256)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
if (guild.Icon == null)
|
|
|
|
|
return null;
|
2024-08-13 16:48:54 +02:00
|
|
|
|
|
|
|
|
var ext = guild.Icon.HasGif ? ".gif" : ".webp";
|
|
|
|
|
|
|
|
|
|
return $"https://cdn.discordapp.com/icons/{guild.ID}/{guild.Icon.Value}{ext}?size={size}";
|
|
|
|
|
}
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
public static ulong ToUlong(this Snowflake snowflake) => snowflake.Value;
|
|
|
|
|
|
|
|
|
|
public static ulong ToUlong(this Optional<Snowflake> snowflake)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
if (!snowflake.IsDefined())
|
|
|
|
|
throw new Exception("ToUlong called on an undefined Snowflake");
|
2024-08-13 13:08:50 +02:00
|
|
|
return snowflake.Value.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:33:59 +02:00
|
|
|
public static string ToPrettyString(this Color color)
|
|
|
|
|
{
|
|
|
|
|
var r = color.R.ToString("X2");
|
|
|
|
|
var g = color.G.ToString("X2");
|
|
|
|
|
var b = color.B.ToString("X2");
|
|
|
|
|
|
|
|
|
|
return $"#{r}{g}{b}";
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public static bool Is(this Optional<Snowflake> s1, Snowflake s2) =>
|
|
|
|
|
s1.IsDefined(out var value) && value == s2;
|
|
|
|
|
|
|
|
|
|
public static bool Is(this Optional<Snowflake> s1, ulong s2) =>
|
|
|
|
|
s1.IsDefined(out var value) && value == s2;
|
2024-08-16 17:03:26 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public static T GetOrThrow<T>(this Result<T> result)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
if (result.Error != null)
|
|
|
|
|
throw new DiscordRestException(result.Error.Message);
|
2024-08-13 13:08:50 +02:00
|
|
|
return result.Entity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 00:51:19 +02:00
|
|
|
public static T GetOrThrow<T>(this Optional<T> optional) =>
|
|
|
|
|
optional.OrThrow(() => new CataloggerError("Optional<T> was unset"));
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public static async Task<T> GetOrThrow<T>(this Task<Result<T>> result) =>
|
|
|
|
|
(await result).GetOrThrow();
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public static async Task<Result> UpdateMessageAsync(
|
|
|
|
|
this IDiscordRestInteractionAPI interactionApi,
|
|
|
|
|
IInteraction interaction,
|
|
|
|
|
InteractionMessageCallbackData data
|
|
|
|
|
) =>
|
|
|
|
|
await interactionApi.CreateInteractionResponseAsync(
|
|
|
|
|
interaction.ID,
|
2024-08-14 16:05:43 +02:00
|
|
|
interaction.Token,
|
2024-10-09 17:35:11 +02:00
|
|
|
new InteractionResponse(
|
|
|
|
|
InteractionCallbackType.UpdateMessage,
|
|
|
|
|
new Optional<
|
|
|
|
|
OneOf<
|
|
|
|
|
IInteractionMessageCallbackData,
|
|
|
|
|
IInteractionAutocompleteCallbackData,
|
|
|
|
|
IInteractionModalCallbackData
|
|
|
|
|
>
|
|
|
|
|
>(data)
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-08-14 16:05:43 +02:00
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
public static string ToPrettyString(this IDiscordPermissionSet permissionSet) =>
|
2024-10-09 17:35:11 +02:00
|
|
|
string.Join(
|
|
|
|
|
", ",
|
|
|
|
|
permissionSet.GetPermissions().Select(p => p.Humanize(LetterCasing.Title))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
public static (Snowflake, Snowflake) GetUserAndGuild(
|
|
|
|
|
this ContextInjectionService contextInjectionService
|
|
|
|
|
)
|
2024-09-02 15:06:10 +02:00
|
|
|
{
|
|
|
|
|
if (contextInjectionService.Context is not IInteractionCommandContext ctx)
|
|
|
|
|
throw new CataloggerError("No context");
|
2024-10-09 17:35:11 +02:00
|
|
|
if (!ctx.TryGetUserID(out var userId))
|
|
|
|
|
throw new CataloggerError("No user ID in context");
|
|
|
|
|
if (!ctx.TryGetGuildID(out var guildId))
|
|
|
|
|
throw new CataloggerError("No guild ID in context");
|
2024-09-02 15:06:10 +02:00
|
|
|
return (userId, guildId);
|
|
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
|
2024-09-21 20:32:02 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Sorts a list of roles by their position in the Discord interface.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roles">The list of guild roles to filter.</param>
|
|
|
|
|
/// <param name="filterByIds">An optional list of role IDs to return, from a member object or similar.
|
|
|
|
|
/// If null, the entire list is returned.</param>
|
|
|
|
|
/// <returns></returns>
|
2024-10-09 17:35:11 +02:00
|
|
|
public static IEnumerable<IRole> Sorted(
|
|
|
|
|
this IEnumerable<IRole> roles,
|
|
|
|
|
IEnumerable<Snowflake>? filterByIds = null
|
|
|
|
|
)
|
2024-09-21 20:32:02 +02:00
|
|
|
{
|
|
|
|
|
var sorted = roles.OrderByDescending(r => r.Position);
|
|
|
|
|
return filterByIds != null ? sorted.Where(r => filterByIds.Contains(r.ID)) : sorted;
|
|
|
|
|
}
|
2024-09-02 15:06:10 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public class DiscordRestException(string message) : Exception(message);
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|