Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/channel.py
7812 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"""2324import datetime25from typing import Optional, Union, Set, TYPE_CHECKING2627from .abcs import Messageable28from .chatter import Chatter, PartialChatter29from .models import BitsLeaderboard3031if TYPE_CHECKING:32from .websocket import WSConnection33from .user import User343536__all__ = ("Channel",)373839class Channel(Messageable):4041__slots__ = ("_name", "_ws", "_message")4243__messageable_channel__ = True4445def __init__(self, name: str, websocket: "WSConnection"):46self._name = name47self._ws = websocket4849def __eq__(self, other):50return other.name == self._name5152def __hash__(self):53return hash(self.name)5455def __repr__(self):56return f"<Channel name: {self.name}>"5758def _fetch_channel(self):59return self # Abstract method6061def _fetch_websocket(self):62return self._ws # Abstract method6364def _fetch_message(self):65return self._message # Abstract method6667def _bot_is_mod(self):68cache = self._ws._cache[self.name] # noqa69for user in cache:70if user.name == self._ws.nick:71try:72mod = user.is_mod73except AttributeError:74return False7576return mod7778@property79def name(self) -> str:80"""The channel name."""81return self._name8283@property84def chatters(self) -> Optional[Set[Union[Chatter, PartialChatter]]]:85"""The channels current chatters."""86try:87chatters = self._ws._cache[self._name] # noqa88except KeyError:89return None9091return chatters9293def get_chatter(self, name: str) -> Optional[Union[Chatter, PartialChatter]]:94"""Retrieve a chatter from the channels user cache.9596Parameters97-----------98name: str99The chatter's name to try and retrieve.100101Returns102--------103Union[:class:`twitchio.chatter.Chatter`, :class:`twitchio.chatter.PartialChatter`]104Could be a :class:`twitchio.user.PartialChatter` depending on how the user joined the channel.105Returns None if no user was found.106"""107name = name.lower()108109try:110cache = self._ws._cache[self._name] # noqa111for chatter in cache:112if chatter.name == name:113return chatter114115return None116except KeyError:117return None118119async def user(self, force=False) -> "User":120"""|coro|121122Fetches the User from the api.123124Parameters125-----------126force: :class:`bool`127Whether to force a fetch from the api, or try and pull from the cache. Defaults to `False`128129Returns130--------131:class:`twitchio.User` the user associated with the channel132"""133return (await self._ws._client.fetch_users(names=[self._name], force=force))[0]134135async def fetch_bits_leaderboard(136self, token: str, period: str = "all", user_id: int = None, started_at: datetime.datetime = None137) -> BitsLeaderboard:138"""|coro|139140Fetches the bits leaderboard for the channel. This requires an OAuth token with the bits:read scope.141142Parameters143-----------144token: :class:`str`145the OAuth token with the bits:read scope146period: Optional[:class:`str`]147one of `day`, `week`, `month`, `year`, or `all`, defaults to `all`148started_at: Optional[:class:`datetime.datetime`]149the timestamp to start the period at. This is ignored if the period is `all`150user_id: Optional[:class:`int`]151the id of the user to fetch for152"""153data = await self._ws._client._http.get_bits_board(token, period, user_id, started_at)154return BitsLeaderboard(self._ws._client._http, data)155156async def whisper(self, content: str):157"""|coro|158159Whispers the user behind the channel. This will not work if the channel is the same as the one you are sending the message from.160161.. warning:162Whispers are very unreliable on twitch. If you do not receive a whisper, this is probably twitch's fault, not the library's.163164Parameters165-----------166content: :class:`str`167The content to send to the user168"""169await self.send(f"/w {self.name} {content}")170171172