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

Add repl.py

This commit is contained in:
RGBCube 2023-01-30 20:02:48 +03:00
parent ec2bafb249
commit 9eb87f0c8d
8 changed files with 50 additions and 3 deletions

4
.gitignore vendored
View file

@ -1,8 +1,8 @@
*
!.gitignore
!bot/
!bot/cogs/
!v_repl_bot/
!v_repl_bot/cogs/
!poetry.lock
!*.py
!*.example.json

View file

@ -11,4 +11,4 @@ jishaku = "*"
uvloop = "*"
[tool.poetry.scripts]
bot = "bot.__main__:main"
bot = "v_repl_bot.__main__:main"

View file

@ -22,6 +22,7 @@ from discord.ext.commands import (
class ReplBot(CommandsBot):
ready_timestamp: float
log_webhook: Webhook
session: AIOHTTPSession
def __init__(self, *, token: str, webhook_url: str) -> None:
self.token = token

46
v_repl_bot/cogs/repl.py Normal file
View file

@ -0,0 +1,46 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from discord.ext import commands
from discord.ext.commands import Cog, command
from jishaku.codeblocks import Codeblock, codeblock_converter
if TYPE_CHECKING:
from discord.ext.commands import Context
from .. import ReplBot
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 = commands.param(converter = codeblock_converter, default = None)
) -> None:
if code is None:
await ctx.reply("No code provided.")
with self.bot.session.post(
"https://vlang.io/play",
data = { "code": code.content },
) as response:
await ctx.reply(
"```\n" + response.text.replace("`", "\u200B`\u200B") + "\n```"
) # Zero-width space.
async def setup(bot: ReplBot) -> None:
await bot.add_cog(REPL(bot))