Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/chatter.py
7790 views
"""1The MIT License (MIT)23Copyright (c) 2017-2021 TwitchIO45Permission is hereby granted, free of charge, to any person obtaining a6copy of this software and associated documentation files (the "Software"),7to deal in the Software without restriction, including without limitation8the rights to use, copy, modify, merge, publish, distribute, sublicense,9and/or sell copies of the Software, and to permit persons to whom the10Software is furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in13all copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21DEALINGS IN THE SOFTWARE.22"""2324from typing import Optional, TYPE_CHECKING, Dict2526from .abcs import Messageable27from .enums import PredictionEnum2829if TYPE_CHECKING:30from .user import User31from .websocket import WSConnection323334__all__ = ("PartialChatter", "Chatter")353637class PartialChatter(Messageable):3839__messageable_channel__ = False4041def __init__(self, websocket, **kwargs):42self._name = kwargs.get("name")43self._ws = websocket44self._channel = kwargs.get("channel", self._name)45self._message = kwargs.get("message")4647def __repr__(self):48return f"<PartialChatter name: {self._name}, channel: {self._channel}>"4950def __eq__(self, other):51return other.name == self.name and other.channel.name == other.channel.name5253def __hash__(self):54return hash(self.name + self.channel.name)5556async def user(self) -> "User":57"""|coro|5859Fetches a :class:`twitchio.User` object based off the chatters channel name6061Returns62--------63:class:`twitchio.User`64"""65return (await self._ws._client.fetch_users(names=[self.name]))[0]6667@property68def name(self):69"""The users name"""70return self._name7172@property73def channel(self):74"""The channel associated with the user."""75return self._channel7677def _fetch_channel(self):78return self # Abstract method7980def _fetch_websocket(self):81return self._ws # Abstract method8283def _fetch_message(self):84return self._message # Abstract method8586def _bot_is_mod(self):87return False888990class Chatter(PartialChatter):9192__slots__ = (93"_name",94"_channel",95"_tags",96"_badges",97"_cached_badges",98"_ws",99"id",100"_turbo",101"_sub",102"_mod",103"_display_name",104"_colour",105)106107__messageable_channel__ = False108109def __init__(self, websocket: "WSConnection", **kwargs):110super(Chatter, self).__init__(websocket, **kwargs)111self._tags = kwargs.get("tags", None)112self._ws = websocket113114self._cached_badges: Optional[Dict[str, str]] = None115116if not self._tags:117self.id = None118self._badges = None119self._turbo = None120self._sub = None121self._mod = None122self._display_name = None123self._colour = None124return125126self.id = self._tags.get("user-id")127self._badges = self._tags.get("badges")128self._turbo = self._tags.get("turbo")129self._sub = int(self._tags["subscriber"])130self._mod = int(self._tags["mod"])131self._display_name = self._tags["display-name"]132self._colour = self._tags["color"]133134if self._badges:135self._cached_badges = {k: v for k, v in [badge.split("/") for badge in self._badges.split(",")]}136137def _bot_is_mod(self):138cache = self._ws._cache[self._channel.name] # noqa139for user in cache:140if user.name == self._ws.nick:141try:142mod = user.is_mod143except AttributeError:144return False145146return mod147148@property149def name(self) -> str:150"""The users name. This may be formatted differently than display name."""151return self._name or (self.display_name and self.display_name.lower())152153@property154def badges(self) -> dict:155"""The users badges."""156if self._cached_badges:157return self._cached_badges.copy()158159return {}160161@property162def display_name(self) -> str:163"""The users display name."""164return self._display_name165166@property167def mention(self) -> str:168"""Mentions the users display name by prefixing it with '@'"""169return f"@{self._display_name}"170171@property172def colour(self) -> str:173"""The users colour. Alias to color."""174return self._colour175176@property177def color(self) -> str:178"""The users color."""179return self.colour180181@property182def is_broadcaster(self) -> bool:183"""A boolean indicating whether the User is the broadcaster of the current channel."""184185return "broadcaster" in self.badges186187@property188def is_mod(self) -> bool:189"""A boolean indicating whether the User is a moderator of the current channel."""190if self._mod == 1:191return True192193return self.channel.name == self.name.lower()194195@property196def is_turbo(self) -> Optional[bool]:197"""A boolean indicating whether the User is Turbo.198199Could be None if no Tags were received.200"""201return self._turbo202203@property204def is_subscriber(self) -> bool:205"""A boolean indicating whether the User is a subscriber of the current channel.206207.. note::208209changed in 2.1.0: return value is no longer optional. founders will now appear as subscribers210"""211return bool(self._sub) or "founder" in self.badges212213@property214def prediction(self) -> Optional[PredictionEnum]:215"""216The users current prediction, if one exists.217218Returns219--------220Optional[:class:`twitchio.enums.PredictionEnum`]221"""222if "blue-1" in self.badges:223return PredictionEnum("blue-1")224225elif "pink-2" in self.badges:226return PredictionEnum("pink-2")227228return None229230231class WhisperChatter(PartialChatter):232233__messageable_channel__ = False234235def __init__(self, websocket: "WSConnection", **kwargs):236super().__init__(websocket, **kwargs)237238def __repr__(self):239return f"<WhisperChatter name: {self._name}>"240241@property242def channel(self):243return None244245def _fetch_channel(self):246return self # Abstract method247248def _fetch_websocket(self):249return self._ws # Abstract method250251def _bot_is_mod(self):252return False253254255