Skip to content

Commit

Permalink
[ext.bridge] Reformat
Browse files Browse the repository at this point in the history
Fixes type issues and reformats files
  • Loading branch information
Dorukyum committed Jun 28, 2022
1 parent c8feeaf commit 08aa4a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
4 changes: 3 additions & 1 deletion discord/ext/bridge/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@


class BotBase(ABC):
async def get_application_context(self, interaction: Interaction, cls=None) -> BridgeApplicationContext:
async def get_application_context(
self, interaction: Interaction, cls=None
) -> BridgeApplicationContext:
cls = cls if cls is not None else BridgeApplicationContext
# Ignore the type hinting error here. BridgeApplicationContext is a subclass of ApplicationContext, and since
# we gave it cls, it will be used instead.
Expand Down
29 changes: 16 additions & 13 deletions discord/ext/bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ async def example(ctx: BridgeContext):
"""

@abstractmethod
async def _respond(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]:
async def _respond(
self, *args, **kwargs
) -> Union[Union[Interaction, WebhookMessage], Message]:
...

@abstractmethod
Expand All @@ -71,7 +73,9 @@ async def _defer(self, *args, **kwargs) -> None:
async def _edit(self, *args, **kwargs) -> Union[InteractionMessage, Message]:
...

async def respond(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]:
async def respond(
self, *args, **kwargs
) -> Union[Union[Interaction, WebhookMessage], Message]:
"""|coro|
Responds to the command with the respective response type to the current context. In :class:`BridgeExtContext`,
Expand All @@ -80,7 +84,9 @@ async def respond(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMess
"""
return await self._respond(*args, **kwargs)

async def reply(self, *args, **kwargs) -> Union[Union[Interaction, WebhookMessage], Message]:
async def reply(
self, *args, **kwargs
) -> Union[Union[Interaction, WebhookMessage], Message]:
"""|coro|
Alias for :meth:`~.BridgeContext.respond`.
Expand Down Expand Up @@ -109,7 +115,7 @@ async def edit(self, *args, **kwargs) -> Union[InteractionMessage, Message]:
"""
return await self._edit(*args, **kwargs)

def _get_super(self, attr: str) -> Optional[Any]:
def _get_super(self, attr: str) -> Any:
return getattr(super(), attr)


Expand Down Expand Up @@ -154,10 +160,13 @@ async def _defer(self, *args, **kwargs) -> None:
kwargs.pop("ephemeral", None)
return await self._get_super("trigger_typing")(*args, **kwargs)

async def _edit(self, *args, **kwargs) -> Message:
return await self._original_response_message.edit(*args, **kwargs)
async def _edit(self, *args, **kwargs) -> Optional[Message]:
if self._original_response_message:
return await self._original_response_message.edit(*args, **kwargs)

async def delete(self, *, delay: Optional[float] = None, reason: Optional[str] = None) -> None:
async def delete(
self, *, delay: Optional[float] = None, reason: Optional[str] = None
) -> None:
"""|coro|
Deletes the original response message, if it exists.
Expand All @@ -171,9 +180,3 @@ async def delete(self, *, delay: Optional[float] = None, reason: Optional[str] =
"""
if self._original_response_message:
await self._original_response_message.delete(delay=delay, reason=reason)


if TYPE_CHECKING:
# This is a workaround for mypy not being able to resolve the type of BridgeCommand.
class BridgeContext(ApplicationContext, Context):
...
45 changes: 26 additions & 19 deletions discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from typing import Any, Union
from typing import Any, List, Union

import discord.commands.options
from discord.commands import Option, SlashCommand
Expand Down Expand Up @@ -138,35 +138,42 @@ async def convert(self, ctx, argument):


def attachment_callback(*args): # pylint: disable=unused-argument
raise ValueError("Attachments are not supported for compatibility commands.")
raise ValueError("Attachments are not supported for bridge commands.")


BRIDGE_CONVERTER_MAPPING = {
SlashCommandOptionType.string: str,
SlashCommandOptionType.integer: int,
SlashCommandOptionType.boolean: lambda val: _convert_to_bool(str(val)),
SlashCommandOptionType.user: UserConverter,
SlashCommandOptionType.channel: GuildChannelConverter,
SlashCommandOptionType.role: RoleConverter,
SlashCommandOptionType.mentionable: MentionableConverter,
SlashCommandOptionType.number: float,
SlashCommandOptionType.attachment: attachment_callback,
}


class BridgeOption(Option, Converter):
async def convert(self, ctx, argument) -> Any:
async def convert(self, ctx, argument: str) -> Any:
try:
if self.converter is not None:
converted = await self.converter.convert(ctx, argument)
else:
mapping = {
SlashCommandOptionType.string: str,
SlashCommandOptionType.integer: int,
SlashCommandOptionType.boolean: lambda val: _convert_to_bool(str(val)),
SlashCommandOptionType.user: UserConverter,
SlashCommandOptionType.channel: GuildChannelConverter,
SlashCommandOptionType.role: RoleConverter,
SlashCommandOptionType.mentionable: MentionableConverter,
SlashCommandOptionType.number: float,
SlashCommandOptionType.attachment: attachment_callback,
}
converter = mapping[self.input_type]
converter = BRIDGE_CONVERTER_MAPPING[self.input_type]
if issubclass(converter, Converter):
converted = await converter().convert(ctx, argument)
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
else:
converted = converter(argument)

if self.choices:
choices_names = [choice.name for choice in self.choices]
if converted in choices_names:
converted = get(self.choices, name=converted).value
choices_names: List[Union[str, int, float]] = [
choice.name for choice in self.choices
]
if converted in choices_names and (
choice := get(self.choices, name=converted)
):
converted = choice.value
else:
choices = [choice.value for choice in self.choices]
if converted not in choices:
Expand Down

0 comments on commit 08aa4a2

Please sign in to comment.