public async Task CarbonCode(CommandContext ctx, string theme, [RemainingText] string code) { if (theme != null && code == null) { await ctx.Channel.SendMessageAsync("Using this command requires you to specify a theme, use `jx themes` to get a list of all available themes"); return; } if (!JynxExtensions.TryParseCodeBlock(code, out code)) { await ctx.RespondAsync("You need to wrap the code in a code block"); return; } var cs = code.ParseCodeBlock(); cs = cs.Remove(cs.LastIndexOf("\n", StringComparison.OrdinalIgnoreCase)); var csUrl = WebUtility.UrlEncode(cs); string validTheme = CarbonService.ThemeMatcher(theme); var codeEmbed = CarbonService.BuildCarbonEmbed(ctx.Member.Username, validTheme, csUrl); await ctx.Channel.SendMessageAsync(codeEmbed); }
public async Task EvalLua(CommandContext ctx, [RemainingText] string code) { if (!JynxExtensions.TryParseCodeBlock(code, out code)) { await ctx.RespondAsync("You need to wrap the code in a code block"); return; } var cs = code.ParseCodeBlock(); try { Script script = new Script(); var res = script.DoString(cs); object resString = res.Type switch { DataType.String => res.String, DataType.Number => res.Number, DataType.Boolean => res.Boolean, DataType.Function => res.Function, DataType.Nil => "null", DataType.Void => "null", DataType.Table => res.Table, DataType.Tuple => res.Tuple, DataType.UserData => "null", DataType.Thread => "null", DataType.ClrFunction => res.Callback.Name, DataType.TailCallRequest => res.UserData.Descriptor.Name, DataType.YieldRequest => "null", _ => "null" }; var resEmbed = new DiscordEmbedBuilder() .WithTitle("Evaluation Success") .WithDescription($"Value\n```lua\n{resString}\n```") .WithColor(JynxCosmetics.JynxColor) .Build(); await ctx.RespondAsync(resEmbed); } catch (Exception e) { if (e is ScriptRuntimeException runtimeException) { var errEmbed = new DiscordEmbedBuilder() .WithTitle("An error occurred, Runtime Exception") .WithDescription($"```lua\n{runtimeException.DecoratedMessage}\n```") .WithColor(JynxCosmetics.JynxColor) .Build(); await ctx.RespondAsync(errEmbed); } else if (e is SyntaxErrorException syntaxException) { var errEmbed = new DiscordEmbedBuilder() .WithTitle($"An error occurred, Syntax Exception") .WithDescription($"```lua\n{syntaxException.DecoratedMessage}\n```") .WithColor(JynxCosmetics.JynxColor) .Build(); await ctx.RespondAsync(errEmbed); } } }