[LoadDev] Add eval button creation

- Eval button creates a button generating a Modal to input code which is then executed from the context of an interaction allowing debugging of interactions.
- The view is also added to the dev env so it can be sent through another eval.
- Add url command to debug and view information about a provided url.
This commit is contained in:
TrustyJAID committed 2024-08-17 09:30:45 -06:00
1 parent f37ae687a2
commit 35ea3bb32d
1 file changed
+119 -1
+119 -1
View File
@@ -1,15 +1,81 @@
from typing import Optional
import discord
from red_commons.logging import getLogger
from redbot import version_info
from redbot.core import Config, checks, commands, i18n
from redbot.core.dev_commands import Dev
from redbot.core.dev_commands import Dev, DevOutput, cleanup_code
from redbot.core.utils.chat_formatting import box, pagify
from yarl import URL
log = getLogger("red.trusty-cogs.loaddev")
_ = i18n.Translator("LoadDev", __file__)
class EvalModal(discord.ui.Modal):
def __init__(self, ctx: commands.Context, debug: bool):
self.debug = debug
self.ctx = ctx
title = "Debug" if self.debug else "Eval"
super().__init__(title=title)
self.text = discord.ui.TextInput(style=discord.TextStyle.paragraph, label=title)
self.add_item(self.text)
async def on_submit(self, interaction: discord.Interaction):
dev_cog = interaction.client.get_cog("Dev")
env = dev_cog.get_environment(self.ctx)
env["interaction"] = interaction
source = cleanup_code(self.text.value)
if self.debug:
output = await DevOutput.from_debug(
self.ctx, source=source, source_cache=dev_cog.source_cache, env=env
)
else:
output = await DevOutput.from_eval(
self.ctx, source=source, source_cache=dev_cog.source_cache, env=env
)
if output.result is not None:
dev_cog._last_result = output.result
await output.send()
await interaction.response.defer()
class EvalButton(discord.ui.Button):
def __init__(self, ctx: commands.Context, debug: bool):
self.debug = debug
self.ctx = ctx
title = "Debug" if self.debug else "Eval"
super().__init__(style=discord.ButtonStyle.blurple, label=title)
async def callback(self, interaction: discord.Interaction):
modal = EvalModal(self.ctx, self.debug)
await interaction.response.send_modal(modal)
class EvalView(discord.ui.View):
def __init__(self, ctx: commands.Context):
super().__init__()
self.eval_button = EvalButton(ctx, debug=False)
self.debug_button = EvalButton(ctx, debug=True)
self.add_item(self.eval_button)
self.add_item(self.debug_button)
self.message: discord.Message = discord.utils.MISSING
async def on_timeout(self):
if self.message is not None:
await self.message.edit(view=None)
async def interaction_check(self, interaction: discord.Interaction):
if not await interaction.client.is_owner(interaction.user):
await interaction.response.send_message(
content=_("You are not authorized to interact with this."), ephemeral=True
)
return False
return True
class LoadDev(commands.Cog):
"""Allow live loading of Dev commands from Core"""
@@ -30,6 +96,10 @@ class LoadDev(commands.Cog):
if not self.bot._cli_flags.dev:
# only remove the dev cog if the dev cli flag is not set
await self.bot.remove_cog("Dev")
try:
self.bot.remove_dev_env_value("evalview")
except Exception:
log.exception("Error removing retrigger from dev environment.")
async def cog_load(self):
replace_mock = await self.config.replace_mock()
@@ -44,6 +114,10 @@ class LoadDev(commands.Cog):
log.debug("Replaced Mock command")
await self.bot.remove_cog("Dev")
await self.bot.add_cog(dev)
try:
self.bot.add_dev_env_value("evalview", lambda x: EvalView)
except Exception:
pass
def format_help_for_context(self, ctx: commands.Context) -> str:
"""
@@ -58,6 +132,50 @@ class LoadDev(commands.Cog):
"""
return
@commands.command(aliases=["ebutt"])
@commands.is_owner()
async def ebutton(self, ctx: commands.Context):
"""
Send a button that allows you to eval code written in a modal.
"""
if not self.bot.get_cog("Dev"):
command = self.load_dev.qualified_name
await ctx.send(
"You need Dev loaded for this command to work."
f"Use `{ctx.clean_prefix}{command}` to set this up without the `--dev` flag."
)
return
view = EvalView(ctx)
view.message = await ctx.send("Dev Tools", view=view)
@commands.command(name="url")
@commands.bot_has_permissions(embed_links=True)
async def url_command(self, ctx: commands.Context, *, url: str) -> None:
"""
Parse and make a URL human readable.
"""
url_obj = URL(url)
em = discord.Embed(description=box(url_obj.human_repr()))
if url_obj.scheme:
em.add_field(name="Scheme", value=box(url_obj.scheme))
if url_obj.user:
em.add_field(name="User", value=box(url_obj.user))
if url_obj.password:
em.add_field(name="password", value=box(url_obj.password))
if url_obj.host:
em.add_field(name="host", value=box(url_obj.host))
if url_obj.port:
em.add_field(name="port", value=box(str(url_obj.port)))
if url_obj.path:
em.add_field(name="path", value=box(url_obj.path))
if url_obj.query:
msg = "".join(box(f"{k}={v}") for k, v in url_obj.query.items())
for page in pagify(msg, delims="```\n", page_length=1024):
em.add_field(name="query", value=page)
if url_obj.fragment:
em.add_field(name="fragment", value=box(url_obj.fragment))
await ctx.send(embed=em)
@commands.group()
@checks.is_owner()
async def devset(self, ctx: commands.Context):