|
| 1 | +#nullable enable |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Discord; |
| 5 | +using Discord.Interactions; |
| 6 | +using Modix.Services.AutoRemoveMessage; |
| 7 | +using Modix.Services.CommandHelp; |
| 8 | +using Modix.Services.Godbolt; |
| 9 | + |
| 10 | +namespace Modix.Bot.Modules |
| 11 | +{ |
| 12 | + [ModuleHelp("Godbolt", "Commands for working with Godbolt.")] |
| 13 | + public class GodboltModule : InteractionModuleBase |
| 14 | + { |
| 15 | + private readonly IAutoRemoveMessageService _autoRemoveMessageService; |
| 16 | + private readonly GodboltService _godboltService; |
| 17 | + |
| 18 | + public GodboltModule(IAutoRemoveMessageService autoRemoveMessageService, GodboltService godboltService) |
| 19 | + { |
| 20 | + _autoRemoveMessageService = autoRemoveMessageService; |
| 21 | + _godboltService = godboltService; |
| 22 | + } |
| 23 | + |
| 24 | + [SlashCommand("godbolt", "Compile and disassemble the JIT code.")] |
| 25 | + public async Task DisasmAsync( |
| 26 | + [Summary(description: "The code to compiler.")] string code, |
| 27 | + [Summary(description: "The language of code.")] Language language = Language.CSharp, |
| 28 | + [Summary(description: "Arguments to pass to the compiler.")] string arguments = "", |
| 29 | + [Summary(description: "Execute the code.")] bool execute = false) |
| 30 | + { |
| 31 | + var langId = language switch |
| 32 | + { |
| 33 | + Language.CSharp => "csharp", |
| 34 | + Language.FSharp => "fsharp", |
| 35 | + Language.VisualBasic => "vb", |
| 36 | + Language.IL => "il", |
| 37 | + _ => throw new ArgumentOutOfRangeException(nameof(language)) |
| 38 | + }; |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + var response = $""" |
| 43 | + ```asm |
| 44 | + {await _godboltService.CompileAsync(code, langId, arguments, execute)} |
| 45 | + ``` |
| 46 | + """; |
| 47 | + |
| 48 | + if (response.Length > DiscordConfig.MaxMessageSize) |
| 49 | + { |
| 50 | + await FollowupAsync("Error: The disassembly code is too long to be converted to a message."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + await FollowupAsync(text: response); |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + await FollowupAsync($"Error: {ex}"); |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public enum Language |
| 64 | + { |
| 65 | + [ChoiceDisplay("C#")] |
| 66 | + CSharp, |
| 67 | + [ChoiceDisplay("F#")] |
| 68 | + FSharp, |
| 69 | + [ChoiceDisplay("VB.NET")] |
| 70 | + VisualBasic, |
| 71 | + [ChoiceDisplay("IL")] |
| 72 | + IL |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments