This commit is contained in:
sam 2024-08-13 13:08:50 +02:00
commit ded4f4db26
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
43 changed files with 2052 additions and 0 deletions

View file

@ -0,0 +1,44 @@
using Remora.Discord.API.Abstractions.Objects;
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();
return discriminator == 0 ? user.Username.Value : $"{user.Username.Value}#{discriminator:0000}";
}
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}";
}
var avatarIndex = user.Discriminator == 0 ? (int)((user.ID.Value >> 22) % 6) : user.Discriminator % 5;
return $"https://cdn.discordapp.com/embed/avatars/{avatarIndex}.png?size={size}";
}
public static ulong ToUlong(this Snowflake snowflake) => snowflake.Value;
public static ulong ToUlong(this Optional<Snowflake> snowflake)
{
if (!snowflake.IsDefined()) throw new Exception("ToUlong called on an undefined Snowflake");
return snowflake.Value.Value;
}
public static T GetOrThrow<T>(this Result<T> result)
{
if (result.Error != null) throw new DiscordRestException(result.Error.Message);
return result.Entity;
}
public static async Task<T> GetOrThrow<T>(this Task<Result<T>> result) => (await result).GetOrThrow();
public class DiscordRestException(string message) : Exception(message);
}