|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2015-2021 Rapptz |
| 5 | +Copyright (c) 2021-present Pycord Development |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +copy of this software and associated documentation files (the "Software"), |
| 9 | +to deal in the Software without restriction, including without limitation |
| 10 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | +and/or sell copies of the Software, and to permit persons to whom the |
| 12 | +Software is furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | +DEALINGS IN THE SOFTWARE. |
| 24 | +""" |
| 25 | +from abc import ABC, abstractmethod |
| 26 | +from typing import TYPE_CHECKING, Any, Optional, Union |
| 27 | + |
| 28 | +from discord.commands import ApplicationContext |
| 29 | +from discord.interactions import Interaction, InteractionMessage |
| 30 | +from discord.message import Message |
| 31 | +from discord.webhook import WebhookMessage |
| 32 | + |
| 33 | +from ..commands import Context |
| 34 | + |
| 35 | +__all__ = ("BridgeContext", "BridgeExtContext", "BridgeApplicationContext") |
| 36 | + |
| 37 | + |
| 38 | +class BridgeContext(ABC): |
| 39 | + """ |
| 40 | + The base context class for compatibility commands. This class is an :class:`ABC` (abstract base class), which is |
| 41 | + subclassed by :class:`BridgeExtContext` and :class:`BridgeApplicationContext`. The methods in this class are meant |
| 42 | + to give parity between the two contexts, while still allowing for all of their functionality. |
| 43 | +
|
| 44 | + When this is passed to a command, it will either be passed as :class:`BridgeExtContext`, or |
| 45 | + :class:`BridgeApplicationContext`. Since they are two separate classes, it is quite simple to use :meth:`isinstance` |
| 46 | + to make different functionality for each context. For example, if you want to respond to a command with the command |
| 47 | + type that it was invoked with, you can do the following: |
| 48 | +
|
| 49 | + .. code-block:: python3 |
| 50 | +
|
| 51 | + @bot.bridge_command() |
| 52 | + async def example(ctx: BridgeContext): |
| 53 | + if isinstance(ctx, BridgeExtContext): |
| 54 | + command_type = "Traditional (prefix-based) command" |
| 55 | + elif isinstance(ctx, BridgeApplicationContext): |
| 56 | + command_type = "Application command" |
| 57 | + await ctx.send(f"This command was invoked with a(n) {command_type}.") |
| 58 | +
|
| 59 | + .. versionadded:: 2.0 |
| 60 | + """ |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + async def _respond(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]: |
| 64 | + ... |
| 65 | + |
| 66 | + @abstractmethod |
| 67 | + async def _defer(self, *args, **kwargs) -> None: |
| 68 | + ... |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + async def _edit(self, *args, **kwargs) -> Union[InteractionMessage, Message]: |
| 72 | + ... |
| 73 | + |
| 74 | + async def respond(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]: |
| 75 | + """|coro| |
| 76 | +
|
| 77 | + Responds to the command with the respective response type to the current context. In :class:`BridgeExtContext`, |
| 78 | + this will be :meth:`~.ExtContext.reply` while in :class:`BridgeApplicationContext`, this will be |
| 79 | + :meth:`~.ApplicationContext.respond`. |
| 80 | + """ |
| 81 | + return await self._respond(*args, **kwargs) |
| 82 | + |
| 83 | + async def reply(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]: |
| 84 | + """|coro| |
| 85 | +
|
| 86 | + Alias for :meth:`~.BridgeContext.respond`. |
| 87 | + """ |
| 88 | + return await self.respond(*args, **kwargs) |
| 89 | + |
| 90 | + async def defer(self, *args, **kwargs) -> None: |
| 91 | + """|coro| |
| 92 | +
|
| 93 | + Defers the command with the respective approach to the current context. In :class:`BridgeExtContext`, this will |
| 94 | + be :meth:`~.ExtContext.trigger_typing` while in :class:`BridgeApplicationContext`, this will be |
| 95 | + :meth:`~.ApplicationContext.defer`. |
| 96 | +
|
| 97 | + .. note:: |
| 98 | + There is no ``trigger_typing`` alias for this method. ``trigger_typing`` will always provide the same |
| 99 | + functionality across contexts. |
| 100 | + """ |
| 101 | + return await self._defer(*args, **kwargs) |
| 102 | + |
| 103 | + async def edit(self, *args, **kwargs) -> Union[InteractionMessage, Message]: |
| 104 | + """|coro| |
| 105 | +
|
| 106 | + Edits the original response message with the respective approach to the current context. In |
| 107 | + :class:`BridgeExtContext`, this will have a custom approach where :meth:`.respond` caches the message to be |
| 108 | + edited here. In :class:`BridgeApplicationContext`, this will be :meth:`~.ApplicationContext.edit`. |
| 109 | + """ |
| 110 | + return await self._edit(*args, **kwargs) |
| 111 | + |
| 112 | + def _get_super(self, attr: str) -> Optional[Any]: |
| 113 | + return getattr(super(), attr) |
| 114 | + |
| 115 | + |
| 116 | +class BridgeApplicationContext(BridgeContext, ApplicationContext): |
| 117 | + """ |
| 118 | + The application context class for compatibility commands. This class is a subclass of :class:`BridgeContext` and |
| 119 | + :class:`ApplicationContext`. This class is meant to be used with :class:`BridgeCommand`. |
| 120 | +
|
| 121 | + .. versionadded:: 2.0 |
| 122 | + """ |
| 123 | + |
| 124 | + async def _respond(self, *args, **kwargs) -> Union[Interaction, WebhookMessage]: |
| 125 | + return await self._get_super("respond")(*args, **kwargs) |
| 126 | + |
| 127 | + async def _defer(self, *args, **kwargs) -> None: |
| 128 | + return await self._get_super("defer")(*args, **kwargs) |
| 129 | + |
| 130 | + async def _edit(self, *args, **kwargs) -> InteractionMessage: |
| 131 | + return await self._get_super("edit")(*args, **kwargs) |
| 132 | + |
| 133 | + |
| 134 | +class BridgeExtContext(BridgeContext, Context): |
| 135 | + """ |
| 136 | + The ext.commands context class for compatibility commands. This class is a subclass of :class:`BridgeContext` and |
| 137 | + :class:`Context`. This class is meant to be used with :class:`BridgeCommand`. |
| 138 | +
|
| 139 | + .. versionadded:: 2.0 |
| 140 | + """ |
| 141 | + |
| 142 | + def __init__(self, *args, **kwargs): |
| 143 | + super().__init__(*args, **kwargs) |
| 144 | + self._original_response_message: Optional[Message] = None |
| 145 | + |
| 146 | + async def _respond(self, *args, **kwargs) -> Message: |
| 147 | + message = await self._get_super("reply")(*args, **kwargs) |
| 148 | + if self._original_response_message == None: |
| 149 | + self._original_response_message = message |
| 150 | + return message |
| 151 | + |
| 152 | + async def _defer(self, *args, **kwargs) -> None: |
| 153 | + return await self._get_super("trigger_typing")(*args, **kwargs) |
| 154 | + |
| 155 | + async def _edit(self, *args, **kwargs) -> Message: |
| 156 | + return await self._original_response_message.edit(*args, **kwargs) |
| 157 | + |
| 158 | + |
| 159 | +if TYPE_CHECKING: |
| 160 | + # This is a workaround for mypy not being able to resolve the type of BridgeCommand. |
| 161 | + class BridgeContext(ApplicationContext, Context): |
| 162 | + ... |
0 commit comments