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/abcs.py
7783 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 abc
26
import time
27
28
from .cooldowns import RateBucket
29
from .errors import *
30
31
32
class IRCLimiterMapping:
33
def __init__(self):
34
self.buckets = {}
35
36
def get_bucket(self, channel: str, method: str) -> RateBucket:
37
try:
38
bucket = self.buckets[channel]
39
except KeyError:
40
bucket = RateBucket(method=method)
41
self.buckets[channel] = bucket
42
43
if bucket.method != method:
44
bucket.method = method
45
bucket.limit = bucket.MODLIMIT if method == "mod" else bucket.IRCLIMIT
46
self.buckets[channel] = bucket
47
48
return bucket
49
50
51
limiter = IRCLimiterMapping()
52
53
54
class Messageable(abc.ABC):
55
56
__slots__ = ()
57
58
@abc.abstractmethod
59
def _fetch_channel(self):
60
raise NotImplementedError
61
62
@abc.abstractmethod
63
def _fetch_websocket(self):
64
raise NotImplementedError
65
66
@abc.abstractmethod
67
def _fetch_message(self):
68
raise NotImplementedError
69
70
@abc.abstractmethod
71
def _bot_is_mod(self):
72
raise NotImplementedError
73
74
def check_bucket(self, channel):
75
mod = self._bot_is_mod()
76
77
if mod:
78
bucket = limiter.get_bucket(channel=channel, method="mod")
79
else:
80
bucket = limiter.get_bucket(channel=channel, method="irc")
81
82
now = time.time()
83
bucket.update()
84
85
if bucket.limited:
86
raise IRCCooldownError(
87
f"IRC Message rate limit reached for channel <{channel}>."
88
f" Please try again in {bucket._reset - now:.2f}s"
89
)
90
91
def check_content(self, content: str):
92
if len(content) > 500:
93
raise InvalidContent("Content must not exceed 500 characters.")
94
95
async def send(self, content: str):
96
"""|coro|
97
98
99
Send a message to the destination associated with the dataclass.
100
101
Destination will either be a channel or user.
102
103
Parameters
104
------------
105
content: str
106
The content you wish to send as a message. The content must be a string.
107
108
Raises
109
--------
110
InvalidContent
111
Invalid content.
112
"""
113
entity = self._fetch_channel()
114
ws = self._fetch_websocket()
115
116
self.check_content(content)
117
self.check_bucket(channel=entity.name)
118
119
try:
120
name = entity.channel.name
121
except AttributeError:
122
name = entity.name
123
124
if entity.__messageable_channel__:
125
await ws.send(f"PRIVMSG #{name} :{content}\r\n")
126
else:
127
await ws.send(f"PRIVMSG #jtv :/w {entity.name} {content}\r\n")
128
129
async def reply(self, content: str):
130
"""|coro|
131
132
133
Send a message in reply to the user who sent a message in the destination
134
associated with the dataclass.
135
136
Destination will be the context of which the message/command was sent.
137
138
Parameters
139
------------
140
content: str
141
The content you wish to send as a message. The content must be a string.
142
143
Raises
144
--------
145
InvalidContent
146
Invalid content.
147
"""
148
entity = self._fetch_channel()
149
ws = self._fetch_websocket()
150
message = self._fetch_message()
151
152
self.check_content(content)
153
self.check_bucket(channel=entity.name)
154
155
try:
156
name = entity.channel.name
157
except AttributeError:
158
name = entity.name
159
160
if entity.__messageable_channel__:
161
await ws.reply(message.id, f"PRIVMSG #{name} :{content}\r\n")
162
else:
163
await ws.send(f"PRIVMSG #jtv :/w {name} {content}\r\n")
164
165