public async Task RunBrainf(CommandContext ctx, [RemainingText] string userInput) { await ctx.Message.CreateReactionAsync(CustomEmoji.Loading); var slice = userInput.ExtractCode().Split('!', 2); string program = slice[0]; string programInput = slice.Length > 1 ? slice[1] : ""; var message = new StringBuilder(); var bf = new Brainfuck(program, programInput); try { message.Append(await bf.RunAsync(3000)); await ctx.AutoReactAsync(true); } catch (Exception e) { switch (e) { case Brainfuck.BrainfuckException be: message.AppendLine($"Runtime exception: {be.Message}"); if (e.InnerException is not null) { message.AppendLine($"Inner exception: {e.InnerException}\n"); } message.AppendLine($"Memory at this point: {be.Memory}\n"); message.AppendLine($"Output up to this point:\n{bf.Output}"); break; case ArgumentException _: message.AppendLine("The provided program has invalid syntax."); break; default: throw; } await ctx.AutoReactAsync(false); } await ctx.Message.DeleteOwnReactionAsync(CustomEmoji.Loading); await ctx.RespondAsync(message.Length == 0? "*No output*" : $"```\n{message.ToString().Truncate(1990)}```"); }
public async Task RunBrainf([Remainder] string userInput) { await Context.Message.AddReactionAsync(CustomEmoji.ELoading, DefaultOptions); var slice = userInput.ExtractCode().Split('!', 2); string program = slice[0]; string programInput = slice.Length > 1 ? slice[1] : ""; var message = new StringBuilder(); var bf = new Brainfuck(program, programInput); try { message.Append(await bf.RunAsync(3000)); await AutoReactAsync(true); } catch (Exception e) { switch (e) { case Brainfuck.BrainfuckException be: message.AppendLine($"Runtime exception: {be.Message}"); if (e.InnerException != null) { message.AppendLine($"Inner exception: {e.InnerException}\n"); } message.AppendLine($"Memory at this point: {be.Memory}\n"); message.AppendLine($"Output up to this point:\n{bf.Output}"); break; case ArgumentException ae: message.AppendLine("The provided program has invalid syntax."); break; default: throw e; } await AutoReactAsync(false); } await Context.Message.RemoveReactionAsync(CustomEmoji.ELoading, Context.Client.CurrentUser, DefaultOptions); await ReplyAsync(message.Length == 0? "*No output*" : $"```\n{message.ToString().Truncate(1990)}```"); }