[Dev] Add repl pause (#4366)

* [Dev] Add repl pause

* Update redbot/core/dev_commands.py

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* Address reviews

* Address reviews x2

* Small consistency fix

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Neuro Assassin 2020-08-25 07:24:40 -04:00 committed by GitHub
parent eeb5f651b2
commit 3534e59cb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ from copy import copy
import discord import discord
from . import checks, commands from . import checks, commands
from .commands import NoParseOptional as Optional
from .i18n import Translator from .i18n import Translator
from .utils.chat_formatting import box, pagify from .utils.chat_formatting import box, pagify
from .utils.predicates import MessagePredicate from .utils.predicates import MessagePredicate
@ -43,7 +44,7 @@ class Dev(commands.Cog):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._last_result = None self._last_result = None
self.sessions = set() self.sessions = {}
@staticmethod @staticmethod
def async_compile(source, filename, mode): def async_compile(source, filename, mode):
@ -214,7 +215,7 @@ class Dev(commands.Cog):
await ctx.send_interactive(self.get_pages(msg), box_lang="py") await ctx.send_interactive(self.get_pages(msg), box_lang="py")
@commands.command() @commands.group(invoke_without_command=True)
@checks.is_owner() @checks.is_owner()
async def repl(self, ctx): async def repl(self, ctx):
"""Open an interactive REPL. """Open an interactive REPL.
@ -237,22 +238,36 @@ class Dev(commands.Cog):
} }
if ctx.channel.id in self.sessions: if ctx.channel.id in self.sessions:
if self.sessions[ctx.channel.id]:
await ctx.send( await ctx.send(
_("Already running a REPL session in this channel. Exit it with `quit`.") _("Already running a REPL session in this channel. Exit it with `quit`.")
) )
else:
await ctx.send(
_(
"Already running a REPL session in this channel. Resume the REPL with `{}repl resume`."
).format(ctx.prefix)
)
return return
self.sessions.add(ctx.channel.id) self.sessions[ctx.channel.id] = True
await ctx.send(_("Enter code to execute or evaluate. `exit()` or `quit` to exit.")) await ctx.send(
_(
"Enter code to execute or evaluate. `exit()` or `quit` to exit. `{}repl pause` to pause."
).format(ctx.prefix)
)
while True: while True:
response = await ctx.bot.wait_for("message", check=MessagePredicate.regex(r"^`", ctx)) response = await ctx.bot.wait_for("message", check=MessagePredicate.regex(r"^`", ctx))
if not self.sessions[ctx.channel.id]:
continue
cleaned = self.cleanup_code(response.content) cleaned = self.cleanup_code(response.content)
if cleaned in ("quit", "exit", "exit()"): if cleaned in ("quit", "exit", "exit()"):
await ctx.send(_("Exiting.")) await ctx.send(_("Exiting."))
self.sessions.remove(ctx.channel.id) del self.sessions[ctx.channel.id]
return return
executor = None executor = None
@ -305,6 +320,22 @@ class Dev(commands.Cog):
except discord.HTTPException as e: except discord.HTTPException as e:
await ctx.send(_("Unexpected error: `{}`").format(e)) await ctx.send(_("Unexpected error: `{}`").format(e))
@repl.command(aliases=["resume"])
async def pause(self, ctx, toggle: Optional[bool] = None):
"""Pauses/resumes the REPL running in the current channel"""
if ctx.channel.id not in self.sessions:
await ctx.send(_("There is no currently running REPL session in this channel."))
return
if toggle is None:
toggle = not self.sessions[ctx.channel.id]
self.sessions[ctx.channel.id] = toggle
if toggle:
await ctx.send(_("The REPL session in this channel has been resumed."))
else:
await ctx.send(_("The REPL session in this channel is now paused."))
@commands.command() @commands.command()
@checks.is_owner() @checks.is_owner()
async def mock(self, ctx, user: discord.Member, *, command): async def mock(self, ctx, user: discord.Member, *, command):