From 9eb87f0c8dd87b17f113111d6a66975247a0f54d Mon Sep 17 00:00:00 2001 From: RGBCube Date: Mon, 30 Jan 2023 20:02:48 +0300 Subject: [PATCH] Add repl.py --- .gitignore | 4 +- pyproject.toml | 2 +- {bot => v_repl_bot}/__init__.py | 1 + {bot => v_repl_bot}/__main__.py | 0 {bot => v_repl_bot}/cogs/error_handler.py | 0 {bot => v_repl_bot}/cogs/miscellanious.py | 0 v_repl_bot/cogs/repl.py | 46 +++++++++++++++++++++++ {bot => v_repl_bot}/config.example.json | 0 8 files changed, 50 insertions(+), 3 deletions(-) rename {bot => v_repl_bot}/__init__.py (98%) rename {bot => v_repl_bot}/__main__.py (100%) rename {bot => v_repl_bot}/cogs/error_handler.py (100%) rename {bot => v_repl_bot}/cogs/miscellanious.py (100%) create mode 100644 v_repl_bot/cogs/repl.py rename {bot => v_repl_bot}/config.example.json (100%) diff --git a/.gitignore b/.gitignore index c68636a..0c26855 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ * !.gitignore -!bot/ -!bot/cogs/ +!v_repl_bot/ +!v_repl_bot/cogs/ !poetry.lock !*.py !*.example.json diff --git a/pyproject.toml b/pyproject.toml index edcd92b..b5141ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,4 +11,4 @@ jishaku = "*" uvloop = "*" [tool.poetry.scripts] -bot = "bot.__main__:main" +bot = "v_repl_bot.__main__:main" diff --git a/bot/__init__.py b/v_repl_bot/__init__.py similarity index 98% rename from bot/__init__.py rename to v_repl_bot/__init__.py index 122067f..45cb011 100644 --- a/bot/__init__.py +++ b/v_repl_bot/__init__.py @@ -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 diff --git a/bot/__main__.py b/v_repl_bot/__main__.py similarity index 100% rename from bot/__main__.py rename to v_repl_bot/__main__.py diff --git a/bot/cogs/error_handler.py b/v_repl_bot/cogs/error_handler.py similarity index 100% rename from bot/cogs/error_handler.py rename to v_repl_bot/cogs/error_handler.py diff --git a/bot/cogs/miscellanious.py b/v_repl_bot/cogs/miscellanious.py similarity index 100% rename from bot/cogs/miscellanious.py rename to v_repl_bot/cogs/miscellanious.py diff --git a/v_repl_bot/cogs/repl.py b/v_repl_bot/cogs/repl.py new file mode 100644 index 0000000..ac8b0df --- /dev/null +++ b/v_repl_bot/cogs/repl.py @@ -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)) diff --git a/bot/config.example.json b/v_repl_bot/config.example.json similarity index 100% rename from bot/config.example.json rename to v_repl_bot/config.example.json