-
Notifications
You must be signed in to change notification settings - Fork 134
/
app_commands.py
1102 lines (900 loc) · 37.6 KB
/
app_commands.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
# SPDX-License-Identifier: MIT
from __future__ import annotations
import math
import re
from abc import ABC
from typing import TYPE_CHECKING, ClassVar, List, Mapping, Optional, Sequence, Tuple, Union
from .enums import (
ApplicationCommandPermissionType,
ApplicationCommandType,
ChannelType,
Locale,
OptionType,
enum_if_int,
try_enum,
try_enum_to_int,
)
from .i18n import Localized
from .permissions import Permissions
from .utils import MISSING, _get_as_snowflake, _maybe_cast
if TYPE_CHECKING:
from typing_extensions import Self
from .i18n import LocalizationProtocol, LocalizationValue, LocalizedOptional, LocalizedRequired
from .state import ConnectionState
from .types.interactions import (
ApplicationCommand as ApplicationCommandPayload,
ApplicationCommandOption as ApplicationCommandOptionPayload,
ApplicationCommandOptionChoice as ApplicationCommandOptionChoicePayload,
ApplicationCommandOptionChoiceValue,
ApplicationCommandPermissions as ApplicationCommandPermissionsPayload,
EditApplicationCommand as EditApplicationCommandPayload,
GuildApplicationCommandPermissions as GuildApplicationCommandPermissionsPayload,
)
Choices = Union[
Sequence["OptionChoice"],
Sequence[ApplicationCommandOptionChoiceValue],
Mapping[str, ApplicationCommandOptionChoiceValue],
Sequence[Localized[str]],
]
APIApplicationCommand = Union["APIUserCommand", "APIMessageCommand", "APISlashCommand"]
__all__ = (
"application_command_factory",
"ApplicationCommand",
"SlashCommand",
"APISlashCommand",
"UserCommand",
"APIUserCommand",
"MessageCommand",
"APIMessageCommand",
"OptionChoice",
"Option",
"ApplicationCommandPermissions",
"GuildApplicationCommandPermissions",
)
def application_command_factory(data: ApplicationCommandPayload) -> APIApplicationCommand:
cmd_type = try_enum(ApplicationCommandType, data.get("type", 1))
if cmd_type is ApplicationCommandType.chat_input:
return APISlashCommand.from_dict(data)
if cmd_type is ApplicationCommandType.user:
return APIUserCommand.from_dict(data)
if cmd_type is ApplicationCommandType.message:
return APIMessageCommand.from_dict(data)
raise TypeError(f"Application command of type {cmd_type} is not valid")
def _validate_name(name: str) -> None:
# used for slash command names and option names
# see https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-naming
if not isinstance(name, str):
raise TypeError(
f"Slash command name and option names must be an instance of class 'str', received '{name.__class__}'"
)
if name != name.lower() or not re.fullmatch(r"[\w-]{1,32}", name):
raise ValueError(
f"Slash command or option name '{name}' should be lowercase, "
"between 1 and 32 characters long, and only consist of "
"these symbols: a-z, 0-9, -, _, and other languages'/scripts' symbols"
)
class OptionChoice:
"""Represents an option choice.
Parameters
----------
name: Union[:class:`str`, :class:`.Localized`]
The name of the option choice (visible to users).
.. versionchanged:: 2.5
Added support for localizations.
value: Union[:class:`str`, :class:`int`]
The value of the option choice.
"""
def __init__(
self,
name: LocalizedRequired,
value: ApplicationCommandOptionChoiceValue,
) -> None:
name_loc = Localized._cast(name, True)
self.name: str = name_loc.string
self.name_localizations: LocalizationValue = name_loc.localizations
self.value: ApplicationCommandOptionChoiceValue = value
def __repr__(self) -> str:
return f"<OptionChoice name={self.name!r} value={self.value!r}>"
def __eq__(self, other) -> bool:
return (
self.name == other.name
and self.value == other.value
and self.name_localizations == other.name_localizations
)
def to_dict(self, *, locale: Optional[Locale] = None) -> ApplicationCommandOptionChoicePayload:
localizations = self.name_localizations.data
name: Optional[str] = None
# if `locale` provided, get localized name from dict
if locale is not None and localizations:
name = localizations.get(str(locale))
# fall back to default name if no locale or no localized name
if name is None:
name = self.name
payload: ApplicationCommandOptionChoicePayload = {
"name": name,
"value": self.value,
}
# if no `locale` provided, include all localizations in payload
if locale is None and localizations:
payload["name_localizations"] = localizations
return payload
@classmethod
def from_dict(cls, data: ApplicationCommandOptionChoicePayload):
return OptionChoice(
name=Localized(data["name"], data=data.get("name_localizations")),
value=data["value"],
)
def localize(self, store: LocalizationProtocol) -> None:
self.name_localizations._link(store)
class Option:
"""Represents a slash command option.
Parameters
----------
name: Union[:class:`str`, :class:`.Localized`]
The option's name.
.. versionchanged:: 2.5
Added support for localizations.
description: Optional[Union[:class:`str`, :class:`.Localized`]]
The option's description.
.. versionchanged:: 2.5
Added support for localizations.
type: :class:`OptionType`
The option type, e.g. :class:`OptionType.user`.
required: :class:`bool`
Whether this option is required.
choices: Union[Sequence[:class:`OptionChoice`], Sequence[Union[:class:`str`, :class:`int`, :class:`float`]], Mapping[:class:`str`, Union[:class:`str`, :class:`int`, :class:`float`]]]
The pre-defined choices for this option.
options: List[:class:`Option`]
The list of sub options. Normally you don't have to specify it directly,
instead consider using ``@main_cmd.sub_command`` or ``@main_cmd.sub_command_group`` decorators.
channel_types: List[:class:`ChannelType`]
The list of channel types that your option supports, if the type is :class:`OptionType.channel`.
By default, it supports all channel types.
autocomplete: :class:`bool`
Whether this option can be autocompleted.
min_value: Union[:class:`int`, :class:`float`]
The minimum value permitted.
max_value: Union[:class:`int`, :class:`float`]
The maximum value permitted.
min_length: :class:`int`
The minimum length for this option if this is a string option.
.. versionadded:: 2.6
max_length: :class:`int`
The maximum length for this option if this is a string option.
.. versionadded:: 2.6
Attributes
----------
name: :class:`str`
The option's name.
description: :class:`str`
The option's description.
type: :class:`OptionType`
The option type, e.g. :class:`OptionType.user`.
required: :class:`bool`
Whether this option is required.
choices: List[:class:`OptionChoice`]
The list of pre-defined choices.
options: List[:class:`Option`]
The list of sub options. Normally you don't have to specify it directly,
instead consider using ``@main_cmd.sub_command`` or ``@main_cmd.sub_command_group`` decorators.
channel_types: List[:class:`ChannelType`]
The list of channel types that your option supports, if the type is :class:`OptionType.channel`.
By default, it supports all channel types.
autocomplete: :class:`bool`
Whether this option can be autocompleted.
min_value: Union[:class:`int`, :class:`float`]
The minimum value permitted.
max_value: Union[:class:`int`, :class:`float`]
The maximum value permitted.
min_length: :class:`int`
The minimum length for this option if this is a string option.
.. versionadded:: 2.6
max_length: :class:`int`
The maximum length for this option if this is a string option.
.. versionadded:: 2.6
"""
__slots__ = (
"name",
"description",
"type",
"required",
"choices",
"options",
"channel_types",
"autocomplete",
"min_value",
"max_value",
"name_localizations",
"description_localizations",
"min_length",
"max_length",
)
def __init__(
self,
name: LocalizedRequired,
description: LocalizedOptional = None,
type: Optional[Union[OptionType, int]] = None,
required: bool = False,
choices: Optional[Choices] = None,
options: Optional[list] = None,
channel_types: Optional[List[ChannelType]] = None,
autocomplete: bool = False,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
) -> None:
name_loc = Localized._cast(name, True)
_validate_name(name_loc.string)
self.name: str = name_loc.string
self.name_localizations: LocalizationValue = name_loc.localizations
desc_loc = Localized._cast(description, False)
self.description: str = desc_loc.string or "-"
self.description_localizations: LocalizationValue = desc_loc.localizations
self.type: OptionType = enum_if_int(OptionType, type) or OptionType.string
self.required: bool = required
self.options: List[Option] = options or []
if min_value and self.type is OptionType.integer:
min_value = math.ceil(min_value)
if max_value and self.type is OptionType.integer:
max_value = math.floor(max_value)
self.min_value: Optional[float] = min_value
self.max_value: Optional[float] = max_value
self.min_length: Optional[int] = min_length
self.max_length: Optional[int] = max_length
if channel_types is not None and not all(isinstance(t, ChannelType) for t in channel_types):
raise TypeError("channel_types must be a list of `ChannelType`s")
self.channel_types: List[ChannelType] = channel_types or []
self.choices: List[OptionChoice] = []
if choices is not None:
if autocomplete:
raise TypeError("can not specify both choices and autocomplete args")
if isinstance(choices, str): # str matches `Sequence[str]`, but isn't meant to be used
raise TypeError("choices argument should be a list/sequence or dict, not str")
if isinstance(choices, Mapping):
self.choices = [OptionChoice(name, value) for name, value in choices.items()]
else:
for c in choices:
if isinstance(c, Localized):
c = OptionChoice(c, c.string)
elif not isinstance(c, OptionChoice):
c = OptionChoice(str(c), c)
self.choices.append(c)
self.autocomplete: bool = autocomplete
def __repr__(self) -> str:
return (
f"<Option name={self.name!r} description={self.description!r}"
f" type={self.type!r} required={self.required!r} choices={self.choices!r}"
f" options={self.options!r} min_value={self.min_value!r} max_value={self.max_value!r}"
f" min_length={self.min_length!r} max_length={self.max_length!r}>"
)
def __eq__(self, other) -> bool:
return (
self.name == other.name
and self.description == other.description
and self.type == other.type
and self.required == other.required
and self.choices == other.choices
and self.options == other.options
and set(self.channel_types) == set(other.channel_types)
and self.autocomplete == other.autocomplete
and self.min_value == other.min_value
and self.max_value == other.max_value
and self.min_length == other.min_length
and self.max_length == other.max_length
and self.name_localizations == other.name_localizations
and self.description_localizations == other.description_localizations
)
@classmethod
def from_dict(cls, data: ApplicationCommandOptionPayload) -> Option:
return Option(
name=Localized(data["name"], data=data.get("name_localizations")),
description=Localized(
data.get("description"), data=data.get("description_localizations")
),
type=data.get("type"),
required=data.get("required", False),
choices=_maybe_cast(
data.get("choices", MISSING), lambda x: list(map(OptionChoice.from_dict, x))
),
options=_maybe_cast(
data.get("options", MISSING), lambda x: list(map(Option.from_dict, x))
),
channel_types=_maybe_cast(
data.get("channel_types", MISSING), lambda x: [try_enum(ChannelType, t) for t in x]
),
autocomplete=data.get("autocomplete", False),
min_value=data.get("min_value"),
max_value=data.get("max_value"),
min_length=data.get("min_length"),
max_length=data.get("max_length"),
)
def add_choice(
self,
name: LocalizedRequired,
value: ApplicationCommandOptionChoiceValue,
) -> None:
"""Adds an OptionChoice to the list of current choices,
parameters are the same as for :class:`OptionChoice`.
"""
self.choices.append(
OptionChoice(
name=name,
value=value,
)
)
def add_option(
self,
name: LocalizedRequired,
description: LocalizedOptional = None,
type: Optional[OptionType] = None,
required: bool = False,
choices: Optional[Choices] = None,
options: Optional[list] = None,
channel_types: Optional[List[ChannelType]] = None,
autocomplete: bool = False,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
) -> None:
"""Adds an option to the current list of options,
parameters are the same as for :class:`Option`.
"""
type = type or OptionType.string
self.options.append(
Option(
name=name,
description=description,
type=type,
required=required,
choices=choices,
options=options,
channel_types=channel_types,
autocomplete=autocomplete,
min_value=min_value,
max_value=max_value,
min_length=min_length,
max_length=max_length,
)
)
def to_dict(self) -> ApplicationCommandOptionPayload:
payload: ApplicationCommandOptionPayload = {
"name": self.name,
"description": self.description,
"type": try_enum_to_int(self.type),
}
if self.required:
payload["required"] = True
if self.autocomplete:
payload["autocomplete"] = True
if self.choices:
payload["choices"] = [c.to_dict() for c in self.choices]
if self.options:
payload["options"] = [o.to_dict() for o in self.options]
if self.channel_types:
payload["channel_types"] = [v.value for v in self.channel_types]
if self.min_value is not None:
payload["min_value"] = self.min_value
if self.max_value is not None:
payload["max_value"] = self.max_value
if self.min_length is not None:
payload["min_length"] = self.min_length
if self.max_length is not None:
payload["max_length"] = self.max_length
if (loc := self.name_localizations.data) is not None:
payload["name_localizations"] = loc
if (loc := self.description_localizations.data) is not None:
payload["description_localizations"] = loc
return payload
def localize(self, store: LocalizationProtocol) -> None:
self.name_localizations._link(store)
self.description_localizations._link(store)
if (name_loc := self.name_localizations.data) is not None:
for value in name_loc.values():
_validate_name(value)
for c in self.choices:
c.localize(store)
for o in self.options:
o.localize(store)
class ApplicationCommand(ABC):
"""The base class for application commands.
The following classes implement this ABC:
- :class:`~.SlashCommand`
- :class:`~.MessageCommand`
- :class:`~.UserCommand`
Attributes
----------
type: :class:`ApplicationCommandType`
The command type
name: :class:`str`
The command name
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
Defaults to ``True``.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
Defaults to ``False``.
.. versionadded:: 2.8
"""
__repr_info__: ClassVar[Tuple[str, ...]] = (
"type",
"name",
"dm_permission",
"default_member_permisions",
"nsfw",
)
def __init__(
self,
type: ApplicationCommandType,
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
nsfw: Optional[bool] = None,
) -> None:
self.type: ApplicationCommandType = enum_if_int(ApplicationCommandType, type)
name_loc = Localized._cast(name, True)
self.name: str = name_loc.string
self.name_localizations: LocalizationValue = name_loc.localizations
self.nsfw: bool = False if nsfw is None else nsfw
self.dm_permission: bool = True if dm_permission is None else dm_permission
self._default_member_permissions: Optional[int]
if default_member_permissions is None:
# allow everyone to use the command if its not supplied
self._default_member_permissions = None
elif isinstance(default_member_permissions, bool):
raise TypeError("`default_member_permissions` cannot be a bool")
elif isinstance(default_member_permissions, int):
self._default_member_permissions = default_member_permissions
else:
self._default_member_permissions = default_member_permissions.value
self._always_synced: bool = False
# reset `default_permission` if set before
self._default_permission: bool = True
@property
def default_member_permissions(self) -> Optional[Permissions]:
"""Optional[:class:`Permissions`]: The default required member permissions for this command.
A member must have *all* these permissions to be able to invoke the command in a guild.
This is a default value, the set of users/roles that may invoke this command can be
overridden by moderators on a guild-specific basis, disregarding this setting.
If ``None`` is returned, it means everyone can use the command by default.
If an empty :class:`Permissions` object is returned (that is, all permissions set to ``False``),
this means no one can use the command.
.. versionadded:: 2.5
"""
if self._default_member_permissions is None:
return None
return Permissions(self._default_member_permissions)
def __repr__(self) -> str:
attrs = " ".join(f"{key}={getattr(self, key)!r}" for key in self.__repr_info__)
return f"<{type(self).__name__} {attrs}>"
def __str__(self) -> str:
return self.name
def __eq__(self, other) -> bool:
return (
self.type == other.type
and self.name == other.name
and self.name_localizations == other.name_localizations
and self.nsfw == other.nsfw
and self._default_member_permissions == other._default_member_permissions
# ignore `dm_permission` if comparing guild commands
and (
any(
(isinstance(obj, _APIApplicationCommandMixin) and obj.guild_id)
for obj in (self, other)
)
or self.dm_permission == other.dm_permission
)
and self._default_permission == other._default_permission
)
def to_dict(self) -> EditApplicationCommandPayload:
data: EditApplicationCommandPayload = {
"type": try_enum_to_int(self.type),
"name": self.name,
"dm_permission": self.dm_permission,
"default_permission": True,
"nsfw": self.nsfw,
}
if self._default_member_permissions is None:
data["default_member_permissions"] = None
else:
data["default_member_permissions"] = str(self._default_member_permissions)
if (loc := self.name_localizations.data) is not None:
data["name_localizations"] = loc
return data
def localize(self, store: LocalizationProtocol) -> None:
self.name_localizations._link(store)
class _APIApplicationCommandMixin:
__repr_info__ = ("id",)
def _update_common(self, data: ApplicationCommandPayload) -> None:
self.id: int = int(data["id"])
self.application_id: int = int(data["application_id"])
self.guild_id: Optional[int] = _get_as_snowflake(data, "guild_id")
self.version: int = int(data["version"])
# deprecated, but kept until API stops returning this field
self._default_permission = data.get("default_permission") is not False
class UserCommand(ApplicationCommand):
"""A user context menu command.
Attributes
----------
name: :class:`str`
The user command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
Defaults to ``True``.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
Defaults to ``False``.
.. versionadded:: 2.8
"""
__repr_info__ = ("name", "dm_permission", "default_member_permissions")
def __init__(
self,
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
nsfw: Optional[bool] = None,
) -> None:
super().__init__(
type=ApplicationCommandType.user,
name=name,
dm_permission=dm_permission,
default_member_permissions=default_member_permissions,
nsfw=nsfw,
)
class APIUserCommand(UserCommand, _APIApplicationCommandMixin):
"""A user context menu command returned by the API.
.. versionadded:: 2.4
Attributes
----------
name: :class:`str`
The user command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
.. versionadded:: 2.8
id: :class:`int`
The user command's ID.
application_id: :class:`int`
The application ID this command belongs to.
guild_id: Optional[:class:`int`]
The ID of the guild this user command is enabled in, or ``None`` if it's global.
version: :class:`int`
Autoincrementing version identifier updated during substantial record changes.
"""
__repr_info__ = UserCommand.__repr_info__ + _APIApplicationCommandMixin.__repr_info__
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.user.value:
raise ValueError(f"Invalid payload type for UserCommand: {cmd_type}")
self = cls(
name=Localized(data["name"], data=data.get("name_localizations")),
dm_permission=data.get("dm_permission") is not False,
default_member_permissions=_get_as_snowflake(data, "default_member_permissions"),
nsfw=data.get("nsfw"),
)
self._update_common(data)
return self
class MessageCommand(ApplicationCommand):
"""A message context menu command
Attributes
----------
name: :class:`str`
The message command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
Defaults to ``True``.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
Defaults to ``False``.
.. versionadded:: 2.8
"""
__repr_info__ = ("name", "dm_permission", "default_member_permissions")
def __init__(
self,
name: LocalizedRequired,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
nsfw: Optional[bool] = None,
) -> None:
super().__init__(
type=ApplicationCommandType.message,
name=name,
dm_permission=dm_permission,
default_member_permissions=default_member_permissions,
nsfw=nsfw,
)
class APIMessageCommand(MessageCommand, _APIApplicationCommandMixin):
"""A message context menu command returned by the API.
.. versionadded:: 2.4
Attributes
----------
name: :class:`str`
The message command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
.. versionadded:: 2.8
id: :class:`int`
The message command's ID.
application_id: :class:`int`
The application ID this command belongs to.
guild_id: Optional[:class:`int`]
The ID of the guild this message command is enabled in, or ``None`` if it's global.
version: :class:`int`
Autoincrementing version identifier updated during substantial record changes.
"""
__repr_info__ = MessageCommand.__repr_info__ + _APIApplicationCommandMixin.__repr_info__
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.message.value:
raise ValueError(f"Invalid payload type for MessageCommand: {cmd_type}")
self = cls(
name=Localized(data["name"], data=data.get("name_localizations")),
dm_permission=data.get("dm_permission") is not False,
default_member_permissions=_get_as_snowflake(data, "default_member_permissions"),
nsfw=data.get("nsfw"),
)
self._update_common(data)
return self
class SlashCommand(ApplicationCommand):
"""The base class for building slash commands.
Attributes
----------
name: :class:`str`
The slash command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
description: :class:`str`
The slash command's description.
description_localizations: :class:`.LocalizationValue`
Localizations for ``description``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
Defaults to ``True``.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
Defaults to ``False``.
.. versionadded:: 2.8
options: List[:class:`Option`]
The list of options the slash command has.
"""
__repr_info__ = (
"name",
"description",
"options",
"dm_permission",
"default_member_permissions",
)
def __init__(
self,
name: LocalizedRequired,
description: LocalizedRequired,
options: Optional[List[Option]] = None,
dm_permission: Optional[bool] = None,
default_member_permissions: Optional[Union[Permissions, int]] = None,
nsfw: Optional[bool] = None,
) -> None:
super().__init__(
type=ApplicationCommandType.chat_input,
name=name,
dm_permission=dm_permission,
default_member_permissions=default_member_permissions,
nsfw=nsfw,
)
_validate_name(self.name)
desc_loc = Localized._cast(description, True)
self.description: str = desc_loc.string
self.description_localizations: LocalizationValue = desc_loc.localizations
self.options: List[Option] = options or []
def __eq__(self, other) -> bool:
return (
super().__eq__(other)
and self.description == other.description
and self.options == other.options
and self.description_localizations == other.description_localizations
)
def add_option(
self,
name: LocalizedRequired,
description: LocalizedOptional = None,
type: Optional[OptionType] = None,
required: bool = False,
choices: Optional[Choices] = None,
options: Optional[list] = None,
channel_types: Optional[List[ChannelType]] = None,
autocomplete: bool = False,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
) -> None:
"""Adds an option to the current list of options,
parameters are the same as for :class:`Option`
"""
self.options.append(
Option(
name=name,
description=description,
type=type or OptionType.string,
required=required,
choices=choices,
options=options,
channel_types=channel_types,
autocomplete=autocomplete,
min_value=min_value,
max_value=max_value,
min_length=min_length,
max_length=max_length,
)
)
def to_dict(self) -> EditApplicationCommandPayload:
res = super().to_dict()
res["description"] = self.description
res["options"] = [o.to_dict() for o in self.options]
if (loc := self.description_localizations.data) is not None:
res["description_localizations"] = loc
return res
def localize(self, store: LocalizationProtocol) -> None:
super().localize(store)
if (name_loc := self.name_localizations.data) is not None:
for value in name_loc.values():
_validate_name(value)
self.description_localizations._link(store)
for o in self.options:
o.localize(store)
class APISlashCommand(SlashCommand, _APIApplicationCommandMixin):
"""A slash command returned by the API.
.. versionadded:: 2.4
Attributes
----------
name: :class:`str`
The slash command's name.
name_localizations: :class:`.LocalizationValue`
Localizations for ``name``.
.. versionadded:: 2.5
description: :class:`str`
The slash command's description.
description_localizations: :class:`.LocalizationValue`
Localizations for ``description``.
.. versionadded:: 2.5
dm_permission: :class:`bool`
Whether this command can be used in DMs.
.. versionadded:: 2.5
nsfw: :class:`bool`
Whether this command is :ddocs:`age-restricted <interactions/application-commands#agerestricted-commands>`.
.. versionadded:: 2.8
id: :class:`int`
The slash command's ID.
options: List[:class:`Option`]
The list of options the slash command has.
application_id: :class:`int`
The application ID this command belongs to.
guild_id: Optional[:class:`int`]
The ID of the guild this slash command is enabled in, or ``None`` if it's global.
version: :class:`int`
Autoincrementing version identifier updated during substantial record changes.
"""
__repr_info__ = SlashCommand.__repr_info__ + _APIApplicationCommandMixin.__repr_info__
@classmethod
def from_dict(cls, data: ApplicationCommandPayload) -> Self:
cmd_type = data.get("type", 0)
if cmd_type != ApplicationCommandType.chat_input.value:
raise ValueError(f"Invalid payload type for SlashCommand: {cmd_type}")
self = cls(
name=Localized(data["name"], data=data.get("name_localizations")),
description=Localized(data["description"], data=data.get("description_localizations")),
options=_maybe_cast(
data.get("options", MISSING), lambda x: list(map(Option.from_dict, x))
),
dm_permission=data.get("dm_permission") is not False,
default_member_permissions=_get_as_snowflake(data, "default_member_permissions"),
nsfw=data.get("nsfw"),
)
self._update_common(data)