This repository was archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathguild.py
1035 lines (948 loc) · 35.8 KB
/
guild.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# cython: language_level=3
# 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 .auto_moderation import (
AutoModAction,
AutoModEventType,
AutoModRule,
AutoModTriggerMetadata,
AutoModTriggerType,
)
from .channel import (
CHANNEL_TYPE,
AnnouncementThread,
CategoryChannel,
Channel,
ForumTag,
TextChannel,
Thread,
VoiceChannel,
_Overwrite,
identify_channel,
)
from .enums import (
ChannelType,
DefaultMessageNotificationLevel,
ExplicitContentFilterLevel,
MFALevel,
NSFWLevel,
PremiumTier,
SortOrderType,
VerificationLevel,
VideoQualityMode,
)
from .file import File
from .flags import Permissions, SystemChannelFlags
from .media import Emoji, Sticker
from .member import Member, MemberPaginator
from .missing import MISSING, Maybe, MissingEnum
from .pages.paginator import Page, Paginator
from .role import Role
from .scheduled_event import ScheduledEvent
from .snowflake import Snowflake
from .types import (
GUILD_FEATURE,
LOCALE,
Ban as DiscordBan,
Guild as DiscordGuild,
GuildPreview as DiscordGuildPreview,
UnavailableGuild,
Widget as DiscordWidget,
WidgetSettings as DiscordWidgetSettings,
)
from .user import User
from .utils import remove_undefined
from .welcome_screen import WelcomeScreen
if TYPE_CHECKING:
from .state import State
class ChannelPosition:
__slots__ = ('id', 'position', 'lock_permissions', 'parent_id')
def __init__(
self,
id: Snowflake,
position: int,
*,
lock_permissions: bool = False,
parent_id: Snowflake | None | MissingEnum,
) -> None:
self.id = id
self.position = position
self.lock_permissions = lock_permissions
self.parent_id = parent_id
def to_dict(self) -> dict[str, Any]:
payload = {
'id': self.id,
'position': self.position,
'lock_permissions': self.lock_permissions,
'parent_id': self.parent_id,
}
return remove_undefined(**payload)
class Guild:
__slots__ = (
'_state',
'_icon',
'_icon_hash',
'_splash',
'_discovery_splash',
'_afk_channel_id',
'_widget_channel_id',
'_roles',
'_emojis',
'_application_id',
'_system_channel_id',
'_rules_channel_id',
'_public_updates_channel_id',
'_banner',
'_welcome_screen',
'id',
'unavailable',
'name',
'owner',
'owner_id',
'permissions',
'afk_channel_id',
'afk_timeout',
'widget_enabled',
'widget_channel_id',
'verification_level',
'default_message_notifications',
'explicit_content_filter',
'roles',
'emojis',
'features',
'mfa_level',
'application_id',
'system_channel_id',
'rules_channel_id',
'max_presences',
'max_members',
'vanity_url',
'description',
'premium_tier',
'premium_subscription_count',
'preferred_locale',
'public_updates_channel_id',
'max_video_channel_users',
'approximate_member_count',
'approximate_presence_count',
'welcome_screen',
'nsfw_level',
'stickers',
'premium_progress_bar_enabled',
'system_channel_flags',
)
def __init__(self, data: DiscordGuild | UnavailableGuild, state: State) -> None:
self.id: Snowflake = Snowflake(data['id'])
self._state = state
if not data.get('unavailable'):
self.unavailable: bool = False
self.name: str = data['name']
# TODO: Asset classes
self._icon: str | None = data['icon']
self._icon_hash: str | None | MissingEnum = data.get('icon_hash')
self._splash: str | None = data['splash']
self._discovery_splash: str | None = data['discovery_splash']
self.owner: bool | MissingEnum = data.get('owner', MISSING)
self.owner_id: Snowflake = Snowflake(data.get('owner_id'))
self.permissions: Permissions = Permissions.from_value(
data.get('permissions', 0)
)
self._afk_channel_id: str | None = data.get('afk_channel_id')
self.afk_channel_id: Snowflake | None = (
Snowflake(self._afk_channel_id)
if self._afk_channel_id is not None
else None
)
self.afk_timeout: int = data.get('afk_timeout')
self.widget_enabled: bool | MissingEnum = data.get(
'widget_enabled', MISSING
)
self._widget_channel_id: str | None | MissingEnum = data.get(
'widget_channel_id'
)
self.widget_channel_id: MissingEnum | Snowflake | None = (
Snowflake(self._widget_channel_id)
if isinstance(self._widget_channel_id, str)
else self._widget_channel_id
)
self.verification_level: VerificationLevel = VerificationLevel(
data['verification_level']
)
self.default_message_notifications: DefaultMessageNotificationLevel = (
DefaultMessageNotificationLevel(data['default_message_notifications'])
)
self.explicit_content_filter: ExplicitContentFilterLevel = (
ExplicitContentFilterLevel(data['explicit_content_filter'])
)
self._roles: list[dict[str, Any]] = data.get('roles', [])
self._process_roles()
self._emojis: list[dict[str, Any]] = data.get('emojis', [])
self._process_emojis()
self.features: list[GUILD_FEATURE] = data['features']
self.mfa_level: MFALevel = MFALevel(data['mfa_level'])
self._application_id: str | None = data.get('application_id')
self.application_id: Snowflake | None = (
Snowflake(self._application_id)
if self._application_id is not None
else None
)
self._system_channel_id: str | None = data.get('system_channel_id')
self.system_channel_id: Snowflake | None = (
Snowflake(self._system_channel_id)
if self._system_channel_id is not None
else None
)
self.system_channel_flags: SystemChannelFlags = (
SystemChannelFlags.from_value(data['system_channel_flags'])
)
self._rules_channel_id: str | None = data.get('rules_channel_id')
self.rules_channel_id: Snowflake | None = (
Snowflake(self._rules_channel_id)
if self._rules_channel_id is not None
else None
)
self.max_presences: int | MissingEnum = data.get('max_presences', MISSING)
self.max_members: int | MissingEnum = data.get('max_members', MISSING)
self.vanity_url: str | None = data.get('vanity_url_code')
self.description: str | None = data.get('description')
self._banner: str | None = data.get('banner')
self.premium_tier: PremiumTier = PremiumTier(data['premium_tier'])
self.premium_subscription_count: int | MissingEnum = data.get(
'premium_subscription_count', MISSING
)
self.preferred_locale: LOCALE = data['preferred_locale']
self._public_updates_channel_id: str | None = data[
'public_updates_channel_id'
]
self.public_updates_channel_id: Snowflake | None = (
Snowflake(self._public_updates_channel_id)
if self._public_updates_channel_id is not None
else None
)
self.max_video_channel_users: int | MissingEnum = data.get(
'max_video_channel_users', MISSING
)
self.approximate_member_count: int | MissingEnum = data.get(
'approximate_member_count', MISSING
)
self.approximate_presence_count: int | MissingEnum = data.get(
'approximate_presence_count', MISSING
)
self._welcome_screen = data.get('welcome_screen', MISSING)
self.welcome_screen: WelcomeScreen | MissingEnum = (
WelcomeScreen(self._welcome_screen)
if self._welcome_screen is not MISSING
else MISSING
)
self.nsfw_level: NSFWLevel = NSFWLevel(data.get('nsfw_level', 0))
self.stickers: list[Sticker] = [
Sticker(d, self._state) for d in data.get('stickers', [])
]
self.premium_progress_bar_enabled: bool = data[
'premium_progress_bar_enabled'
]
else:
self.unavailable: bool = True
def _process_roles(self) -> None:
self.roles: list[Role] = [Role(role, state=self._state) for role in self._roles]
def _process_emojis(self) -> None:
self.emojis: list[Emoji] = []
for emoji in self._emojis:
emo = Emoji(emoji, state=self._state)
emo._inject_roles(self.roles)
self.emojis.append(emo)
async def list_auto_moderation_rules(self) -> list[AutoModRule]:
"""list the auto moderation rules for this guild.
Returns
-------
list[:class:`AutoModRule`]
The auto moderation rules for this guild.
"""
data = await self._state.http.list_auto_moderation_rules_for_guild(self.id)
return [AutoModRule(rule, self._state) for rule in data]
async def get_auto_moderation_rule(self, rule_id: int) -> AutoModRule:
"""Get an auto moderation rule for this guild.
Parameters
----------
rule_id: :class:`int`
The ID of the rule to get.
Returns
-------
:class:`AutoModRule`
The auto moderation rule for this guild.
"""
data = await self._state.http.get_auto_moderation_rule_for_guild(
self.id, rule_id
)
return AutoModRule(data, self._state)
async def create_auto_moderation_rule(
self,
*,
name: str,
event_type: AutoModEventType,
trigger_type: AutoModTriggerType,
trigger_metadata: AutoModTriggerMetadata | MissingEnum = MISSING,
actions: list[AutoModAction],
enabled: bool = False,
exempt_roles: list[Snowflake] | MissingEnum = MISSING,
exempt_channels: list[Snowflake] | MissingEnum = MISSING,
reason: str | None = None,
) -> AutoModRule:
"""Create an auto moderation rule for this guild.
Parameters
----------
name: :class:`str`
The name of the rule.
event_type: :class:`AutoModEventType`
The event type of the rule.
trigger_type: :class:`AutoModTriggerType`
The trigger type of the rule.
trigger_metadata: :class:`AutoModTriggerMetadata`
The trigger metadata of the rule.
actions: list[:class:`AutoModAction`]
The actions to take when the rule is triggered.
enabled: :class:`bool`
Whether the rule is enabled.
exempt_roles: list[:class:`Snowflake`]
The roles to exempt from this rule.
exempt_channels: list[:class:`Snowflake`]
The channels to exempt from this rule.
reason: :class:`str` | None
The reason for creating the rule.
Returns
-------
:class:`AutoModRule`
The auto moderation rule for this guild.
"""
data = await self._state.http.create_auto_moderation_rule_for_guild(
self.id,
name=name,
event_type=event_type,
trigger_type=trigger_type,
trigger_metadata=trigger_metadata,
actions=actions,
enabled=enabled,
exempt_roles=exempt_roles,
exempt_channels=exempt_channels,
reason=reason,
)
return AutoModRule(data, self._state)
async def create_emoji(
self,
*,
name: str,
image: bytes, # TODO
roles: list[Role] | None = None,
reason: str | None = None,
) -> Emoji:
"""Creates an emoji.
Parameters
----------
name: :class:`str`
The name of the emoji.
image: :class:`bytes`
The image data of the emoji.
roles: list[:class:`Role`]
The roles that can use the emoji.
reason: :class:`str` | None
The reason for creating the emoji. Shows up on the audit log.
Returns
-------
:class:`Emoji`
The created emoji.
"""
data = await self._state.http.create_guild_emoji(
self.id, name, image, roles, reason
)
return Emoji(data, state=self._state)
async def edit_emoji(
self,
emoji_id: Snowflake,
*,
name: str | MissingEnum = MISSING,
roles: list[Role] | MissingEnum = MISSING,
reason: str | None = None,
) -> Emoji:
"""Edits the emoji.
Parameters
----------
emoji_id: :class:`Snowflake`
The ID of the emoji to edit.
name: :class:`str`
The new name of the emoji.
roles: list[:class:`Role`]
The new roles that can use the emoji.
reason: :class:`str` | None
The reason for editing the emoji. Shows up on the audit log.
Returns
-------
:class:`Emoji`
The edited emoji.
"""
data = await self._state.http.modify_guild_emoji(
self.id, emoji_id, name=name, roles=roles, reason=reason
)
return Emoji(data, self._state)
async def delete_emoji(
self, emoji_id: Snowflake, *, reason: str | None = None
) -> None:
"""Deletes an emoji.
Parameters
----------
emoji_id: :class:`Snowflake`
The ID of the emoji to delete.
reason: :class:`str` | None
The reason for deleting the emoji. Shows up on the audit log.
"""
await self._state.http.delete_guild_emoji(self.id, emoji_id, reason=reason)
async def get_preview(self) -> GuildPreview:
"""Get a preview of this guild.
Returns
-------
:class:`GuildPreview`
The preview of this guild.
"""
data = await self._state.http.get_guild_preview(self.id)
return GuildPreview(data, self._state)
async def edit(
self,
*,
name: str | MissingEnum = MISSING,
verification_level: VerificationLevel | None | MissingEnum = MISSING,
default_message_notifications: DefaultMessageNotificationLevel
| None
| MissingEnum = MISSING,
explicit_content_filter: ExplicitContentFilterLevel
| None
| MissingEnum = MISSING,
afk_channel: VoiceChannel | None | MissingEnum = MISSING,
afk_timeout: int | MissingEnum = MISSING,
icon: File | None | MissingEnum = MISSING,
owner: User | MissingEnum = MISSING,
splash: File | None | MissingEnum = MISSING,
discovery_splash: File | None | MissingEnum = MISSING,
banner: File | None | MissingEnum = MISSING,
system_channel: TextChannel | None | MissingEnum = MISSING,
rules_channel: TextChannel | None | MissingEnum = MISSING,
public_updates_channel: TextChannel | None | MissingEnum = MISSING,
preferred_locale: str | None | MissingEnum = MISSING,
features: list[GUILD_FEATURE] | MissingEnum = MISSING,
description: str | None | MissingEnum = MISSING,
premium_progress_bar_enabled: bool | MissingEnum = MISSING,
reason: str | None = None,
) -> Guild:
"""Edits the guild.
Parameters
----------
name: :class:`str`
The new name of the guild.
verification_level: :class:`VerificationLevel`
The new verification level of the guild.
default_message_notifications: :class:`DefaultMessageNotificationLevel`
The new default message notification level of the guild.
explicit_content_filter: :class:`ExplicitContentFilterLevel`
The new explicit content filter level of the guild.
afk_channel: :class:`VoiceChannel`
The new AFK channel of the guild.
afk_timeout: :class:`int`
The new AFK timeout of the guild.
icon: :class:`.File`
The new icon of the guild.
owner: :class:`User`
The new owner of the guild.
splash: :class:`.File`
The new splash of the guild.
discovery_splash: :class:`.File`
The new discovery splash of the guild.
banner: :class:`.File`
The new banner of the guild.
system_channel: :class:`TextChannel`
The new system channel of the guild.
rules_channel: :class:`TextChannel`
The new rules channel of the guild.
public_updates_channel: :class:`TextChannel`
The new public updates channel of the guild.
preferred_locale: :class:`str`
The new preferred locale of the guild.
features: list[:class:`GUILD_FEATURE`]
The new features of the guild.
description: :class:`str`
The new description of the guild.
reason: :class:`str` | None
The reason for editing the guild. Shows up on the audit log.
premium_progress_bar_enabled: :class:`bool`
Whether the premium progress bar is enabled.
Returns
-------
:class:`Guild`
The edited guild.
"""
data = await self._state.http.modify_guild(
self.id,
name=name,
verification_level=verification_level.value
if verification_level
else verification_level,
default_message_notifications=default_message_notifications.value
if default_message_notifications
else default_message_notifications,
explicit_content_filter=explicit_content_filter.value
if explicit_content_filter
else explicit_content_filter,
afk_channel_id=afk_channel.id if afk_channel else afk_channel,
afk_timeout=afk_timeout,
icon=icon,
owner_id=owner.id if owner else owner,
splash=splash,
discovery_splash=discovery_splash,
banner=banner,
system_channel_id=system_channel.id if system_channel else system_channel,
rules_channel_id=rules_channel.id if rules_channel else rules_channel,
public_updates_channel_id=public_updates_channel.id
if public_updates_channel
else public_updates_channel,
preferred_locale=preferred_locale,
features=features,
description=description,
premium_progress_bar_enabled=premium_progress_bar_enabled,
reason=reason,
)
return Guild(data, self._state)
async def delete(self) -> None:
"""Deletes the guild."""
await self._state.http.delete_guild(self.id)
async def get_channels(self) -> list[CHANNEL_TYPE]:
"""Gets the channels of the guild.
Returns
-------
list[:class:`Channel`]
The channels of the guild.
"""
data = await self._state.http.get_guild_channels(self.id)
return [identify_channel(channel, self._state) for channel in data]
async def create_channel(
self,
name: str,
type: ChannelType,
*,
topic: str | None | MissingEnum = MISSING,
bitrate: int | None | MissingEnum = MISSING,
user_limit: int | None | MissingEnum = MISSING,
rate_limit_per_user: int | None | MissingEnum = MISSING,
position: int | None | MissingEnum = MISSING,
permission_overwrites: list[_Overwrite] | None | MissingEnum = MISSING,
parent: CategoryChannel | None | MissingEnum = MISSING,
nsfw: bool | MissingEnum = MISSING,
rtc_region: str | None | MissingEnum = MISSING,
video_quality_mode: VideoQualityMode | None | MissingEnum = MISSING,
default_auto_archive_duration: int | None | MissingEnum = MISSING,
default_reaction_emoji: str | None | MissingEnum = MISSING,
available_tags: list[ForumTag] | None | MissingEnum = MISSING,
default_sort_order: SortOrderType | None | MissingEnum = MISSING,
reason: str | None = None,
) -> CHANNEL_TYPE:
"""Creates a channel in the guild.
Parameters
----------
name: :class:`str`
The name of the channel.
type: :class:`ChannelType`
The type of the channel.
topic: :class:`str` | None | :class:`.MissingEnum`
The topic of the channel.
bitrate: :class:`int` | None | :class:`.MissingEnum`
The bitrate of the channel.
user_limit: :class:`int` | None | :class:`.MissingEnum`
The user limit of the channel.
rate_limit_per_user: :class:`int` | None | :class:`.MissingEnum`
The rate limit per user of the channel.
position: :class:`int` | None | :class:`.MissingEnum`
The position of the channel.
permission_overwrites: list[:class:`_Overwrite`]
The permission overwrites of the channel.
parent: :class:`CategoryChannel` | None | :class:`.MissingEnum`
The parent of the channel.
nsfw: :class:`bool` | :class:`.MissingEnum`
Whether the channel is NSFW.
rtc_region: :class:`str` | None | :class:`.MissingEnum`
The RTC region of the channel.
video_quality_mode: :class:`VideoQualityMode` | None | :class:`.MissingEnum`
The video quality mode of the channel.
default_auto_archive_duration: :class:`int` | None | :class:`.MissingEnum`
The default auto archive duration of the channel.
default_reaction_emoji: :class:`str` | None | :class:`.MissingEnum`
The default reaction emoji of the channel.
available_tags: list[:class:`ForumTag`] | None | :class:`.MissingEnum`
The available tags of the channel.
default_sort_order: :class:`SortOrderType` | None | :class:`.MissingEnum`
The default sort order of the channel.
reason: :class:`str` | None
The reason for creating the channel. Shows up on the audit log.
Returns
-------
:class:`Channel`
The created channel.
"""
data = await self._state.http.create_channel(
self.id,
name=name,
type=type.value if type else type,
topic=topic,
bitrate=bitrate,
user_limit=user_limit,
rate_limit_per_user=rate_limit_per_user,
position=position,
permission_overwrites=[o.to_dict() for o in permission_overwrites],
parent_id=parent.id if parent else parent,
nsfw=nsfw,
rtc_region=rtc_region,
video_quality_mode=video_quality_mode,
default_auto_archive_duration=default_auto_archive_duration,
default_reaction_emoji=default_reaction_emoji,
available_tags=available_tags,
default_sort_order=default_sort_order,
reason=reason,
)
return identify_channel(data, self._state)
async def modify_channel_positions(self, channels: list[ChannelPosition]) -> None:
"""Modifies the positions of the channels.
Parameters
----------
channels: list[:class:`ChannelPosition`]
The channels to modify.
"""
await self._state.http.modify_channel_positions(
self.id, [c.to_dict() for c in channels]
)
async def list_active_threads(self) -> list[Thread | AnnouncementThread]:
"""Lists the active threads in the guild.
Returns
-------
list[:class:`Channel`]
The active threads in the guild.
"""
data = await self._state.http.list_active_threads(self.id)
return [identify_channel(channel, self._state) for channel in data]
async def get_member(self, id: Snowflake):
"""Gets a member from the guild.
Parameters
----------
id: :class:`Snowflake`
The ID of the member.
Returns
-------
:class:`Member`
The member.
"""
data = await self._state.http.get_member(self.id, id)
return Member(data, self._state, guild_id=self.id)
def list_members(
self, limit: int = None, after: datetime.datetime | None = None
) -> MemberPaginator:
"""Lists the members in the guild.
Parameters
----------
limit: :class:`int`
The maximum number of members to return.
after: :class:`datetime.datetime` | None
List only members whos accounts were created after this date.
Returns
-------
:class:`MemberPaginator`
An async iterator that can be used for iterating over the guild's members.
"""
return MemberPaginator(self._state, self.id, limit=limit, after=after)
async def search_members(
self,
query: str,
*,
limit: int = None,
) -> list[Member]:
"""Searches for members in the guild.
Parameters
----------
query: :class:`str`
The query to search for.
limit: :class:`int`
The maximum number of members to return.
Returns
-------
list[:class:`Member`]
The members.
"""
data = await self._state.http.search_guild_members(self.id, query, limit=limit)
return [Member(member, self._state, guild_id=self.id) for member in data]
async def add_member(
self,
id: Snowflake,
access_token: str,
*,
nick: str | None = None,
roles: list[Role] | None = None,
mute: bool = False,
deaf: bool = False,
) -> Member:
"""Adds a member to the guild through Oauth2.
Parameters
----------
id: :class:`Snowflake`
The ID of the member.
access_token: :class:`str`
The access token of the member.
nick: :class:`str` | None
The nickname of the member.
roles: list[:class:`Role`] | None
The roles of the member.
mute: :class:`bool`
Whether the member is muted.
deaf: :class:`bool`
Whether the member is deafened.
Returns
-------
:class:`Member`
The member.
"""
nick = nick or MISSING
roles = roles or []
data = await self._state.http.add_member(
self.id,
id,
access_token,
nick=nick,
roles=[role.id for role in roles],
mute=mute,
deaf=deaf,
)
return Member(data, self._state, guild_id=self.id)
async def edit_own_member(
self, *, nick: str | None | MissingEnum = MISSING, reason: str | None = None
) -> Member:
"""Edits the bot's guild member.
Parameters
----------
nick: :class:`str` | None | :class:`MissingEnum`
The bot's new nickname.
reason: :class:`str` | None
The reasoning for editing the bot. Shows up in the audit log.
Returns
-------
:class:`Member`
The updated member.
"""
data = await self._state.http.modify_current_member(
self.id, nick=nick, reason=reason
)
return Member(data, self._state, guild_id=self.id)
def get_bans(
self,
*,
limit: int | None = 1000,
before: datetime.datetime | None = None,
after: datetime.datetime | None = None,
) -> BanPaginator:
"""Lists the bans in the guild.
.. note::
If both ``after`` and ``before`` parameters are provided,
only ``before`` will be respected.
Parameters
----------
limit: :class:`int`
The maximum number of bans to return.
before: :class:`datetime.datetime` | None
List only bans related to users whos accounts
were created before this date.
This is not related to the ban's creation date.
after: :class:`datetime.datetime` | None
List only bans related to users whos accounts
were created after this date.
This is not related to the ban's creation date.
Returns
-------
:class:`BanPaginator`
An async iterator that can be used for iterating over the guild's bans.
"""
return BanPaginator(
self._state, self.id, limit=limit, before=before, after=after
)
async def get_ban(
self,
user_id: Snowflake,
) -> Ban:
"""Gets a ban for a user.
Parameters
----------
user_id: :class:`Snowflake`
The user ID to fetch a ban for.
Returns
-------
:class:`Ban`
The user's ban.
"""
data = await self._state.http.get_guild_ban(self.id, user_id)
return Ban(data, self._state)
async def ban(
self,
user: User,
*,
delete_message_seconds: int | MissingEnum = MISSING,
reason: str | None = None,
) -> None:
"""Bans a user.
Parameters
----------
user: :class:`User`
The user to ban
delete_message_seconds: :class:`int` | MissingEnum
The amount of seconds worth of messages that should be deleted.
reason: :class:`str` | None
The reason for the ban. Shows up in the audit log, and when the ban is fetched.
"""
await self._state.http.create_guild_ban(
self.id,
user.id,
delete_message_seconds=delete_message_seconds,
reason=reason,
)
async def unban(self, user: User, *, reason: str | None = None) -> None:
"""Unbans a user.
Parameters
----------
user: :class:`User`
The user to unban
reason: :class:`str` | None
The reason for the unban. Shows up in the audit log.
"""
await self._state.http.remove_guild_ban(self.id, user.id, reason=reason)
async def get_scheduled_events(self, with_user_count: bool) -> list[ScheduledEvent]:
"""
Get the scheduled events in this guild.
Parameters
----------
with_user_count: :class:`bool`
include number of users subscribed to each event
Returns
-------
:class:`list`[:class:`.ScheduledEvent`]
"""
scheds = await self._state.http.list_scheduled_events(
self.id, with_user_count=with_user_count
)
return [ScheduledEvent(s, self._state) for s in scheds]
class GuildPreview:
def __init__(self, data: DiscordGuildPreview, state: State) -> None:
self.id: Snowflake = Snowflake(data['id'])
self.name: str = data['name']
# TODO: Asset classes
self._icon: str | None = data['icon']
self._splash: str | None = data['splash']
self._discovery_splash: str | None = data['discovery_splash']
self.emojis: list[Emoji] = [Emoji(emoji, state) for emoji in data['emojis']]
self.features: list[GUILD_FEATURE] = data['features']
self.approximate_member_count: int = data['approximate_member_count']
self.approximate_presence_count: int = data['approximate_presence_count']
self.description: str | None = data['description']
self.stickers: list[Sticker] = [
Sticker(sticker, state) for sticker in data['stickers']
]
class WidgetSettings:
def __init__(self, data: DiscordWidgetSettings) -> None:
self.enabled: bool = data['enabled']
self.channel_id: Snowflake | None = (
Snowflake(data['channel_id']) if data['channel_id'] is not None else None
)
class Widget:
def __init__(self, data: DiscordWidget, state: State) -> None:
self.id: Snowflake = Snowflake(data['id'])
self.name: str = data['name']
self.instant_invite: str | None = data['instant_invite']
self.channels: list[Channel] = [
Channel(channel, state) for channel in data['channels']
]
self.members: list[User] = [User(user, state) for user in data['members']]
class Ban:
def __init__(self, data: DiscordBan, state: State) -> None:
self.user: User = User(data['user'], state)
self.reason: str | None = data['reason']
class BanPage(Page[Ban]):
def __init__(self, ban: Ban) -> None:
self.value = ban
class BanPaginator(Paginator[BanPage]):
def __init__(
self,
state: State,
guild_id: Snowflake,
*,
limit: int = 1,
before: datetime.datetime | None = None,
after: datetime.datetime | None = None,
) -> None:
super().__init__()
self._state: State = state
self.guild_id: Snowflake = guild_id
self.limit: int | None = limit
self.reverse_order: bool = False
self.last_id: Snowflake | MissingEnum
if before:
self.last_id = Snowflake.from_datetime(before)
self.reverse_order = True
elif after:
self.last_id = Snowflake.from_datetime(after)
else:
self.last_id = MISSING
self.done = False
async def fill(self):
if self._previous_page is None or self._previous_page[0] >= len(self._pages):