it sorta works, still need to actually use interpreter
This commit is contained in:
		| @@ -1,8 +1,10 @@ | |||||||
| using Discord; | using Discord; | ||||||
| using Discord.Commands; | using Discord.Commands; | ||||||
| using NadekoBot.Classes; | using NadekoBot.Classes; | ||||||
|  | using System.Collections.Concurrent; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.Diagnostics; | using System.Diagnostics; | ||||||
|  | using System.Text; | ||||||
| using System.Threading; | using System.Threading; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
|  |  | ||||||
| @@ -13,7 +15,7 @@ namespace NadekoBot.Modules.Programming.Commands | |||||||
| { | { | ||||||
|     class HaskellRepl : DiscordCommand |     class HaskellRepl : DiscordCommand | ||||||
|     { |     { | ||||||
|         Queue<KeyValuePair<string, Channel>> commandQueue = new Queue<KeyValuePair<string, Channel>>(); |         ConcurrentQueue<KeyValuePair<string, Channel>> commandQueue = new ConcurrentQueue<KeyValuePair<string, Channel>>(); | ||||||
|  |  | ||||||
|         Thread haskellThread; |         Thread haskellThread; | ||||||
|  |  | ||||||
| @@ -23,27 +25,51 @@ namespace NadekoBot.Modules.Programming.Commands | |||||||
|  |  | ||||||
|             haskellThread = new Thread(new ThreadStart(() => |             haskellThread = new Thread(new ThreadStart(() => | ||||||
|             { |             { | ||||||
|                 Process.Start(new ProcessStartInfo |                 var p = Process.Start(new ProcessStartInfo | ||||||
|                 { |                 { | ||||||
|                     FileName = "stack", //shouldn't use repl, but a Language.Haskell.Interpreter somehow |                     FileName = "stack", //shouldn't use repl, but a Language.Haskell.Interpreter somehow | ||||||
|                     Arguments = "repl" |                     Arguments = "repl", | ||||||
|  |                     UseShellExecute = false, | ||||||
|  |                     RedirectStandardInput = true, | ||||||
|  |                     RedirectStandardOutput = true, | ||||||
|  |                     CreateNoWindow = true, | ||||||
|  |                 }); | ||||||
|  |  | ||||||
|  |                 Task.Run(async () => | ||||||
|  |                 { | ||||||
|  |                     while (true) | ||||||
|  |                     { | ||||||
|  |                         while (commandQueue.Count == 0) | ||||||
|  |                             await Task.Delay(100); | ||||||
|  |  | ||||||
|  |                         //read from queue | ||||||
|  |                         KeyValuePair<string, Channel> com; | ||||||
|  |                         if (!commandQueue.TryDequeue(out com)) | ||||||
|  |                         { | ||||||
|  |                             await Task.Delay(100); | ||||||
|  |                             continue; | ||||||
|  |                         } | ||||||
|  |                         //var bytes = Encoding.ASCII.GetBytes(com.Key); | ||||||
|  |  | ||||||
|  |                         //send the command to the process | ||||||
|  |                         p.StandardInput.WriteLine(com.Key); | ||||||
|  |  | ||||||
|  |                         //wait 50 ms for execution | ||||||
|  |                         await Task.Delay(50); | ||||||
|  |  | ||||||
|  |                         //read everything from the output | ||||||
|  |                         var outBuffer = new byte[1500]; | ||||||
|  |  | ||||||
|  |                         p.StandardOutput.BaseStream.Read(outBuffer, 0, 1500); | ||||||
|  |  | ||||||
|  |                         var outStr = Encoding.ASCII.GetString(outBuffer); | ||||||
|  |                         //send to channel | ||||||
|  |                         await com.Value.SendMessage($"```hs\nPrelude> {com.Key}\n" + outStr + "\n```"); | ||||||
|  |                     } | ||||||
|                 }); |                 }); | ||||||
|  |  | ||||||
|             })); |             })); | ||||||
|  |             haskellThread.Start(); | ||||||
|  |  | ||||||
|             Task.Run(() => |  | ||||||
|             { |  | ||||||
|                 //read from queue |  | ||||||
|  |  | ||||||
|                 //send the command to the process |  | ||||||
|  |  | ||||||
|                 //wait 50 ms for execution |  | ||||||
|  |  | ||||||
|                 //read everything from the output |  | ||||||
|  |  | ||||||
|                 //send to chanenl |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -51,9 +77,15 @@ namespace NadekoBot.Modules.Programming.Commands | |||||||
|         { |         { | ||||||
|             cgb.CreateCommand(Module.Prefix + "hs") |             cgb.CreateCommand(Module.Prefix + "hs") | ||||||
|                 .Description("Executes a haskell express with LAMBDABOT") |                 .Description("Executes a haskell express with LAMBDABOT") | ||||||
|                 .Do(async e => |                 .Parameter("command", ParameterType.Unparsed) | ||||||
|  |                 .Do(e => | ||||||
|                 { |                 { | ||||||
|  |                     var com = e.GetArg("command")?.Trim(); | ||||||
|  |                     if (string.IsNullOrWhiteSpace(com)) | ||||||
|  |                         return; | ||||||
|  |  | ||||||
|                     //send a command and a channel to the queue |                     //send a command and a channel to the queue | ||||||
|  |                     commandQueue.Enqueue(new KeyValuePair<string, Channel>(com, e.Channel)); | ||||||
|                 }); |                 }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -16,6 +16,7 @@ using NadekoBot.Modules.NSFW; | |||||||
| using NadekoBot.Modules.Permissions; | using NadekoBot.Modules.Permissions; | ||||||
| using NadekoBot.Modules.Permissions.Classes; | using NadekoBot.Modules.Permissions.Classes; | ||||||
| using NadekoBot.Modules.Pokemon; | using NadekoBot.Modules.Pokemon; | ||||||
|  | using NadekoBot.Modules.Programming; | ||||||
| using NadekoBot.Modules.Searches; | using NadekoBot.Modules.Searches; | ||||||
| using NadekoBot.Modules.Translator; | using NadekoBot.Modules.Translator; | ||||||
| using NadekoBot.Modules.Trello; | using NadekoBot.Modules.Trello; | ||||||
| @@ -183,6 +184,7 @@ namespace NadekoBot | |||||||
|             modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None); |             modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None); | ||||||
|             modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None); |             modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None); | ||||||
|             modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None); |             modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None); | ||||||
|  |             modules.Add(new ProgrammingModule(), "Programming", ModuleFilter.None); | ||||||
|             if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey)) |             if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey)) | ||||||
|                 modules.Add(new TrelloModule(), "Trello", ModuleFilter.None); |                 modules.Add(new TrelloModule(), "Trello", ModuleFilter.None); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -123,6 +123,8 @@ | |||||||
|     <Compile Include="Modules\ClashOfClans\ClashOfClans.cs" /> |     <Compile Include="Modules\ClashOfClans\ClashOfClans.cs" /> | ||||||
|     <Compile Include="Classes\DBHandler.cs" /> |     <Compile Include="Classes\DBHandler.cs" /> | ||||||
|     <Compile Include="Classes\FlowersHandler.cs" /> |     <Compile Include="Classes\FlowersHandler.cs" /> | ||||||
|  |     <Compile Include="Modules\Programming\Commands\HaskellRepl.cs" /> | ||||||
|  |     <Compile Include="Modules\Programming\ProgrammingModule.cs" /> | ||||||
|     <Compile Include="Modules\Searches\Commands\IMDB\ImdbMovie.cs" /> |     <Compile Include="Modules\Searches\Commands\IMDB\ImdbMovie.cs" /> | ||||||
|     <Compile Include="Modules\Searches\Commands\IMDB\ImdbScraper.cs" /> |     <Compile Include="Modules\Searches\Commands\IMDB\ImdbScraper.cs" /> | ||||||
|     <Compile Include="Classes\IncidentsHandler.cs" /> |     <Compile Include="Classes\IncidentsHandler.cs" /> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user