|
| 1 | +import logging |
| 2 | +import re |
| 3 | +from dataclasses import dataclass |
| 4 | +from types import MethodType |
| 5 | +from typing import Any, Callable |
| 6 | + |
| 7 | +from discord import Embed, Interaction, Message, app_commands |
| 8 | +from discord.abc import MISSING |
| 9 | +from discord.app_commands import ContextMenu |
| 10 | +from discord.utils import Coro |
| 11 | + |
| 12 | +from ghutils.core.cog import GHUtilsCog |
| 13 | +from ghutils.utils.discord.embeds import create_issue_embed |
| 14 | +from ghutils.utils.discord.references import IssueReferenceTransformer |
| 15 | +from ghutils.utils.discord.visibility import respond_with_visibility |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +type ContextMenuCallback[GroupT: GHUtilsCog] = Callable[ |
| 20 | + [GroupT, Interaction, Message], Coro[Any] |
| 21 | +] |
| 22 | + |
| 23 | +type ContextMenuBuilder[GroupT: GHUtilsCog] = Callable[[GroupT], ContextMenu] |
| 24 | + |
| 25 | + |
| 26 | +_builders = list[ContextMenuBuilder[Any]]() |
| 27 | + |
| 28 | + |
| 29 | +def context_menu[GroupT: GHUtilsCog]( |
| 30 | + *, |
| 31 | + name: str | app_commands.locale_str, |
| 32 | + nsfw: bool = False, |
| 33 | + auto_locale_strings: bool = True, |
| 34 | + extras: dict[Any, Any] = MISSING, |
| 35 | +) -> Callable[[ContextMenuCallback[GroupT]], ContextMenuCallback[GroupT]]: |
| 36 | + def decorator(f: ContextMenuCallback[GroupT]): |
| 37 | + def builder(group: GroupT): |
| 38 | + return ContextMenu( |
| 39 | + name=name, |
| 40 | + callback=MethodType(f, group), |
| 41 | + nsfw=nsfw, |
| 42 | + auto_locale_strings=auto_locale_strings, |
| 43 | + extras=extras, |
| 44 | + ) |
| 45 | + |
| 46 | + _builders.append(builder) |
| 47 | + return f |
| 48 | + |
| 49 | + return decorator |
| 50 | + |
| 51 | + |
| 52 | +_issue_pattern = re.compile( |
| 53 | + r""" |
| 54 | + (?<![a-zA-Z`</]) |
| 55 | + (?P<value> |
| 56 | + (?P<repo>[\w-]+/[\w-]+)? |
| 57 | + \# |
| 58 | + (?P<reference>[0-9]+) |
| 59 | + ) |
| 60 | + (?![a-zA-Z`>]) |
| 61 | + """, |
| 62 | + flags=re.VERBOSE, |
| 63 | +) |
| 64 | + |
| 65 | + |
| 66 | +@dataclass(eq=False) |
| 67 | +class ContextMenusCog(GHUtilsCog): |
| 68 | + """Context menu commands.""" |
| 69 | + |
| 70 | + def __post_init__(self): |
| 71 | + self._ctx_menus = list[ContextMenu]() |
| 72 | + |
| 73 | + async def cog_load(self): |
| 74 | + await super().cog_load() |
| 75 | + |
| 76 | + for builder in _builders: |
| 77 | + ctx_menu = builder(self) |
| 78 | + self._ctx_menus.append(ctx_menu) |
| 79 | + self.bot.tree.add_command(ctx_menu) |
| 80 | + |
| 81 | + async def cog_unload(self) -> None: |
| 82 | + await super().cog_unload() |
| 83 | + |
| 84 | + for ctx_menu in self._ctx_menus: |
| 85 | + self.bot.tree.remove_command(ctx_menu.name, type=ctx_menu.type) |
| 86 | + self._ctx_menus.clear() |
| 87 | + |
| 88 | + @context_menu(name="Show GitHub issues") |
| 89 | + async def show_issues(self, interaction: Interaction, message: Message): |
| 90 | + await interaction.response.defer() |
| 91 | + |
| 92 | + seen = set[str]() |
| 93 | + embeds = list[Embed]() |
| 94 | + transformer = IssueReferenceTransformer() |
| 95 | + |
| 96 | + for match in _issue_pattern.finditer(message.content): |
| 97 | + value = match.group("value") |
| 98 | + try: |
| 99 | + repo, issue = await transformer.transform(interaction, value) |
| 100 | + except Exception: |
| 101 | + logger.warning( |
| 102 | + f"Failed to transform issue reference: {value}", exc_info=True |
| 103 | + ) |
| 104 | + continue |
| 105 | + |
| 106 | + if issue.html_url in seen: |
| 107 | + continue |
| 108 | + seen.add(issue.html_url) |
| 109 | + |
| 110 | + embeds.append(create_issue_embed(repo, issue, add_body=False)) |
| 111 | + if len(embeds) >= 10: |
| 112 | + break |
| 113 | + |
| 114 | + if not embeds: |
| 115 | + await respond_with_visibility( |
| 116 | + interaction, |
| 117 | + "public", |
| 118 | + content="No issue references found.", |
| 119 | + ) |
| 120 | + return |
| 121 | + |
| 122 | + await respond_with_visibility( |
| 123 | + interaction, |
| 124 | + "public", |
| 125 | + embeds=embeds, |
| 126 | + ) |
0 commit comments