Skip to content

Commit 2a3097e

Browse files
committed
Add handle_user_ids decorator
1 parent c07f85b commit 2a3097e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

twitchio/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import pathlib
99
import struct
1010
import sys
11+
from collections.abc import Callable
1112
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
1315
from urllib.parse import quote
1416

1517

@@ -36,6 +38,7 @@
3638
"parse_timestamp",
3739
"url_encode_datetime",
3840
"MISSING",
41+
"handle_user_ids",
3942
)
4043

4144

@@ -726,3 +729,23 @@ async def wait(self) -> Any:
726729

727730
async def predicate(self, *args: Any) -> bool:
728731
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

Comments
 (0)