Skip to content

Commit

Permalink
feat: add helper function for markdown links
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobCoffee committed Nov 22, 2023
1 parent d2de3b7 commit 96bcf7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/byte/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from discord.ext.commands import Context
from discord.ext.commands._types import Check

__all__ = ("is_byte_dev",)
__all__ = ("is_byte_dev", "linker")


def is_byte_dev() -> Check[Any]:
Expand All @@ -38,3 +38,17 @@ async def predicate(ctx: Context) -> bool:
return any(role.name == "byte-dev" for role in ctx.author.roles)

return commands.check(predicate)


def linker(title: str, link: str, show_embed: bool = False) -> str:
"""Create a Markdown link, optionally with an embed.
Args:
title: The title of the link.
link: The URL of the link.
show_embed: Whether to show the embed or not.
Returns:
A Markdown link.
"""
return f"[{title}]({link})" if show_embed else f"[{title}](<{link}>)"
7 changes: 6 additions & 1 deletion src/byte/plugins/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from discord.ext.commands import Bot, Cog

from src.byte.lib.common import litestar_logo_yellow, mcve
from src.byte.lib.utils import linker
from src.byte.views.forums import HelpThreadView

__all__ = ("Events", "setup")
Expand Down Expand Up @@ -38,7 +39,11 @@ async def on_thread_create(self, thread: Thread) -> None:
f"`{command_prefix}solve`" for command_prefix in cast(list[str], self.bot.command_prefix)
)
embed.add_field(name="Closing", value=f"To close, type {commands_to_solve}.", inline=True)
embed.add_field(name="MCVE", value=f"Please include an [MCVE](<{mcve}>) if relevant.", inline=False)
embed.add_field(
name="MCVE",
value=f"Please include an {linker('MCVE', mcve)} so that we can reproduce your issue locally.",
inline=False,
)
embed.set_thumbnail(url=litestar_logo_yellow)
view = HelpThreadView(author=thread.owner, bot=self.bot)
await thread.send(embed=embed, view=view)
Expand Down
4 changes: 3 additions & 1 deletion src/byte/plugins/forums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

__all__ = ("ForumCommands", "setup")

from src.byte.lib.utils import linker


class ForumCommands(Cog):
"""Forum command cog."""
Expand Down Expand Up @@ -71,7 +73,7 @@ async def tree_sync(self, interaction: Interaction, user: Member) -> None:
embed.add_field(name="Hi", value=f"{user.mention}", inline=True)
embed.add_field(
name="MCVE",
value=f"Please include an [MCVE](<{mcve}>) so that we can reproduce your issue locally.",
value=f"Please include an {linker('MCVE', mcve)} so that we can reproduce your issue locally.",
inline=True,
)
embed.set_thumbnail(url=litestar_logo_yellow)
Expand Down

0 comments on commit 96bcf7e

Please sign in to comment.