1
Fork 0
mirror of https://github.com/RGBCube/VReplBot synced 2025-07-27 00:17:46 +00:00
This commit is contained in:
RGBCube 2023-01-31 19:43:56 +03:00
parent 9a416f5000
commit 75b6c2e8c1

View file

@ -8,11 +8,20 @@ from discord.ext.commands import Cog, command, param
from jishaku.codeblocks import Codeblock, codeblock_converter from jishaku.codeblocks import Codeblock, codeblock_converter
if TYPE_CHECKING: if TYPE_CHECKING:
from discord import MessageReference, TextChannel
from discord.ext.commands import Context from discord.ext.commands import Context
from .. import ReplBot 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( class REPL(
Cog, Cog,
name = "REPL", name = "REPL",
@ -32,10 +41,14 @@ class REPL(
*, *,
code: Codeblock | None = param(converter = codeblock_converter, default = None) code: Codeblock | None = param(converter = codeblock_converter, default = None)
) -> None: ) -> None:
if code is None: if not code:
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)
code = codeblock_converter(content)
async with await self.bot.session.post( async with await self.bot.session.post(
"https://play.vlang.io/run", "https://play.vlang.io/run",
data = { "code": code.content }, data = { "code": code.content },
@ -60,13 +73,12 @@ class REPL(
help = "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: async def show(self, ctx: Context, query: str | None = None) -> None:
if query is None: if not query:
if not (reply := ctx.message.reference): if not (reply := ctx.message.reference):
await ctx.reply("No query provided.") await ctx.reply("No query provided.")
return return
replied_message = await ctx.channel.fetch_message(reply.message_id) content = await get_message_content(ctx.channel, reply)
content = replied_message.content
if "play.vlang.io/?query=" in content: if "play.vlang.io/?query=" in content:
query = content.split("play.vlang.io/?query=", 1)[1].split(" ", 1)[0] query = content.split("play.vlang.io/?query=", 1)[1].split(" ", 1)[0]
@ -84,6 +96,7 @@ class REPL(
data = { "hash": query } data = { "hash": query }
) as response: ) as response:
text = await response.text() text = 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.")