Skip to content

Commit 069ac55

Browse files
Create a better eval experience for eval users (#31)
* Update eval to use a code template, and not return a bugged codeblock if nothing printed * ran black --------- Co-authored-by: Alex Nørgaard <[email protected]>
1 parent 458251e commit 069ac55

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

modules/eval.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from __future__ import annotations
2424

2525
import logging
26+
import textwrap
2627

2728
from discord.ext import commands
2829

@@ -34,6 +35,16 @@
3435
LOGGER = logging.getLogger(__name__)
3536

3637

38+
CODE = """
39+
def __user_code__():
40+
{user_code}
41+
42+
__value__ = __user_code__()
43+
if __value__:
44+
print(__value__)
45+
"""
46+
47+
3748
class Evaluation(core.Cog):
3849
"""Evaluation Cog.
3950
@@ -45,7 +56,9 @@ def __init__(self, bot: core.Bot, endpoint_url: str) -> None:
4556
self.eval_endpoint: str = endpoint_url
4657

4758
async def perform_eval(self, code: core.Codeblock) -> str:
48-
async with self.bot.session.post(self.eval_endpoint, json={"input": code.content}) as eval_response:
59+
formatted = CODE.format(user_code=textwrap.indent(code.content.replace("\t", " "), " "))
60+
61+
async with self.bot.session.post(self.eval_endpoint, json={"input": formatted}) as eval_response:
4962
if eval_response.status != 200:
5063
response_text = await eval_response.text()
5164
raise InvalidEval(eval_response.status, response_text)
@@ -70,14 +83,18 @@ async def eval(
7083

7184
async with ctx.typing():
7285
output = await self.perform_eval(code)
86+
await ctx.message.add_reaction("\U00002705")
7387

7488
if len(output) > 1000:
7589
codeblock = await self.bot.mb_client.create_paste(content=output, filename="eval.py")
7690

77-
else:
91+
elif output:
7892
codeblock = formatters.to_codeblock(output, escape_md=False)
7993

80-
await ctx.message.add_reaction("\U00002705")
94+
else:
95+
await ctx.send(f"Hey {ctx.author.display_name}, your code ran successfully, and returned nothing")
96+
return
97+
8198
await ctx.send(f"Hey {ctx.author.display_name}, here is your eval output:\n{codeblock}")
8299

83100
@eval.error

0 commit comments

Comments
 (0)