From 9a416f50001d05260027903daba677c8c7a11d72 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Tue, 31 Jan 2023 19:31:08 +0300 Subject: [PATCH] Add &show --- v_repl_bot/cogs/repl.py | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/v_repl_bot/cogs/repl.py b/v_repl_bot/cogs/repl.py index 64f2e24..b9d71b1 100644 --- a/v_repl_bot/cogs/repl.py +++ b/v_repl_bot/cogs/repl.py @@ -48,10 +48,57 @@ class REPL( "The output was too long to be sent as a message. Here is a file instead:", file = File(BytesIO(text.encode()), filename = "output.txt") ) + return + + await ctx.reply( + "```" + text + "```" + ) + + @command( + aliases = ("download",), + brief = "Shows the code in a V playground query.", + help = "Shows the code in a V playground query." + ) + async def show(self, ctx: Context, query: str | None = None) -> None: + if query is None: + if not (reply := ctx.message.reference): + await ctx.reply("No query provided.") + return + + replied_message = await ctx.channel.fetch_message(reply.message_id) + content = replied_message.content + + if "play.vlang.io/?query=" in content: + query = content.split("play.vlang.io/?query=", 1)[1].split(" ", 1)[0] else: + query = content.split(" ", 1)[0] + + query = query.lstrip("https://").lstrip("play.vlang.io/?query=") + + if not query: + await ctx.reply("No query provided.") + return + + async with await self.bot.session.post( + f"https://play.vlang.io/query", + data = { "hash": query } + ) as response: + text = await response.text() + + if text == "Not found.": + await ctx.reply("Invalid link.") + return + + if len(text) + 8 > 2000: await ctx.reply( - "```" + text + "```" + "The code was too long to be sent as a message. Here is a file instead:", + file = File(BytesIO(text.encode()), filename = "code.v") ) + return + + await ctx.reply( + "```v\n" + text + "```" + ) async def setup(bot: ReplBot) -> None: