aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Matteo Bertucci <[email protected]>2020-01-29 19:19:58 +0000
committerGravatar Matteo Bertucci <[email protected]>2020-01-29 19:19:58 +0000
commit6991e5fab13893c05d6f220e71f6ffc71509c1aa (patch)
treea7c756ddfefdc365de716a8363766d3dce3f63c6
parentDon't strip whitespaces during snekbox formatting (diff)
Re-eval snippet with emoji reaction
If the eval message is edited after less than 10 seconds, an emoji is added to the message, and if the user adds the same, the snippet is re-evaluated. This make easier to correct snipper mistakes.
-rw-r--r--bot/cogs/snekbox.py69
1 files changed, 46 insertions, 23 deletions
diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py
index 81951efd3..1688c0278 100644
--- a/bot/cogs/snekbox.py
+++ b/bot/cogs/snekbox.py
@@ -1,3 +1,4 @@
+import asyncio
import datetime
import logging
import re
@@ -200,32 +201,54 @@ class Snekbox(Cog):
log.info(f"Received code from {ctx.author} for evaluation:\n{code}")
- self.jobs[ctx.author.id] = datetime.datetime.now()
- code = self.prepare_input(code)
+ while True:
+ self.jobs[ctx.author.id] = datetime.datetime.now()
+ code = self.prepare_input(code)
- try:
- async with ctx.typing():
- results = await self.post_eval(code)
- msg, error = self.get_results_message(results)
-
- if error:
- output, paste_link = error, None
- else:
- output, paste_link = await self.format_output(results["stdout"])
-
- icon = self.get_status_emoji(results)
- msg = f"{ctx.author.mention} {icon} {msg}.\n\n```py\n{output}\n```"
- if paste_link:
- msg = f"{msg}\nFull output: {paste_link}"
-
- response = await ctx.send(msg)
- self.bot.loop.create_task(
- wait_for_deletion(response, user_ids=(ctx.author.id,), client=ctx.bot)
+ try:
+ async with ctx.typing():
+ results = await self.post_eval(code)
+ msg, error = self.get_results_message(results)
+
+ if error:
+ output, paste_link = error, None
+ else:
+ output, paste_link = await self.format_output(results["stdout"])
+
+ icon = self.get_status_emoji(results)
+ msg = f"{ctx.author.mention} {icon} {msg}.\n\n```py\n{output}\n```"
+ if paste_link:
+ msg = f"{msg}\nFull output: {paste_link}"
+
+ response = await ctx.send(msg)
+ self.bot.loop.create_task(
+ wait_for_deletion(response, user_ids=(ctx.author.id,), client=ctx.bot)
+ )
+
+ log.info(f"{ctx.author}'s job had a return code of {results['returncode']}")
+ finally:
+ del self.jobs[ctx.author.id]
+
+ try:
+ _, new_message = await self.bot.wait_for(
+ 'message_edit',
+ check=lambda o, n: n.id == ctx.message.id and o.content != n.content,
+ timeout=10
+ )
+ await ctx.message.add_reaction('🔁')
+ await self.bot.wait_for(
+ 'reaction_add',
+ check=lambda r, u: r.message.id == ctx.message.id and u.id == ctx.author.id and str(r) == '🔁',
+ timeout=10
)
- log.info(f"{ctx.author}'s job had a return code of {results['returncode']}")
- finally:
- del self.jobs[ctx.author.id]
+ log.info(f"Re-evaluating message {ctx.message.id}")
+ code = new_message.content.split(' ', maxsplit=1)[1]
+ await ctx.message.clear_reactions()
+ await response.delete()
+ except asyncio.TimeoutError:
+ await ctx.message.clear_reactions()
+ return
def setup(bot: Bot) -> None: