|
8 | 8 | import pathlib |
9 | 9 | import struct |
10 | 10 | import sys |
| 11 | +from collections.abc import Callable |
11 | 12 | from datetime import UTC, datetime |
12 | | -from typing import TYPE_CHECKING, Any, Self |
| 13 | +from functools import wraps |
| 14 | +from typing import TYPE_CHECKING, Any, Self, TypeVar, cast |
13 | 15 | from urllib.parse import quote |
14 | 16 |
|
15 | 17 |
|
|
36 | 38 | "parse_timestamp", |
37 | 39 | "url_encode_datetime", |
38 | 40 | "MISSING", |
| 41 | + "handle_user_ids", |
39 | 42 | ) |
40 | 43 |
|
41 | 44 |
|
@@ -726,3 +729,23 @@ async def wait(self) -> Any: |
726 | 729 |
|
727 | 730 | async def predicate(self, *args: Any) -> bool: |
728 | 731 | return True |
| 732 | + |
| 733 | + |
| 734 | +F = TypeVar("F", bound=Callable[..., Any]) |
| 735 | + |
| 736 | + |
| 737 | +def handle_user_ids(is_self: bool = False) -> Callable[..., Any]: |
| 738 | + def decorator(func: F) -> F: |
| 739 | + @wraps(func) |
| 740 | + def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 741 | + from .user import PartialUser |
| 742 | + |
| 743 | + new_args = [(arg.id if isinstance(arg, PartialUser) else arg) for arg in (args[1:] if is_self else args)] |
| 744 | + if is_self: |
| 745 | + new_args = [args[0], *new_args] |
| 746 | + new_kwargs = {k: (v.id if isinstance(v, PartialUser) else v) for k, v in kwargs.items()} |
| 747 | + return func(*new_args, **new_kwargs) |
| 748 | + |
| 749 | + return cast(F, wrapper) |
| 750 | + |
| 751 | + return decorator |
0 commit comments