Skip to content

Commit

Permalink
Start work on support of advanced interaction options
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluenix2 committed Oct 6, 2021
1 parent ee27b99 commit 910cbae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
8 changes: 4 additions & 4 deletions wumpy/interactions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ApplicationCommandOption(enum.Enum):
class ResolvedInteractionData:
"""Asynchronously resolved data from Discord."""

users: Dict[int, Dict[str, Any]]
users: Dict[int, InteractionUser]
members: Dict[int, Dict[str, Any]]

roles: Dict[int, Dict[str, Any]]
Expand All @@ -54,8 +54,8 @@ class ResolvedInteractionData:

__slots__ = ('users', 'members', 'roles', 'channels', 'messages')

def __init__(self, data: Dict[str, Any]) -> None:
self.users = {int(k): v for k, v in data.get('users', {}).items()}
def __init__(self, rest: InteractionRequester, data: Dict[str, Any]) -> None:
self.users = {int(k): InteractionUser(rest, v) for k, v in data.get('users', {}).items()}
self.members = {int(k): v for k, v in data.get('members', {}).items()}

self.roles = {int(k): v for k, v in data.get('roles', {}).items()}
Expand Down Expand Up @@ -208,7 +208,7 @@ def __init__(
self.invoked = data['data']['id']
self.invoked_type = ApplicationCommandOption(data['data']['type'])

self.resolved = ResolvedInteractionData(data['data'].get('resolved', {}))
self.resolved = ResolvedInteractionData(rest, data['data'].get('resolved', {}))

target_id = data['data'].get('target_id')
self.target_id = int(target_id) if target_id else None
Expand Down
21 changes: 21 additions & 0 deletions wumpy/interactions/commands/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing_extensions import Annotated

from ...errors import CommandSetupError
from ...models import InteractionUser
from ...utils import MISSING
from ..base import (
ApplicationCommandOption, CommandInteraction, CommandInteractionOption
Expand Down Expand Up @@ -72,6 +73,7 @@ class unless it is for the purpose of extending it.
int: ApplicationCommandOption.integer,
bool: ApplicationCommandOption.boolean,
float: ApplicationCommandOption.number,
InteractionUser: ApplicationCommandOption.user,
}

def __init__(
Expand Down Expand Up @@ -245,12 +247,31 @@ def resolve(
)

value = data.value

if value is None:
raise CommandSetupError(
f"Expected command option value for '{self.param}' of '{interaction.name}'"
)

if self.converter is not MISSING:
try:
value = self.converter(value)
except Exception as exc:
raise CommandSetupError('Could not convert argument:', value) from exc

# Some options only pass IDs because Discord asynchronously resolves
# the data for them, these are then passed in a special `resolved`
# field that we need to look them up by.
if self.type is ApplicationCommandOption.user:
value = interaction.resolved.users.get(int(value))

# At this point `value` may be None from our lookups of the resolved
# data
if value is None:
raise CommandSetupError(
"Didn't receive resolved data for command '{interaction.name}'"
)

return value

def to_dict(self) -> Dict[str, Any]:
Expand Down
3 changes: 1 addition & 2 deletions wumpy/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def _update(self, data: Dict) -> None:
avatar = data['avatar']
self.avatar = Asset(self._rest, f'avatars/{self.id}/{avatar}') if avatar else self.avatar

flags = data.get('public_flags')
self.public_flags = UserFlags(flags) if flags else self.public_flags
self.public_flags = UserFlags(data['public_flags'])

self.bot = data.get('bot', self.bot)
self.system = data.get('system', self.system)
Expand Down

0 comments on commit 910cbae

Please sign in to comment.