Ejemplo n.º 1
0
        public static LocalEmbedBuilder CreateEmbed(EmbedType type, string title, string content = null)
        {
            LocalEmbedBuilder embed = new LocalEmbedBuilder();

            switch (type)
            {
            case EmbedType.Info:
                embed.WithAuthor(title);
                embed.WithColor(new Color(67, 181, 129));
                break;

            case EmbedType.Success:
                embed.WithAuthor(title, "https://i.imgur.com/XnVa7ta.png");
                embed.WithColor(new Color(67, 181, 129));
                break;

            case EmbedType.Failure:
                embed.WithAuthor(title, "https://i.imgur.com/Sg4663k.png");
                embed.WithColor(new Color(67, 181, 129));
                break;
            }

            embed.WithDescription(content);

            return(embed);
        }
Ejemplo n.º 2
0
        public static bool TryParseEmbed(string json, out LocalEmbedBuilder embed)
        {
            embed = new LocalEmbedBuilder();
            try
            {
                var embedDeserialized = JsonConvert.DeserializeObject <JsonEmbed>(json);

                var author      = embedDeserialized.Author;
                var title       = embedDeserialized.Title;
                var description = embedDeserialized.Description;

                var colorString = embedDeserialized.Color;
                var thumbnail   = embedDeserialized.Thumbnail;
                var image       = embedDeserialized.Image;
                var fields      = embedDeserialized.Fields;
                var footer      = embedDeserialized.Footer;
                var timestamp   = embedDeserialized.Timestamp;

                if (author != null)
                {
                    embed.WithAuthor(author);
                }

                if (!string.IsNullOrEmpty(title))
                {
                    embed.WithTitle(title);
                }

                if (!string.IsNullOrEmpty(description))
                {
                    embed.WithDescription(description);
                }

                if (!string.IsNullOrEmpty(colorString))
                {
                    embed.WithColor(HexToInt(colorString) ?? 0xFFFFFF);
                }

                if (!string.IsNullOrEmpty(thumbnail))
                {
                    embed.WithThumbnailUrl(thumbnail);
                }
                if (!string.IsNullOrEmpty(image))
                {
                    embed.WithImageUrl(image);
                }

                if (fields != null)
                {
                    foreach (var field in fields)
                    {
                        var fieldName   = field.Name;
                        var fieldValue  = field.Value;
                        var fieldInline = field.IsInline;

                        if (!string.IsNullOrEmpty(fieldName) && !string.IsNullOrEmpty(fieldValue))
                        {
                            embed.AddField(fieldName, fieldValue, fieldInline);
                        }
                    }
                }

                if (footer != null)
                {
                    embed.WithFooter(footer);
                }

                if (timestamp.HasValue)
                {
                    embed.WithTimestamp(timestamp.Value);
                }
                else if (embedDeserialized.WithCurrentTimestamp)
                {
                    embed.WithCurrentTimestamp();
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
 public static LocalEmbedBuilder WithDefaultColor(this LocalEmbedBuilder eb)
 => eb.WithColor(Global.DefaultEmbedColor);
Ejemplo n.º 4
0
        public async Task Eval([Remainder] string code)
        {
            var builder = new LocalEmbedBuilder
            {
                Title       = "Evaluating Code...",
                Color       = Color.DarkRed,
                Description = "Waiting for completion...",
                Author      = new LocalEmbedAuthorBuilder
                {
                    IconUrl = Context.User.GetAvatarUrl(),
                    Name    = Context.User.DisplayName
                },
                Timestamp    = DateTimeOffset.UtcNow,
                ThumbnailUrl = Context.Guild.CurrentMember.GetAvatarUrl()
            };
            var msg = await ReplyAsync(embed : builder);

            var sw     = Stopwatch.StartNew();
            var script = EvalService.Build(code);

            string snippet = string.Join(Environment.NewLine, script.Code.Split(Environment.NewLine.ToCharArray()).Where(line => !line.StartsWith("using")));

            var diagnostics     = script.Compile();
            var compilationTime = sw.ElapsedMilliseconds;

            if (diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
            {
                builder.WithDescription($"Compilation finished in: {compilationTime}ms");
                builder.WithColor(Color.Red);
                builder.WithTitle("Failed Evaluation");

                builder.AddField("Code", $"```cs{Environment.NewLine}{snippet}```");
                builder.AddField("Compilation Errors", string.Join('\n', diagnostics.Select(x => $"{x}")));

                await msg.ModifyAsync(x => x.Embed = builder.Build());

                return;
            }
            sw.Restart();

            var context = new RoslynContext(Context, Services);

            try
            {
                var result = await script.RunAsync(context);

                sw.Stop();
                builder.WithColor(Color.Green);

                builder.WithDescription($"Code compiled in {compilationTime}ms and ran in {sw.ElapsedMilliseconds}ms");
                builder.WithTitle("Code Evaluated");
                builder.AddField("Code", $"```cs{Environment.NewLine}{snippet}```");

                if (!(result.ReturnValue is null))
                {
                    var sb     = new StringBuilder();
                    var type   = result.ReturnValue.GetType();
                    var rValue = result.ReturnValue;

                    switch (rValue)
                    {
                    case Color col:
                        builder.WithColor(col);
                        builder.AddField("Colour", $"{col.RawValue}");
                        break;

                    case string str:
                        builder.AddField($"{type}", $"\"{str}\"");
                        break;

                    case IEnumerable enumerable:

                        var list     = enumerable.Cast <object>().ToList();
                        var enumType = enumerable.GetType();

                        if (list.Count > 25)
                        {
                            builder.AddField($"{enumType}", $"Enumerable has more than 10 elements ({list.Count})");
                            break;
                        }

                        if (list.Count > 0)
                        {
                            sb.AppendLine("```css");

                            foreach (var element in list)
                            {
                                sb.Append('[').Append(element).AppendLine("]");
                            }

                            sb.AppendLine("```");
                        }
                        else
                        {
                            sb.AppendLine("Collection is empty");
                        }

                        builder.AddField($"{enumType}", sb.ToString());

                        break;

                    case Enum @enum:

                        builder.AddField($"{@enum.GetType()}", $"```\n{@enum.Humanize()}\n```");

                        break;

                    default:

                        var messages = rValue.Inspect();

                        if (type.IsValueType && messages.Count == 0)
                        {
                            builder.AddField($"{type}", rValue);
                        }

                        foreach (var message in messages)
                        {
                            await ReplyAsync($"```css\n{message}\n```");
                        }

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                sw.Stop();


                builder.WithDescription($"Code evaluated in {sw.ElapsedMilliseconds}ms but there was a issue tho");
                builder.WithColor(Color.Red);
                builder.WithTitle("Failed Evaluation");
                builder.AddField("Code", $"```cs{Environment.NewLine}{snippet}```");

                var str = ex.ToString();

                builder.AddField("Exception", Format.Sanitize(str.Length >= 1000 ? str.Substring(0, 1000) : str));
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            await msg.ModifyAsync(x => x.Embed = builder.Build());
        }
 public LocalizedEmbedBuilder WithColor(Color?color)
 {
     _builder.WithColor(color);
     return(this);
 }
Ejemplo n.º 6
0
 public static LocalEmbedBuilder WithErrorColor(this LocalEmbedBuilder builder)
 => builder.WithColor(Config.ErrorColor);
Ejemplo n.º 7
0
 public static LocalEmbedBuilder WithWarnColor(this LocalEmbedBuilder builder)
 => builder.WithColor(Config.WarnColor);
Ejemplo n.º 8
0
 public static LocalEmbedBuilder WithSuccessColor(this LocalEmbedBuilder builder)
 => builder.WithColor(Config.SuccessColor);
 /// <summary>
 /// Sends an <paramref name="embed"/> to a <paramref name="channel"/>, with an optional <paramref name="msg"/>.
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="embed"></param>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static Task <RestUserMessage> EmbedAsync(this IMessageChannel channel, LocalEmbedBuilder embed, string msg = "")
 => channel.SendMessageAsync(msg, embed: embed.WithColor(Color.LightGreen).Build());