示例#1
0
            public async Task EmojiAsync([Remainder] string emoji)
            {
                if (!RiasUtilities.TryParseEmoji(emoji, out var emojiId))
                {
                    await ReplyErrorAsync(Localization.AdministrationEmojiNotValid);

                    return;
                }

                var httpClient = HttpClient;
                var emojiUrl   = string.Format(EmojiCdn, $"{emojiId}.gif");
                var result     = await httpClient.GetAsync(emojiUrl);

                if (result.StatusCode == HttpStatusCode.UnsupportedMediaType)
                {
                    emojiUrl = string.Format(EmojiCdn, $"{emojiId}.png");
                    result   = await httpClient.GetAsync(emojiUrl);

                    if (!result.IsSuccessStatusCode)
                    {
                        await ReplyErrorAsync(Localization.AdministrationEmojiNotValid);

                        return;
                    }
                }

                var embed = new DiscordEmbedBuilder
                {
                    Color  = RiasUtilities.ConfirmColor,
                    Author = new DiscordEmbedBuilder.EmbedAuthor
                    {
                        Name = emoji[(emoji.IndexOf(":", StringComparison.Ordinal) + 1)..emoji.LastIndexOf(":", StringComparison.Ordinal)],
示例#2
0
            public async Task AddEmojiAsync(string emoji, [Remainder] string name)
            {
                var httpClient = HttpClient;

                await using var emojiStream = new MemoryStream();
                bool?isAnimated = null;

                if (RiasUtilities.TryParseEmoji(emoji, out var emojiId))
                {
                    var emojiUrl = string.Format(EmojiCdn, $"{emojiId}.gif");
                    var result   = await httpClient.GetAsync(emojiUrl);

                    isAnimated = true;

                    if (result.StatusCode == HttpStatusCode.UnsupportedMediaType)
                    {
                        emojiUrl = string.Format(EmojiCdn, $"{emojiId}.png");
                        result   = await httpClient.GetAsync(emojiUrl);

                        isAnimated = false;

                        if (!result.IsSuccessStatusCode)
                        {
                            return;
                        }
                    }

                    await using var stream = await result.Content.ReadAsStreamAsync();

                    await stream.CopyToAsync(emojiStream);

                    emojiStream.Position = 0;
                }

                if (!isAnimated.HasValue)
                {
                    if (!Uri.TryCreate(emoji, UriKind.Absolute, out var emojiUri))
                    {
                        await ReplyErrorAsync(Localization.UtilityUrlNotValid);

                        return;
                    }

                    if (emojiUri.Scheme != Uri.UriSchemeHttps)
                    {
                        await ReplyErrorAsync(Localization.UtilityUrlNotHttps);

                        return;
                    }

                    using var result = await httpClient.GetAsync(emojiUri);

                    if (!result.IsSuccessStatusCode)
                    {
                        await ReplyErrorAsync(Localization.UtilityImageOrUrlNotGood);

                        return;
                    }

                    await using var stream = await result.Content.ReadAsStreamAsync();

                    await stream.CopyToAsync(emojiStream);

                    emojiStream.Position = 0;

                    if (RiasUtilities.IsPng(emojiStream) || RiasUtilities.IsJpg(emojiStream))
                    {
                        isAnimated = false;
                    }

                    if (!isAnimated.HasValue && RiasUtilities.IsGif(emojiStream))
                    {
                        isAnimated = true;
                    }

                    if (!isAnimated.HasValue)
                    {
                        await ReplyErrorAsync(Localization.UtilityUrlNotPngJpgGif);

                        return;
                    }
                }

                var emojis      = Context.Guild !.Emojis.Values;
                var emojisSlots = Context.Guild !.GetGuildEmotesSlots();

                if (isAnimated.Value)
                {
                    if (emojis.Count(x => x.IsAnimated) >= emojisSlots)
                    {
                        await ReplyErrorAsync(Localization.AdministrationAnimatedEmojisLimit, emojisSlots);

                        return;
                    }
                }
                else
                {
                    if (emojis.Count(x => !x.IsAnimated) >= emojisSlots)
                    {
                        await ReplyErrorAsync(Localization.AdministrationStaticEmojisLimit, emojisSlots);

                        return;
                    }
                }

                // Check if length is bigger than 256 KB
                if (emojiStream.Length / 1024 > 256)
                {
                    await ReplyErrorAsync(Localization.AdministrationEmojiSizeLimit);

                    return;
                }

                name = name.Replace(" ", "_");
                emojiStream.Position = 0;
                await Context.Guild.CreateEmojiAsync(name, emojiStream);

                await ReplyConfirmationAsync(Localization.AdministrationEmojiCreated, name);
            }