From 893942fcd7a7e56b71d6b55acda31d9dcb5d07fa Mon Sep 17 00:00:00 2001 From: Joinemm Date: Tue, 21 Nov 2023 18:02:45 +0200 Subject: [PATCH] Add mermaid graph generator using kroki api --- cogs/utility.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cogs/utility.py b/cogs/utility.py index 551d806..a841f1a 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -3,6 +3,7 @@ # https://git.joinemm.dev/miso-bot import html +import io import json import random from time import time @@ -1124,6 +1125,33 @@ async def market(self, ctx: commands.Context, *, search_term: str): await MarketPaginator(data["results"]).run(ctx) + @commands.command() + async def graph(self, ctx: commands.Context, *, code: str): + """Generate a graph from code using Mermaid language + + Syntax reference: https://mermaid.js.org/intro/syntax-reference.html + """ + async with self.bot.session.post( + "https://kroki.io/", + json={ + "diagram_source": code.strip("`"), + "diagram_type": "mermaid", + "output_format": "png", + "diagram_options": {"theme": "dark"}, + }, + ) as response: + if not response.ok: + error = await response.text() + raise exceptions.CommandError(error.split(" at", 1)[0]) + + buffer = io.BytesIO(await response.read()) + await ctx.send( + file=discord.File( + fp=buffer, + filename="miso_bot_mermaid_graph.png", + ), + ) + async def setup(bot): await bot.add_cog(Utility(bot))