Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions discord/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
)

from ..enums import ChannelType, SlashCommandOptionType
from ..errors import ClientException, ValidationError
from ..errors import ClientException, ValidationError, NotFound
from ..member import Member
from ..message import Attachment, Message
from ..user import User
Expand Down Expand Up @@ -744,9 +744,10 @@ async def _invoke(self, ctx: ApplicationContext) -> None:

elif op.input_type == SlashCommandOptionType.mentionable:
arg_id = int(arg)
arg = await get_or_fetch(ctx.guild, "member", arg_id)
if arg is None:
arg = ctx.guild.get_role(arg_id) or arg_id
try:
arg = await get_or_fetch(ctx.guild, "member", arg_id)
except NotFound:
arg = await get_or_fetch(ctx.guild, "role", arg_id)

elif op.input_type == SlashCommandOptionType.string and (converter := op.converter) is not None:
arg = await converter.convert(converter, ctx, arg)
Expand Down
32 changes: 32 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,38 @@ async def fetch_roles(self) -> List[Role]:
data = await self._state.http.get_roles(self.id)
return [Role(guild=self, state=self._state, data=d) for d in data]

async def _fetch_role(self, role_id: int) -> Role:
"""|coro|

Retrieves a :class:`Role` that the guild has.

.. note::

This method is an API call. For general usage, consider using :attr:`get_role` instead.

.. versionadded:: 2.0

Parameters
-----------
role_id: :class:`int`
The role ID to fetch from the guild.

Raises
-------
HTTPException
Retrieving the role failed.

Returns
-------
Optional[:class:`Role`]
The role in the guild with the specified ID.
Returns ``None`` if not found.
"""
roles = await self.fetch_roles()
for role in roles:
if role.id == role_id:
return role

@overload
async def create_role(
self,
Expand Down
4 changes: 3 additions & 1 deletion discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def time_snowflake(dt: datetime.datetime, high: bool = False) -> int:
The snowflake representing the time given.
"""
discord_millis = int(dt.timestamp() * 1000 - DISCORD_EPOCH)
return (discord_millis << 22) + (2**22 - 1 if high else 0)
return (discord_millis << 22) + (2 ** 22 - 1 if high else 0)


def find(predicate: Callable[[T], Any], seq: Iterable[T]) -> Optional[T]:
Expand Down Expand Up @@ -477,6 +477,8 @@ async def get_or_fetch(obj, attr: str, id: int, *, default: Any = MISSING):
if getter is None:
try:
getter = await getattr(obj, f"fetch_{attr}")(id)
except AttributeError:
getter = await getattr(obj, f"_fetch_{attr}")(id)
except HTTPException:
if default is not MISSING:
return default
Expand Down