1
Fork 0
mirror of https://github.com/RGBCube/VReplBot synced 2025-07-27 00:17:46 +00:00

Features and refactoringn

This commit is contained in:
RGBCube 2023-02-03 21:45:42 +03:00
parent 06f3829207
commit 7a9706ee0d

View file

@ -4,8 +4,8 @@ from io import BytesIO
from typing import Literal, TYPE_CHECKING from typing import Literal, TYPE_CHECKING
from discord import File from discord import File
from discord.ext.commands import Cog, command, param from discord.ext.commands import Cog, command
from jishaku.codeblocks import Codeblock, codeblock_converter from jishaku.codeblocks import codeblock_converter
if TYPE_CHECKING: if TYPE_CHECKING:
from discord import MessageReference, TextChannel from discord import MessageReference, TextChannel
@ -37,23 +37,32 @@ class Playground(
async def run_test_common( async def run_test_common(
self, self,
ctx: Context, ctx: Context,
code: Codeblock | None, code: str | None,
*, *,
type: Literal["run"] | Literal["run_test"] type: Literal["run"] | Literal["run_test"]
) -> None: ) -> None:
if (c_stripped := code.lstrip("https://")).startswith("play.vlang.io/?query="):
query = c_stripped.lstrip("play.vlang.io/?query=").split(" ", 1)[0]
code = await self.get_query_content(query)
if not code: if not code:
await ctx.reply("Invalid query.")
return
elif not code:
if not (reply := ctx.message.reference): if not (reply := ctx.message.reference):
await ctx.reply("No code provided.") await ctx.reply("No code provided.")
return return
content = await get_message_content(ctx.channel, reply) content = await get_message_content(ctx.channel, reply)
code = codeblock_converter(content) code = codeblock_converter(content).content
async with await self.bot.session.post( async with await self.bot.session.post(
f"https://play.vlang.io/{type}", f"https://play.vlang.io/{type}",
data = { "code": code.content }, data = { "code": code },
) as response: ) as response:
text = sanitize_str_for_codeblock(await response.text()) body = await response.json()
text = sanitize_str_for_codeblock(body["output"])
if len(text) + 7 > 2000: if len(text) + 7 > 2000:
await ctx.reply( await ctx.reply(
@ -75,9 +84,9 @@ class Playground(
self, self,
ctx: Context, ctx: Context,
*, *,
code: Codeblock | None = param(converter = codeblock_converter, default = None) query_or_code: str | None = None
) -> None: ) -> None:
await self.run_test_common(ctx, code, type = "run") await self.run_test_common(ctx, query_or_code, type = "run")
@command( @command(
brief = "Runs tests of V code.", brief = "Runs tests of V code.",
@ -87,9 +96,21 @@ class Playground(
self, self,
ctx: Context, ctx: Context,
*, *,
code: Codeblock | None = param(converter = codeblock_converter, default = None) query_or_code: str | None = None
) -> None: ) -> None:
await self.run_test_common(ctx, code, type = "run_test") await self.run_test_common(ctx, query_or_code, type = "run_test")
async def get_query_content(self, query: str) -> str | None:
async with await self.bot.session.post(
f"https://play.vlang.io/query",
data = { "hash": query }
) as response:
text = sanitize_str_for_codeblock(await response.text())
if text == "Not found.":
return None
return text
@command( @command(
aliases = ("download",), aliases = ("download",),
@ -115,25 +136,21 @@ class Playground(
await ctx.reply("No query provided.") await ctx.reply("No query provided.")
return return
async with await self.bot.session.post( code = await self.get_query_content(query)
f"https://play.vlang.io/query",
data = { "hash": query }
) as response:
text = sanitize_str_for_codeblock(await response.text())
if text == "Not found.": if not code:
await ctx.reply("Invalid link.") await ctx.reply("Invalid link.")
return return
if len(text) + 8 > 2000: if len(code) + 8 > 2000:
await ctx.reply( await ctx.reply(
"The code was too long to be sent as a message. Here is a file instead:", "The code was too long to be sent as a message. Here is a file instead:",
file = File(BytesIO(text.encode()), filename = "code.v") file = File(BytesIO(code.encode()), filename = "code.v")
) )
return return
await ctx.reply( await ctx.reply(
"```v\n" + text + "```" "```v\n" + code + "```"
) )