Skip to content

Commit 9bd0a91

Browse files
author
Eviee Py
committed
Start of Chatter class implementation
1 parent 4bb12b2 commit 9bd0a91

File tree

3 files changed

+114
-6
lines changed

3 files changed

+114
-6
lines changed

twitchio/ext/commands/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
if TYPE_CHECKING:
3838
from models.eventsub_ import ChatMessage
3939
from types_.options import Prefix_T
40-
from user import PartialUser
40+
from user import Chatter, PartialUser
4141

4242
from .bot import Bot
4343
from .core import Command
@@ -83,7 +83,7 @@ def invoked_with(self) -> str | None:
8383
return self._invoked_with
8484

8585
@property
86-
def chatter(self) -> PartialUser:
86+
def chatter(self) -> Chatter:
8787
return self._message.chatter
8888

8989
@property

twitchio/models/eventsub_.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from twitchio.models.charity import CharityValues
3333
from twitchio.models.chat import EmoteSet
3434
from twitchio.models.predictions import PredictionOutcome
35-
from twitchio.user import PartialUser
35+
from twitchio.user import Chatter, PartialUser
3636
from twitchio.utils import Colour, parse_timestamp
3737

3838

@@ -773,7 +773,7 @@ class ChatMessage(BaseChatMessage):
773773

774774
def __init__(self, payload: ChannelChatMessageEvent, *, http: HTTPClient) -> None:
775775
super().__init__(payload, http=http)
776-
self.chatter: PartialUser = PartialUser(payload["chatter_user_id"], payload["chatter_user_login"], http=http)
776+
777777
self.colour: Colour | None = Colour.from_hex(payload["color"]) if payload["color"] else None
778778
self.channel_points_id: str | None = payload["channel_points_custom_reward_id"]
779779
self.channel_points_animation_id: str | None = payload["channel_points_animation_id"]
@@ -801,6 +801,8 @@ def __init__(self, payload: ChannelChatMessageEvent, *, http: HTTPClient) -> Non
801801
ChatMessageBadge(badge) for badge in (payload.get("source_badges") or [])
802802
]
803803

804+
self.chatter: Chatter = Chatter(payload, broadcaster=self.broadcaster, badges=self.badges, http=http)
805+
804806
def __repr__(self) -> str:
805807
return f"<ChatMessage broadcaster={self.broadcaster} chatter={self.chatter} id={self.id} text={self.text}>"
806808

twitchio/user.py

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from .models.charity import CharityCampaign, CharityDonation
5454
from .models.chat import ChannelEmote, ChatBadge, ChatSettings, Chatters, SentMessage, SharedChatSession, UserEmote
5555
from .models.clips import Clip, CreatedClip
56+
from .models.eventsub_ import ChannelChatMessageEvent, ChatMessageBadge
5657
from .models.goals import Goal
5758
from .models.hype_train import HypeTrainEvent
5859
from .models.moderation import (
@@ -101,6 +102,17 @@ def __init__(self, id: str | int, name: str | None = None, *, http: HTTPClient)
101102
def __repr__(self) -> str:
102103
return f"<PartialUser id={self.id}, name={self.name}>"
103104

105+
def __str__(self) -> str:
106+
return self.name or "..."
107+
108+
@property
109+
def mention(self) -> str:
110+
"""Property returning the users name formatted to mention them in chat.
111+
112+
E.g. "@kappa"
113+
"""
114+
return f"@{self}"
115+
104116
async def start_commercial(self, *, length: int, token_for: str) -> CommercialStart:
105117
"""Starts a commercial on the specified channel.
106118
@@ -858,10 +870,10 @@ def fetch_user_emotes(
858870
max_results=max_results,
859871
)
860872

861-
async def fetch_chat_badges(self, token_for: str | None = None) -> list[ChatBadge]:
873+
async def fetch_badges(self, token_for: str | None = None) -> list[ChatBadge]:
862874
"""Fetches the broadcaster's list of custom chat badges.
863875
864-
If you wish to fetch globally available chat badges use If you wish to fetch a specific broadcaster's chat badges use [`client.fetch_chat_badges`][twitchio.client.fetch_chat_badges]
876+
If you wish to fetch globally available chat badges use If you wish to fetch a specific broadcaster's chat badges use [`client.fetch_badges`][twitchio.client.fetch_badges]
865877
866878
Parameters
867879
----------
@@ -3480,3 +3492,97 @@ def _to_dict(self) -> dict[str, dict[str, dict[str, bool | str | int | None]]]:
34803492
"overlay": {str(i + 1): overlay._to_dict() for i, overlay in enumerate(self.overlays)},
34813493
"component": {str(i + 1): component._to_dict() for i, component in enumerate(self.components)},
34823494
}
3495+
3496+
3497+
class Chatter(PartialUser):
3498+
__slots__ = (
3499+
"__dict__",
3500+
"_is_staff",
3501+
"_is_admin",
3502+
"_is_broadcaster",
3503+
"_is_moderator",
3504+
"_is_verified",
3505+
"_is_vip",
3506+
"_is_artist_badge",
3507+
"_is_founder",
3508+
"_is_subscriber",
3509+
"_is_no_audio",
3510+
"_is_no_video",
3511+
"_is_turbo",
3512+
"_is_premium",
3513+
"channel",
3514+
)
3515+
3516+
def __init__(
3517+
self,
3518+
payload: ChannelChatMessageEvent,
3519+
*,
3520+
http: HTTPClient,
3521+
broadcaster: PartialUser,
3522+
badges: list[ChatMessageBadge],
3523+
) -> None:
3524+
super().__init__(id=payload["chatter_user_id"], name=payload["chatter_user_login"], http=http)
3525+
3526+
slots = [s for s in self.__slots__ if not s.startswith("__")]
3527+
for badge in badges:
3528+
name = f"_is_{badge.set_id}".replace("-", "_")
3529+
3530+
if name in slots:
3531+
setattr(self, name, True)
3532+
3533+
self.channel: PartialUser = broadcaster
3534+
3535+
def __repr__(self) -> str:
3536+
return f"<Chatter id={self.id}, name={self.name}, channel={self.channel}>"
3537+
3538+
@property
3539+
def staff(self) -> bool:
3540+
return getattr(self, "_is_staff", False)
3541+
3542+
@property
3543+
def admin(self) -> bool:
3544+
return getattr(self, "_is_admin", False)
3545+
3546+
@property
3547+
def broadcaster(self) -> bool:
3548+
return getattr(self, "_is_broadcaster", False)
3549+
3550+
@property
3551+
def moderator(self) -> bool:
3552+
return getattr(self, "_is_moderator", False) or self.broadcaster
3553+
3554+
@property
3555+
def vip(self) -> bool:
3556+
return getattr(self, "_is_vip", False)
3557+
3558+
@property
3559+
def artist(self) -> bool:
3560+
return getattr(self, "_is_artist_badge", False)
3561+
3562+
@property
3563+
def founder(self) -> bool:
3564+
return getattr(self, "_is_founder", False)
3565+
3566+
@property
3567+
def subscriber(self) -> bool:
3568+
return getattr(self, "_is_subscriber", False)
3569+
3570+
@property
3571+
def no_audio(self) -> bool:
3572+
return getattr(self, "_is_no_audio", False)
3573+
3574+
@property
3575+
def no_video(self) -> bool:
3576+
return getattr(self, "_is_no_video", False)
3577+
3578+
@property
3579+
def partner(self) -> bool:
3580+
return getattr(self, "_is_partner", False)
3581+
3582+
@property
3583+
def turbo(self) -> bool:
3584+
return getattr(self, "_is_turbo", False)
3585+
3586+
@property
3587+
def prime(self) -> bool:
3588+
return getattr(self, "_is_premium", False)

0 commit comments

Comments
 (0)