Beispiel #1
0
        public async Task JumboAsync(CommandContext context, DiscordEmoji emote)
        {
            try
            {
                var emoteUri = new Uri(emote.GetEmojiURL());

                using var httpClient     = _httpClientFactory.CreateClient();
                await using var response = await httpClient.GetStreamAsync(emoteUri);

                // Workaround httpClient response streams not being allowed to seek around.
                await using var stream = new MemoryStream();
                await response.CopyToAsync(stream);

                stream.Seek(0, SeekOrigin.Begin);

                var builder = new DiscordMessageBuilder();
                if (emote.IsAnimated)
                {
                    var filename = Path.GetFileName(emoteUri.LocalPath);
                    await context.RespondAsync(builder.WithFile(filename, stream));

                    return;
                }
                else if (emote.Id == 0)
                {
                    var filename = Path.GetFileNameWithoutExtension(emoteUri.LocalPath);
                    using var svg = new SKSvg();
                    if (svg.Load(stream) is { })
                    {
                        float scaleX = 128 / svg.Picture.CullRect.Height;
                        float scaleY = 128 / svg.Picture.CullRect.Width;
                        using var bitmap  = svg.Picture.ToBitmap(SKColors.Transparent, scaleX, scaleY, SKColorType.Rgba8888, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
                        using var image   = SKImage.FromBitmap(bitmap);
                        using var _stream = image.Encode(SKEncodedImageFormat.Webp, 90).AsStream(true);
                        await context.RespondAsync(builder.WithFile(filename + ".webp", _stream));
                    }
                }