osu-cascades / hackbot

Discord bot for the Cascades Tech Club Discord server
https://discordapp.com
4 stars 8 forks source link

Create LanguageRunner system and a command to execute user code #124

Closed Colin-Suckow closed 4 years ago

Colin-Suckow commented 4 years ago

Adds a run command that allows users to provide code for hackbot to run. Behind the scenes is system of LanguageRunners to execute code and provide the result. The whole LanguageRunner system is pretty much a copy and paste clone of the existing command system, so it should be familiar. Included in the commit is a LanguageRunner for rust.

I still need to add tests, so for now consider this PR as a discussion for improvements.

To use invoke the run command and provide a code block with a language name corresponding to the language hackbot should use to run it. The code block syntax follows all the rules of discord's codeblock, just make sure that there is a space between the command and the block. Also don't forget to specify the language.

Example input Example of command usage

Example output Example of command output It's just the stdout from the program inside a codeblock

ctsstc commented 4 years ago

Looks awesome, thanks for running the linter too ;)

To my understanding, this runs the REPL code in the cloud, so there's little concern about injection/executing arbitrary code and having someone modify hackbot or promote themselves; also keeps things simple on our side ^__^

This doesn't require an API key? :0 :HYPE:

Could you give an example of a call to the command and the expected output in the PR. -- I guess the tests will help demonstrate this. It might be tricky mocking up the endpoint, so let me know if you need a hand.


I'll have to look more at this later, but so far it looks good.

I'll have to digest the redundancy of the loader, I know why it's there, but maybe it can be DRY'd up if we make the original more flexible - if it fits design choice though.

Another point of interest to dig more into passing the loaded commands into the command context - if no other commands need access to this, maybe it can be a singleton/const that's at the top of the command file that loads up when it gets imported, that way it's only scoped to that module.

Colin-Suckow commented 4 years ago

Thanks for taking a look.

I agree 100% with the concerns about DRYness, doesn't feel quite right copy and pasting large amounts of code like that.

I'll go ahead and implement the singleton/const idea. This is my first time using typescript and wasn't sure you could do something like that.

I'll also edit the original PR to include an example. Completely slipped my mind :p

ctsstc commented 4 years ago

Sorry I never made it back to this, just catching up 👀

ctsstc commented 4 years ago

Woops, maybe should have kept the base branch as master until dev was merged into master.