mirror of
https://github.com/RGBCube/VReplBot
synced 2025-07-27 08:27:45 +00:00
Add &test
This commit is contained in:
parent
2bcf24e0fd
commit
fdeb115212
1 changed files with 36 additions and 13 deletions
|
@ -1,7 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import 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, param
|
||||||
|
@ -14,6 +14,10 @@ if TYPE_CHECKING:
|
||||||
from .. import ReplBot
|
from .. import ReplBot
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_str_for_codeblock(string: str) -> str:
|
||||||
|
return string.replace("`", "\u200B`\u200B") # Zero-width space.
|
||||||
|
|
||||||
|
|
||||||
async def get_message_content(channel: TextChannel, ref: MessageReference) -> str:
|
async def get_message_content(channel: TextChannel, ref: MessageReference) -> str:
|
||||||
if ref.resolved:
|
if ref.resolved:
|
||||||
return ref.resolved.content
|
return ref.resolved.content
|
||||||
|
@ -30,16 +34,12 @@ class REPL(
|
||||||
def __init__(self, bot: ReplBot) -> None:
|
def __init__(self, bot: ReplBot) -> None:
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
@command(
|
async def run_test_common(
|
||||||
aliases = ("run", "repl"),
|
|
||||||
brief = "Runs V code.",
|
|
||||||
help = "Runs V code."
|
|
||||||
)
|
|
||||||
async def eval(
|
|
||||||
self,
|
self,
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
|
code: Codeblock | None,
|
||||||
*,
|
*,
|
||||||
code: Codeblock | None = param(converter = codeblock_converter, default = None)
|
type: Literal["run"] | Literal["run_test"]
|
||||||
) -> None:
|
) -> None:
|
||||||
if not code:
|
if not code:
|
||||||
if not (reply := ctx.message.reference):
|
if not (reply := ctx.message.reference):
|
||||||
|
@ -50,11 +50,10 @@ class REPL(
|
||||||
code = codeblock_converter(content)
|
code = codeblock_converter(content)
|
||||||
|
|
||||||
async with await self.bot.session.post(
|
async with await self.bot.session.post(
|
||||||
"https://play.vlang.io/run",
|
f"https://play.vlang.io/{type}",
|
||||||
data = { "code": code.content },
|
data = { "code": code.content },
|
||||||
) as response:
|
) as response:
|
||||||
text = await response.text()
|
text = sanitize_str_for_codeblock(await response.text())
|
||||||
text = text.replace("`", "\u200B`\u200B") # Zero-width space.
|
|
||||||
|
|
||||||
if len(text) + 6 > 2000:
|
if len(text) + 6 > 2000:
|
||||||
await ctx.reply(
|
await ctx.reply(
|
||||||
|
@ -67,6 +66,31 @@ class REPL(
|
||||||
"```" + text + "```"
|
"```" + text + "```"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@command(
|
||||||
|
aliases = ("eval", "repl"),
|
||||||
|
brief = "Runs V code.",
|
||||||
|
help = "Runs V code."
|
||||||
|
)
|
||||||
|
async def run(
|
||||||
|
self,
|
||||||
|
ctx: Context,
|
||||||
|
*,
|
||||||
|
code: Codeblock | None = param(converter = codeblock_converter, default = None)
|
||||||
|
) -> None:
|
||||||
|
await self.run_test_common(ctx, code, type = "run")
|
||||||
|
|
||||||
|
@command(
|
||||||
|
brief = "Runs tests of V code.",
|
||||||
|
help = "Runs tests of V code."
|
||||||
|
)
|
||||||
|
async def test(
|
||||||
|
self,
|
||||||
|
ctx: Context,
|
||||||
|
*,
|
||||||
|
code: Codeblock | None = param(converter = codeblock_converter, default = None)
|
||||||
|
) -> None:
|
||||||
|
await self.run_test_common(ctx, code, type = "run_test")
|
||||||
|
|
||||||
@command(
|
@command(
|
||||||
aliases = ("download",),
|
aliases = ("download",),
|
||||||
brief = "Shows the code in a V playground link.",
|
brief = "Shows the code in a V playground link.",
|
||||||
|
@ -95,8 +119,7 @@ class REPL(
|
||||||
f"https://play.vlang.io/query",
|
f"https://play.vlang.io/query",
|
||||||
data = { "hash": query }
|
data = { "hash": query }
|
||||||
) as response:
|
) as response:
|
||||||
text = await response.text()
|
text = sanitize_str_for_codeblock(await response.text())
|
||||||
text = text.replace("`", "\u200B`\u200B") # Zero-width space.
|
|
||||||
|
|
||||||
if text == "Not found.":
|
if text == "Not found.":
|
||||||
await ctx.reply("Invalid link.")
|
await ctx.reply("Invalid link.")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue