Skip to content

Commit

Permalink
Add mermaid graph generator using kroki api
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Nov 21, 2023
1 parent d45de9b commit 893942f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://git.joinemm.dev/miso-bot

import html
import io
import json
import random
from time import time
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 893942f

Please sign in to comment.