2024-10-09 17:33:59 +02:00
|
|
|
using System.Drawing;
|
2024-10-12 23:28:15 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-16 22:28:05 +02:00
|
|
|
using Humanizer;
|
2024-10-11 20:38:53 +02:00
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.API.Abstractions.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-10-11 20:38:53 +02:00
|
|
|
public static string? AvatarUrl(this IGuildMemberUpdate member, int size = 256) =>
|
|
|
|
|
GuildAvatarUrl(
|
|
|
|
|
member.GuildID,
|
|
|
|
|
member.User.ID,
|
|
|
|
|
member.Avatar.OrDefault()?.Value,
|
|
|
|
|
isAnimated: member.Avatar.OrDefault()?.HasGif,
|
|
|
|
|
size
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
public static string? AvatarUrl(this IGuildMember member, Snowflake guildId, int size = 256) =>
|
|
|
|
|
GuildAvatarUrl(
|
|
|
|
|
guildId,
|
|
|
|
|
member.User.GetOrThrow().ID,
|
|
|
|
|
member.Avatar.OrDefault()?.Value,
|
|
|
|
|
isAnimated: member.Avatar.OrDefault()?.HasGif,
|
|
|
|
|
size
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
private static string? GuildAvatarUrl(
|
|
|
|
|
Snowflake guildId,
|
|
|
|
|
Snowflake userId,
|
|
|
|
|
string? hash,
|
|
|
|
|
bool? isAnimated,
|
|
|
|
|
int size = 256
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (hash == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var ext = isAnimated == true ? ".gif" : ".webp";
|
|
|
|
|
|
|
|
|
|
return $"https://cdn.discordapp.com/guilds/{guildId}/users/{userId}/avatars/{hash}{ext}?size={size}";
|
|
|
|
|
}
|
|
|
|
|
|
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-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-10-13 14:58:44 +02:00
|
|
|
public static async Task<string> TryFormatUserAsync(
|
2024-10-12 23:28:15 +02:00
|
|
|
this UserCache userCache,
|
2024-10-13 14:58:44 +02:00
|
|
|
Snowflake userId,
|
|
|
|
|
bool addMention = true
|
2024-10-12 23:28:15 +02:00
|
|
|
)
|
|
|
|
|
{
|
2024-10-13 14:58:44 +02:00
|
|
|
var user = await userCache.GetUserAsync(userId);
|
|
|
|
|
if (addMention)
|
|
|
|
|
return user != null
|
|
|
|
|
? $"{user.Tag()} <@{user.ID}>"
|
|
|
|
|
: $"*(unknown user {userId}) <@{userId}>*";
|
|
|
|
|
|
|
|
|
|
return user != null ? user.Tag() : $"*(unknown user {userId})*";
|
2024-10-12 23:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-12 23:47:18 +02:00
|
|
|
public static int TextLength(this IEmbed embed)
|
|
|
|
|
{
|
|
|
|
|
var length = OptionalStringLength(embed.Description) + OptionalStringLength(embed.Title);
|
|
|
|
|
var fieldLength = (embed.Fields.OrDefault() ?? [])
|
|
|
|
|
.Select(f => f.Name.Length + f.Value.Length)
|
|
|
|
|
.Sum();
|
|
|
|
|
|
|
|
|
|
return length + fieldLength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int OptionalStringLength(Optional<string> s) => s.OrDefault()?.Length ?? 0;
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public class DiscordRestException(string message) : Exception(message);
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|