Exemplo n.º 1
0
        public async Task HeadpatCommand([Optional] IGuildUser User)
        {
            if (User == null)
            {
                User = Context.Guild.GetUser(Context.User.Id);
            }

            string NameOfUser = Regex.Replace(User.Username, "[^a-zA-Z]", "", RegexOptions.Compiled);

            if (NameOfUser.Length < 2)
            {
                NameOfUser = "******";
            }

            string ImageCacheDir = Path.Combine(Directory.GetCurrentDirectory(), "ImageCache");

            if (!Directory.Exists(ImageCacheDir))
            {
                Directory.CreateDirectory(ImageCacheDir);
            }

            string FilePath = Path.Join(ImageCacheDir, $"{NameOfUser}.gif");

            using (AnimatedGifCreator Gif = AnimatedGif.AnimatedGif.Create(FilePath, 80)) {
                string[] Files = Directory.GetFiles(FunConfiguration.HeadpatsDir, "*.png", SearchOption.AllDirectories);

                using WebClient WebClient       = new();
                using MemoryStream MemoryStream = new(WebClient.DownloadData(User.GetTrueAvatarUrl()));
                using Image PFPImage            = Image.FromStream(MemoryStream);

                for (int Index = 0; Index < Files.Length; Index++)
                {
                    using Image Headpat = Image.FromFile(Files[Index]);

                    using Bitmap DrawnImage = new(Headpat.Width, Headpat.Height);

                    List <ushort> HeadpatPos = FunConfiguration.HeadpatPositions[Index];

                    using (Graphics Graphics = Graphics.FromImage(DrawnImage)) {
                        Graphics.DrawImage(PFPImage, HeadpatPos[0], HeadpatPos[1], HeadpatPos[2], HeadpatPos[3]);
                        Graphics.DrawImage(Headpat, 0, 0);
                    }

                    await Gif.AddFrameAsync(DrawnImage, delay : -1, quality : GifQuality.Bit8);
                }
            }

            using (Discord.Image EmoteImage = new (FilePath)) {
                IGuild Guild = DiscordSocketClient.GetGuild(FunConfiguration.HeadpatStorageGuild);

                Console.WriteLine(NameOfUser.Length);
                Console.WriteLine(EmoteImage);

                GuildEmote PrevEmote = Guild.Emotes.Where(Emote => Emote.Name == NameOfUser).FirstOrDefault();

                if (PrevEmote != null)
                {
                    await Guild.DeleteEmoteAsync(PrevEmote);
                }

                GuildEmote Emote = await Guild.CreateEmoteAsync(NameOfUser, EmoteImage);

                DiscordWebhookClient Webhook = await CreateOrGetWebhook(Context.Channel.Id, FunConfiguration.HeadpatWebhookName);

                await Webhook.SendMessageAsync(
                    Emote.ToString(),
                    username : string.IsNullOrEmpty(Context.Guild.GetUser(Context.User.Id).Nickname)?Context.User.Username : Context.Guild.GetUser(Context.User.Id).Nickname,
                    avatarUrl : Context.User.GetTrueAvatarUrl()
                    );

                await Guild.DeleteEmoteAsync(Emote);
            }

            File.Delete(FilePath);
        }
Exemplo n.º 2
0
        public async Task Custom(string name, string fontName, string color, string fontSize, [Remainder] string text)
        {
            if (!Program.Owners.Contains(Context.User.Id))
            {
                await Utility.SendEmbedMessage(Context, "이 커맨드를 실행할 권한이 없습니다.");

                return;
            }

            name = new string(name.Where(c => alpha.Contains(c.ToString())).ToArray());
            if (name.Length == 1)
            {
                await Utility.SendEmbedMessage(Context, "이름이 한 글자 이하인 이모지를 만들 수 없습니다.");

                return;
            }

            FontFamily font = new FontFamily(fontName == "_" ? "NanumGothic" : fontName);

            Bitmap   img     = new Bitmap(512, 512);
            Graphics graphic = Graphics.FromImage(img);

            graphic.Clear(System.Drawing.Color.Transparent);

            graphic.InterpolationMode  = InterpolationMode.High;
            graphic.SmoothingMode      = SmoothingMode.HighQuality;
            graphic.TextRenderingHint  = TextRenderingHint.AntiAliasGridFit;
            graphic.CompositingQuality = CompositingQuality.HighQuality;

            int   len  = text.Length;
            float size = fontSize == "_" ? (len == 1 ? 300 : len <= 4 ? 225 : 175) : int.Parse(fontSize);

            GraphicsPath path = new GraphicsPath();

            path.AddString(text, font, style, size, rect, format);
            graphic.DrawPath(pen, path);
            graphic.FillPath(new SolidBrush(System.Drawing.Color.FromName(color == "_" ? "White" : color)), path);

            graphic.Dispose();

            SocketGuild guild = GetChoGuild(text[0]);

            if (guild == null)
            {
                await Utility.SendEmbedMessage(Context, "남은 이모지 슬롯이 없습니다.");

                return;
            }

            if (GetChoGuilds(text[0]).Where(g => g.Emotes.Where(e => e.Name == name).Count() != 0).Count() == 0)
            {
                if (guild.Emotes.Where(e => !e.Animated).Count() < 50)
                {
                    img.Save($"img/{name}.png");
                    img.Dispose();

                    GuildEmote emote = await guild.CreateEmoteAsync(name, new Discord.Image($"img/{name}.png"));
                    await ReplyAsync(emote.ToString());
                }
                else
                {
                    AnimatedGifEncoder e = new AnimatedGifEncoder();
                    e.SetTransparent(System.Drawing.Color.Black);
                    e.Start($"img/{name}.gif");

                    e.SetDelay(1);
                    e.SetRepeat(-1);

                    e.AddFrame(img);
                    e.AddFrame(img);

                    e.Finish();
                    img.Dispose();

                    GuildEmote emote = await guild.CreateEmoteAsync(name, new Discord.Image($"img/{name}.gif"));
                    await ReplyAsync(emote.ToString());
                }
            }
            else
            {
                await Utility.SendEmbedMessage(Context, "이미 존재하는 이모지입니다.");
            }
        }