mirror of
https://github.com/RGBCube/VReplBot
synced 2026-01-14 17:21:02 +00:00
118 lines
3.6 KiB
Python
118 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from io import BytesIO
|
|
from typing import TYPE_CHECKING
|
|
|
|
from discord import File
|
|
from discord.ext.commands import Cog, command, param
|
|
from jishaku.codeblocks import Codeblock, codeblock_converter
|
|
|
|
if TYPE_CHECKING:
|
|
from discord import MessageReference, TextChannel
|
|
from discord.ext.commands import Context
|
|
|
|
from .. import ReplBot
|
|
|
|
|
|
async def get_message_content(channel: TextChannel, ref: MessageReference) -> str:
|
|
if ref.resolved:
|
|
return ref.resolved.content
|
|
else:
|
|
message = await channel.fetch_message(ref.message_id)
|
|
return message.content
|
|
|
|
|
|
class REPL(
|
|
Cog,
|
|
name = "REPL",
|
|
description = "REPL (Read, Eval, Print, Loop) commands.",
|
|
):
|
|
def __init__(self, bot: ReplBot) -> None:
|
|
self.bot = bot
|
|
|
|
@command(
|
|
aliases = ("run", "repl"),
|
|
brief = "Runs V code.",
|
|
help = "Runs V code."
|
|
)
|
|
async def eval(
|
|
self,
|
|
ctx: Context,
|
|
*,
|
|
code: Codeblock | None = param(converter = codeblock_converter, default = None)
|
|
) -> None:
|
|
if not code:
|
|
if not (reply := ctx.message.reference):
|
|
await ctx.reply("No code provided.")
|
|
return
|
|
|
|
content = await get_message_content(ctx.channel, reply)
|
|
code = codeblock_converter(content)
|
|
|
|
async with await self.bot.session.post(
|
|
"https://play.vlang.io/run",
|
|
data = { "code": code.content },
|
|
) as response:
|
|
text = await response.text()
|
|
text = text.replace("`", "\u200B`\u200B") # Zero-width space.
|
|
|
|
if len(text) + 6 > 2000:
|
|
await ctx.reply(
|
|
"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 link.",
|
|
help = "Shows the code in a V playground link."
|
|
)
|
|
async def show(self, ctx: Context, query: str | None = None) -> None:
|
|
if not query:
|
|
if not (reply := ctx.message.reference):
|
|
await ctx.reply("No query provided.")
|
|
return
|
|
|
|
content = await get_message_content(ctx.channel, reply)
|
|
|
|
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()
|
|
text = text.replace("`", "\u200B`\u200B") # Zero-width space.
|
|
|
|
if text == "Not found.":
|
|
await ctx.reply("Invalid link.")
|
|
return
|
|
|
|
if len(text) + 8 > 2000:
|
|
await ctx.reply(
|
|
"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:
|
|
await bot.add_cog(REPL(bot))
|