forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrations.py
374 lines (308 loc) · 11.5 KB
/
integrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
The MIT License (MIT)
Copyright (c) 2015-2021 Rapptz
Copyright (c) 2021-present Pycord Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import datetime
from typing import TYPE_CHECKING, Any
from .enums import ExpireBehaviour, try_enum
from .errors import InvalidArgument
from .user import User
from .utils import MISSING, _get_as_snowflake, parse_time
__all__ = (
"IntegrationAccount",
"IntegrationApplication",
"Integration",
"StreamIntegration",
"BotIntegration",
)
if TYPE_CHECKING:
from .guild import Guild
from .role import Role
from .types.integration import BotIntegration as BotIntegrationPayload
from .types.integration import Integration as IntegrationPayload
from .types.integration import IntegrationAccount as IntegrationAccountPayload
from .types.integration import (
IntegrationApplication as IntegrationApplicationPayload,
)
from .types.integration import IntegrationType
from .types.integration import StreamIntegration as StreamIntegrationPayload
class IntegrationAccount:
"""Represents an integration account.
.. versionadded:: 1.4
Attributes
----------
id: :class:`str`
The account ID.
name: :class:`str`
The account name.
"""
__slots__ = ("id", "name")
def __init__(self, data: IntegrationAccountPayload) -> None:
self.id: str = data["id"]
self.name: str = data["name"]
def __repr__(self) -> str:
return f"<IntegrationAccount id={self.id} name={self.name!r}>"
class Integration:
"""Represents a guild integration.
.. versionadded:: 1.4
Attributes
----------
id: :class:`int`
The integration ID.
name: :class:`str`
The integration name.
guild: :class:`Guild`
The guild of the integration.
type: :class:`str`
The integration type (i.e. Twitch).
enabled: :class:`bool`
Whether the integration is currently enabled.
account: :class:`IntegrationAccount`
The account linked to this integration.
user: :class:`User`
The user that added this integration.
"""
__slots__ = (
"guild",
"id",
"_state",
"type",
"name",
"account",
"user",
"enabled",
)
def __init__(self, *, data: IntegrationPayload, guild: Guild) -> None:
self.guild = guild
self._state = guild._state
self._from_data(data)
def __repr__(self):
return f"<{self.__class__.__name__} id={self.id} name={self.name!r}>"
def _from_data(self, data: IntegrationPayload) -> None:
self.id: int = int(data["id"])
self.type: IntegrationType = data["type"]
self.name: str = data["name"]
self.account: IntegrationAccount = IntegrationAccount(data["account"])
user = data.get("user")
self.user = User(state=self._state, data=user) if user else None
self.enabled: bool = data["enabled"]
async def delete(self, *, reason: str | None = None) -> None:
"""|coro|
Deletes the integration.
You must have the :attr:`~Permissions.manage_guild` permission to
do this.
Parameters
----------
reason: :class:`str`
The reason the integration was deleted. Shows up on the audit log.
.. versionadded:: 2.0
Raises
------
Forbidden
You do not have permission to delete the integration.
HTTPException
Deleting the integration failed.
"""
await self._state.http.delete_integration(self.guild.id, self.id, reason=reason)
class StreamIntegration(Integration):
"""Represents a stream integration for Twitch or YouTube.
.. versionadded:: 2.0
Attributes
----------
id: :class:`int`
The integration ID.
name: :class:`str`
The integration name.
guild: :class:`Guild`
The guild of the integration.
type: :class:`str`
The integration type (i.e. Twitch).
enabled: :class:`bool`
Whether the integration is currently enabled.
syncing: :class:`bool`
Where the integration is currently syncing.
enable_emoticons: Optional[:class:`bool`]
Whether emoticons should be synced for this integration (currently twitch only).
expire_behaviour: :class:`ExpireBehaviour`
The behaviour of expiring subscribers. Aliased to ``expire_behavior`` as well.
expire_grace_period: :class:`int`
The grace period (in days) for expiring subscribers.
user: :class:`User`
The user for the integration.
account: :class:`IntegrationAccount`
The integration account information.
synced_at: :class:`datetime.datetime`
An aware UTC datetime representing when the integration was last synced.
"""
__slots__ = (
"revoked",
"expire_behaviour",
"expire_grace_period",
"synced_at",
"_role_id",
"syncing",
"enable_emoticons",
"subscriber_count",
)
def _from_data(self, data: StreamIntegrationPayload) -> None:
super()._from_data(data)
self.revoked: bool = data["revoked"]
self.expire_behaviour: ExpireBehaviour = try_enum(
ExpireBehaviour, data["expire_behavior"]
)
self.expire_grace_period: int = data["expire_grace_period"]
self.synced_at: datetime.datetime = parse_time(data["synced_at"])
self._role_id: int | None = _get_as_snowflake(data, "role_id")
self.syncing: bool = data["syncing"]
self.enable_emoticons: bool = data["enable_emoticons"]
self.subscriber_count: int = data["subscriber_count"]
@property
def expire_behavior(self) -> ExpireBehaviour:
"""An alias for :attr:`expire_behaviour`."""
return self.expire_behaviour
@property
def role(self) -> Role | None:
"""The role which the integration uses for subscribers."""
return self.guild.get_role(self._role_id) # type: ignore
async def edit(
self,
*,
expire_behaviour: ExpireBehaviour = MISSING,
expire_grace_period: int = MISSING,
enable_emoticons: bool = MISSING,
) -> None:
"""|coro|
Edits the integration.
You must have the :attr:`~Permissions.manage_guild` permission to
do this.
Parameters
----------
expire_behaviour: :class:`ExpireBehaviour`
The behaviour when an integration subscription lapses. Aliased to ``expire_behavior`` as well.
expire_grace_period: :class:`int`
The period (in days) where the integration will ignore lapsed subscriptions.
enable_emoticons: :class:`bool`
Where emoticons should be synced for this integration (currently twitch only).
Raises
------
Forbidden
You do not have permission to edit the integration.
HTTPException
Editing the guild failed.
InvalidArgument
``expire_behaviour`` did not receive a :class:`ExpireBehaviour`.
"""
payload: dict[str, Any] = {}
if expire_behaviour is not MISSING:
if not isinstance(expire_behaviour, ExpireBehaviour):
raise InvalidArgument(
"expire_behaviour field must be of type ExpireBehaviour"
)
payload["expire_behavior"] = expire_behaviour.value
if expire_grace_period is not MISSING:
payload["expire_grace_period"] = expire_grace_period
if enable_emoticons is not MISSING:
payload["enable_emoticons"] = enable_emoticons
# This endpoint is undocumented.
# Unsure if it returns the data or not as a result
await self._state.http.edit_integration(self.guild.id, self.id, **payload)
async def sync(self) -> None:
"""|coro|
Syncs the integration.
You must have the :attr:`~Permissions.manage_guild` permission to
do this.
Raises
------
Forbidden
You do not have permission to sync the integration.
HTTPException
Syncing the integration failed.
"""
await self._state.http.sync_integration(self.guild.id, self.id)
self.synced_at = datetime.datetime.now(datetime.timezone.utc)
class IntegrationApplication:
"""Represents an application for a bot integration.
.. versionadded:: 2.0
Attributes
----------
id: :class:`int`
The ID for this application.
name: :class:`str`
The application's name.
icon: Optional[:class:`str`]
The application's icon hash.
description: :class:`str`
The application's description. Can be an empty string.
summary: :class:`str`
The summary of the application. Can be an empty string.
user: Optional[:class:`User`]
The bot user on this application.
"""
__slots__ = (
"id",
"name",
"icon",
"description",
"summary",
"user",
)
def __init__(self, *, data: IntegrationApplicationPayload, state):
self.id: int = int(data["id"])
self.name: str = data["name"]
self.icon: str | None = data["icon"]
self.description: str = data["description"]
self.summary: str = data["summary"]
user = data.get("bot")
self.user: User | None = User(state=state, data=user) if user else None
class BotIntegration(Integration):
"""Represents a bot integration on discord.
.. versionadded:: 2.0
Attributes
----------
id: :class:`int`
The integration ID.
name: :class:`str`
The integration name.
guild: :class:`Guild`
The guild of the integration.
type: :class:`str`
The integration type (i.e. Twitch).
enabled: :class:`bool`
Whether the integration is currently enabled.
user: :class:`User`
The user that added this integration.
account: :class:`IntegrationAccount`
The integration account information.
application: :class:`IntegrationApplication`
The application tied to this integration.
"""
__slots__ = ("application",)
def _from_data(self, data: BotIntegrationPayload) -> None:
super()._from_data(data)
self.application = IntegrationApplication(
data=data["application"], state=self._state
)
def _integration_factory(value: str) -> tuple[type[Integration], str]:
if value == "discord":
return BotIntegration, value
elif value in {"twitch", "youtube"}:
return StreamIntegration, value
else:
return Integration, value