Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/channel.py
7812 views
1
"""
2
The MIT License (MIT)
3
4
Copyright (c) 2017-2021 TwitchIO
5
6
Permission is hereby granted, free of charge, to any person obtaining a
7
copy of this software and associated documentation files (the "Software"),
8
to deal in the Software without restriction, including without limitation
9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
and/or sell copies of the Software, and to permit persons to whom the
11
Software is furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
DEALINGS IN THE SOFTWARE.
23
"""
24
25
import datetime
26
from typing import Optional, Union, Set, TYPE_CHECKING
27
28
from .abcs import Messageable
29
from .chatter import Chatter, PartialChatter
30
from .models import BitsLeaderboard
31
32
if TYPE_CHECKING:
33
from .websocket import WSConnection
34
from .user import User
35
36
37
__all__ = ("Channel",)
38
39
40
class Channel(Messageable):
41
42
__slots__ = ("_name", "_ws", "_message")
43
44
__messageable_channel__ = True
45
46
def __init__(self, name: str, websocket: "WSConnection"):
47
self._name = name
48
self._ws = websocket
49
50
def __eq__(self, other):
51
return other.name == self._name
52
53
def __hash__(self):
54
return hash(self.name)
55
56
def __repr__(self):
57
return f"<Channel name: {self.name}>"
58
59
def _fetch_channel(self):
60
return self # Abstract method
61
62
def _fetch_websocket(self):
63
return self._ws # Abstract method
64
65
def _fetch_message(self):
66
return self._message # Abstract method
67
68
def _bot_is_mod(self):
69
cache = self._ws._cache[self.name] # noqa
70
for user in cache:
71
if user.name == self._ws.nick:
72
try:
73
mod = user.is_mod
74
except AttributeError:
75
return False
76
77
return mod
78
79
@property
80
def name(self) -> str:
81
"""The channel name."""
82
return self._name
83
84
@property
85
def chatters(self) -> Optional[Set[Union[Chatter, PartialChatter]]]:
86
"""The channels current chatters."""
87
try:
88
chatters = self._ws._cache[self._name] # noqa
89
except KeyError:
90
return None
91
92
return chatters
93
94
def get_chatter(self, name: str) -> Optional[Union[Chatter, PartialChatter]]:
95
"""Retrieve a chatter from the channels user cache.
96
97
Parameters
98
-----------
99
name: str
100
The chatter's name to try and retrieve.
101
102
Returns
103
--------
104
Union[:class:`twitchio.chatter.Chatter`, :class:`twitchio.chatter.PartialChatter`]
105
Could be a :class:`twitchio.user.PartialChatter` depending on how the user joined the channel.
106
Returns None if no user was found.
107
"""
108
name = name.lower()
109
110
try:
111
cache = self._ws._cache[self._name] # noqa
112
for chatter in cache:
113
if chatter.name == name:
114
return chatter
115
116
return None
117
except KeyError:
118
return None
119
120
async def user(self, force=False) -> "User":
121
"""|coro|
122
123
Fetches the User from the api.
124
125
Parameters
126
-----------
127
force: :class:`bool`
128
Whether to force a fetch from the api, or try and pull from the cache. Defaults to `False`
129
130
Returns
131
--------
132
:class:`twitchio.User` the user associated with the channel
133
"""
134
return (await self._ws._client.fetch_users(names=[self._name], force=force))[0]
135
136
async def fetch_bits_leaderboard(
137
self, token: str, period: str = "all", user_id: int = None, started_at: datetime.datetime = None
138
) -> BitsLeaderboard:
139
"""|coro|
140
141
Fetches the bits leaderboard for the channel. This requires an OAuth token with the bits:read scope.
142
143
Parameters
144
-----------
145
token: :class:`str`
146
the OAuth token with the bits:read scope
147
period: Optional[:class:`str`]
148
one of `day`, `week`, `month`, `year`, or `all`, defaults to `all`
149
started_at: Optional[:class:`datetime.datetime`]
150
the timestamp to start the period at. This is ignored if the period is `all`
151
user_id: Optional[:class:`int`]
152
the id of the user to fetch for
153
"""
154
data = await self._ws._client._http.get_bits_board(token, period, user_id, started_at)
155
return BitsLeaderboard(self._ws._client._http, data)
156
157
async def whisper(self, content: str):
158
"""|coro|
159
160
Whispers the user behind the channel. This will not work if the channel is the same as the one you are sending the message from.
161
162
.. warning:
163
Whispers are very unreliable on twitch. If you do not receive a whisper, this is probably twitch's fault, not the library's.
164
165
Parameters
166
-----------
167
content: :class:`str`
168
The content to send to the user
169
"""
170
await self.send(f"/w {self.name} {content}")
171
172