Ejemplo n.º 1
0
        public async Task RotateRoleColor(int timeout, IRole role, params string[] hexes)
        {
            var channel = (ITextChannel)Context.Channel;

            if ((timeout < 60 && timeout != 0) || timeout > 3600)
                return;

            Timer t;
            if (timeout == 0 || hexes.Length == 0)
            {
                if (_rotatingRoleColors.TryRemove(role.Id, out t))
                {
                    t.Change(Timeout.Infinite, Timeout.Infinite);
                    await ReplyConfirmLocalized("rrc_stop", Format.Bold(role.Name)).ConfigureAwait(false);
                }
                return;
            }
            
            var hexColors = hexes.Select(hex =>
            {
                try { return (ImageSharp.Color?)ImageSharp.Color.FromHex(hex.Replace("#", "")); } catch { return null; }
            })
            .Where(c => c != null)
            .Select(c => c.Value)
            .ToArray();

            if (!hexColors.Any())
            {
                await ReplyErrorLocalized("rrc_no_colors").ConfigureAwait(false);
                return;
            }

            var images = hexColors.Select(color =>
            {
                var img = new ImageSharp.Image(50, 50);
                img.BackgroundColor(color);
                return img;
            }).Merge().ToStream();

            var i = 0;
            t = new Timer(async (_) =>
            {
                try
                {
                    var color = hexColors[i];
                    await role.ModifyAsync(r => r.Color = new Discord.Color(color.R, color.G, color.B)).ConfigureAwait(false);
                    ++i;
                    if (i >= hexColors.Length)
                        i = 0;
                }
                catch { }
            }, null, 0, timeout * 1000);

            _rotatingRoleColors.AddOrUpdate(role.Id, t, (key, old) =>
            {
                old.Change(Timeout.Infinite, Timeout.Infinite);
                return t;
            });
            await channel.SendFileAsync(images, "magicalgirl.jpg", GetText("rrc_start", Format.Bold(role.Name))).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public async Task Color([Remainder] string color = null)
        {
            color = color?.Trim().Replace("#", "");
            if (string.IsNullOrWhiteSpace(color))
            {
                return;
            }
            var img = new ImageSharp.Image(50, 50);

            img.BackgroundColor(new ImageSharp.Color(color));

            await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png").ConfigureAwait(false);;
        }
Ejemplo n.º 3
0
        public MemoryStream SpoilerTxt(string spoil)
        {
            var             fill  = Brushes.Solid <Rgba32>(Rgba32.Black);
            var             brush = Brushes.Solid <Rgba32>(Rgba32.White);
            RendererOptions op    = new RendererOptions(_Spoiltxt);
            var             text  = WordWrap(spoil, 120);
            var             tsize = TextMeasurer.MeasureBounds(text, op);

            int[] size = { Convert.ToInt32(tsize.Width) + 20, Convert.ToInt32(tsize.Height) + 20 };
            var   img2 = new Image <Rgba32>(size[0], size[1]);

            img2.DrawText(text, _Spoiltxt, brush, new PointF(0, 0), new TextGraphicsOptions(true));
            img2.BackgroundColor(Rgba32.Black);
            var img1 = new Image <Rgba32>(img2.Width, img2.Height);

            _Spoil = _fonts.Find("Whitney-Bold").CreateFont(img2.Width * 0.064f);
            var rect = new Rectangle(0, 0, img2.Width, img2.Height);

            img1.BackgroundColor(Rgba32.Black, rect);
            img1.DrawText("Spoiler Warning" + "\r\n" + "(Mouse over to view)", _Spoil, brush, new PointF(0, 0), new TextGraphicsOptions(true)
            {
                WrapTextWidth = img1.Width - 20
            });
            MagickImage f1 = new MagickImage(img1.ToStream());
            MagickImage f2 = new MagickImage(img2.ToStream());

            MagickImage[] frames = { f1, f2 };
            MemoryStream  ms     = new MemoryStream();

            using (MagickImageCollection gif = new MagickImageCollection())
            {
                gif.Add(f1);
                gif.Add(f2);

                for (int i = 0; i < gif.Count; i++)
                {
                    gif[i].AnimationDelay      = 50;
                    gif[i].AnimationIterations = 1;
                }
                QuantizeSettings settings = new QuantizeSettings
                {
                    Colors       = 256,
                    DitherMethod = DitherMethod.No
                };
                gif.Quantize(settings);
                gif.Optimize();
                gif.Write(ms, MagickFormat.Gif);
                ms.Position = 0;
            }
            return(ms);
        }
Ejemplo n.º 4
0
        public async Task Color([Remainder] string color = null)
        {
            color = color?.Trim().Replace("#", "");
            if (string.IsNullOrWhiteSpace((string)color))
            {
                return;
            }
            var img = new ImageSharp.Image(50, 50);

            var red   = Convert.ToInt32(color.Substring(0, 2), 16);
            var green = Convert.ToInt32(color.Substring(2, 2), 16);
            var blue  = Convert.ToInt32(color.Substring(4, 2), 16);

            img.BackgroundColor(new ImageSharp.Color(color));

            await Context.Channel.SendFileAsync(img.ToStream(), $"{color}.png");
        }
Ejemplo n.º 5
0
 public Task <MagickImageCollection> SpoilerImg(string uri) => Task.Run(async() =>
 {
     //load image
     var img2 = await DownloadImageAsync(uri);
     //create spoiler warning image
     Image <Rgba32> img1 = new Image <Rgba32>(img2.Width, img2.Height);
     _Spoil    = _fonts.Find("Whitney-Bold").CreateFont(img2.Width * 0.064f);
     var fill  = Brushes.Solid <Rgba32>(Rgba32.Black);
     var brush = Brushes.Solid <Rgba32>(Rgba32.White);
     var rect  = new Rectangle(0, 0, img2.Width, img2.Height);
     img1.BackgroundColor(Rgba32.Black, rect);
     img1.DrawText("Spoiler Warning" + "\r\n" + "(Mouse over to view)", _Spoil, brush, new PointF(10, 10), new TextGraphicsOptions(true)
     {
         WrapTextWidth = img1.Width - 20
     });
     MagickImage f1       = new MagickImage(img1.ToStream());
     MagickImage f2       = new MagickImage(img2.ToStream());
     MagickImage[] frames = { f1, f2 };
     var gifs             = GetGif(frames);
     var gif = new MagickImageCollection(gifs);
     return(gif);
 });
Ejemplo n.º 6
0
        public async Task RotateRoleColor(int timeout, IRole role, params string[] hexes)
        {
            var channel = (ITextChannel)Context.Channel;

            if ((timeout < 60 && timeout != 0) || timeout > 3600)
            {
                return;
            }

            Timer t;

            if (timeout == 0 || hexes.Length == 0)
            {
                if (rotatingRoleColors.TryRemove(role.Id, out t))
                {
                    t.Change(Timeout.Infinite, Timeout.Infinite);
                    await channel.SendConfirmAsync($"Stopped rotating colors for the **{role.Name}** role").ConfigureAwait(false);
                }
                return;
            }

            var hexColors = hexes.Select(hex =>
            {
                try { return((ImageSharp.Color?) new ImageSharp.Color(hex.Replace("#", ""))); } catch { return(null); }
            })
                            .Where(c => c != null)
                            .Select(c => c.Value)
                            .ToArray();

            if (!hexColors.Any())
            {
                await channel.SendMessageAsync("No colors are in the correct format. Use `#00ff00` for example.").ConfigureAwait(false);

                return;
            }

            var images = hexColors.Select(color =>
            {
                var img = new ImageSharp.Image(50, 50);
                img.BackgroundColor(color);
                return(img);
            }).Merge().ToStream();

            var i = 0;

            t = new Timer(async(_) =>
            {
                try
                {
                    var color = hexColors[i];
                    await role.ModifyAsync(r => r.Color = new Discord.Color(color.R, color.G, color.B)).ConfigureAwait(false);
                    ++i;
                    if (i >= hexColors.Length)
                    {
                        i = 0;
                    }
                }
                catch { }
            }, null, 0, timeout * 1000);

            rotatingRoleColors.AddOrUpdate(role.Id, t, (key, old) =>
            {
                old.Change(Timeout.Infinite, Timeout.Infinite);
                return(t);
            });

            await channel.SendFileAsync(images, "magicalgirl.jpg", $"Rotating **{role.Name}** role's color.").ConfigureAwait(false);
        }