Source code for discord_tools.app_commands.context_menu
"""
The MIT License (MIT)
Copyright (c) 2024-present Developer Anonymous
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import inspect
from typing import Generic, Union, TypeVar, Any, Callable, Optional
from discord import Interaction, Message, Member, User
from discord.utils import MISSING, find
from discord.ext.commands import Cog, GroupCog, Bot
from discord.app_commands import ContextMenu, locale_str, CommandTree
CogLike = Union[Cog, GroupCog]
CogT = TypeVar("CogT", bound=CogLike)
T = TypeVar("T")
InteractionT = TypeVar("InteractionT", bound=Interaction[Any])
ContextMenuCallback = Union[
Callable[[CogT, InteractionT, Message], Any],
Callable[[CogT, InteractionT, Member], Any],
Callable[[CogT, InteractionT, User], Any],
Callable[[CogT, InteractionT, Union[Member, User]], Any],
]
__all__ = ("CogContextMenuHolder",)
[docs]
class CogContextMenuHolder(Generic[CogT]):
"""Represents a :class:`discord.ext.commands.Cog` or :class:`discord.ext.commands.GroupCog` context menu holder.
.. versionadded:: 1.0
Parameters
----------
cog: Union[:class:`discord.ext.commands.Cog`, :class:`discord.ext.commands.GroupCog`]
The cog this context menu holder belongs to.
"""
def __init__(self, cog: CogT) -> None:
self.cog: CogT = cog
self._context_menus: list[ContextMenu] = []
@property
def context_menus(self) -> list[ContextMenu]:
"""List[:class:`discord.app_commands.ContextMenu`]: Returns all the context menus
that this holder has loaded.
"""
return self._context_menus.copy()
def _get_cog_client(self) -> Optional[Bot]:
if hasattr(self.cog, "bot"):
return self.cog.bot # type: ignore
if hasattr(self.cog, "client"):
return self.cog.client # type: ignore
return None
[docs]
def add_to_tree(self, *, tree: CommandTree = MISSING):
"""Adds the context menus to the command tree.
If no ``tree`` is passed then it tries to resolve to ``cog.bot`` or ``cog.client``, and after
that, ``bot.tree``.
Raises
------
RuntimeError
No client/bot found on the cog.
Parameters
----------
tree: :class:`discord.app_commands.CommandTree`
The tree to add the context menus to.
"""
if tree is MISSING:
client = self._get_cog_client()
if client is None:
raise RuntimeError(f"No client/bot found on the {self.cog!r} cog.")
tree = client.tree
for menu in self.context_menus:
tree.add_command(menu)
[docs]
def clear(self):
"""Clears all the current menus in this holder.
.. note::
This **does not** clear them from the tree.
"""
self._context_menus.clear()