Skip to content

Commit ac5c055

Browse files
feat!: Add command enhancements (#1286)
* feat!: Add command's enhancements * fix: incorrect type * chore: bruh * ci: correct from checks. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ef29882 commit ac5c055

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

interactions/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
ComponentContext,
102102
ComponentType,
103103
context_menu,
104+
user_context_menu,
105+
message_context_menu,
104106
ContextMenu,
105107
ContextMenuContext,
106108
Converter,
@@ -402,6 +404,8 @@
402404
"ComponentType",
403405
"const",
404406
"context_menu",
407+
"user_context_menu",
408+
"message_context_menu",
405409
"CONTEXT_MENU_NAME_LENGTH",
406410
"ContextMenu",
407411
"ContextMenuContext",

interactions/models/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
ComponentCommand,
204204
ComponentContext,
205205
context_menu,
206+
user_context_menu,
207+
message_context_menu,
206208
ContextMenu,
207209
ContextMenuContext,
208210
Converter,
@@ -351,6 +353,8 @@
351353
"ComponentContext",
352354
"ComponentType",
353355
"context_menu",
356+
"user_context_menu",
357+
"message_context_menu",
354358
"ContextMenu",
355359
"ContextMenuContext",
356360
"Converter",

interactions/models/internal/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
component_callback,
1919
ComponentCommand,
2020
context_menu,
21+
user_context_menu,
22+
message_context_menu,
2123
ContextMenu,
2224
global_autocomplete,
2325
GlobalAutoComplete,
@@ -111,6 +113,8 @@
111113
"ComponentCommand",
112114
"ComponentContext",
113115
"context_menu",
116+
"user_context_menu",
117+
"message_context_menu",
114118
"ContextMenu",
115119
"ContextMenuContext",
116120
"Converter",

interactions/models/internal/application_commands.py

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"component_callback",
5959
"ComponentCommand",
6060
"context_menu",
61+
"user_context_menu",
62+
"message_context_menu",
6163
"ContextMenu",
6264
"global_autocomplete",
6365
"GlobalAutoComplete",
@@ -725,7 +727,7 @@ def group(
725727

726728
def subcommand(
727729
self,
728-
sub_cmd_name: LocalisedName | str,
730+
sub_cmd_name: Absent[LocalisedName | str] = MISSING,
729731
group_name: LocalisedName | str = None,
730732
sub_cmd_description: Absent[LocalisedDesc | str] = MISSING,
731733
group_description: Absent[LocalisedDesc | str] = MISSING,
@@ -734,13 +736,15 @@ def subcommand(
734736
inherit_checks: bool = True,
735737
) -> Callable[..., "SlashCommand"]:
736738
def wrapper(call: Callable[..., Coroutine]) -> "SlashCommand":
737-
nonlocal sub_cmd_description
739+
nonlocal sub_cmd_name, sub_cmd_description
738740

739741
if not asyncio.iscoroutinefunction(call):
740742
raise TypeError("Subcommand must be coroutine")
741743

742744
if sub_cmd_description is MISSING:
743745
sub_cmd_description = call.__doc__ or "No Description Set"
746+
if sub_cmd_name is MISSING:
747+
sub_cmd_name = call.__name__
744748

745749
return SlashCommand(
746750
name=self.name,
@@ -863,7 +867,7 @@ def decorator(func: Callable) -> GlobalAutoComplete:
863867

864868

865869
def slash_command(
866-
name: str | LocalisedName,
870+
name: Absent[str | LocalisedName] = MISSING,
867871
*,
868872
description: Absent[str | LocalisedDesc] = MISSING,
869873
scopes: Absent[List["Snowflake_Type"]] = MISSING,
@@ -885,7 +889,7 @@ def slash_command(
885889
one of the future ui updates.
886890
887891
Args:
888-
name: 1-32 character name of the command
892+
name: 1-32 character name of the command, defaults to the name of the coroutine.
889893
description: 1-100 character description of the command
890894
scopes: The scope this command exists within
891895
options: The parameters for the command, max 25
@@ -913,12 +917,16 @@ def wrapper(func: AsyncCallable) -> SlashCommand:
913917
else:
914918
perm = func.default_member_permissions
915919

920+
_name = name
921+
if _name is MISSING:
922+
_name = func.__name__
923+
916924
_description = description
917925
if _description is MISSING:
918926
_description = func.__doc__ or "No Description Set"
919927

920928
cmd = SlashCommand(
921-
name=name,
929+
name=_name,
922930
group_name=group_name,
923931
group_description=group_description,
924932
sub_cmd_name=sub_cmd_name,
@@ -941,7 +949,7 @@ def subcommand(
941949
base: str | LocalisedName,
942950
*,
943951
subcommand_group: Optional[str | LocalisedName] = None,
944-
name: Optional[str | LocalisedName] = None,
952+
name: Absent[str | LocalisedName] = MISSING,
945953
description: Absent[str | LocalisedDesc] = MISSING,
946954
base_description: Optional[str | LocalisedDesc] = None,
947955
base_desc: Optional[str | LocalisedDesc] = None,
@@ -980,6 +988,10 @@ def wrapper(func: AsyncCallable) -> SlashCommand:
980988
if not asyncio.iscoroutinefunction(func):
981989
raise ValueError("Commands must be coroutines")
982990

991+
_name = name
992+
if _name is MISSING:
993+
_name = func.__name__
994+
983995
_description = description
984996
if _description is MISSING:
985997
_description = func.__doc__ or "No Description Set"
@@ -989,7 +1001,7 @@ def wrapper(func: AsyncCallable) -> SlashCommand:
9891001
description=(base_description or base_desc) or "No Description Set",
9901002
group_name=subcommand_group,
9911003
group_description=(subcommand_group_description or sub_group_desc) or "No Description Set",
992-
sub_cmd_name=name,
1004+
sub_cmd_name=_name,
9931005
sub_cmd_description=_description,
9941006
default_member_permissions=base_default_member_permissions,
9951007
dm_permission=base_dm_permission,
@@ -1004,7 +1016,8 @@ def wrapper(func: AsyncCallable) -> SlashCommand:
10041016

10051017

10061018
def context_menu(
1007-
name: str | LocalisedName,
1019+
name: Absent[str | LocalisedName] = MISSING,
1020+
*,
10081021
context_type: "CommandType",
10091022
scopes: Absent[List["Snowflake_Type"]] = MISSING,
10101023
default_member_permissions: Optional["Permissions"] = None,
@@ -1014,7 +1027,7 @@ def context_menu(
10141027
A decorator to declare a coroutine as a Context Menu.
10151028
10161029
Args:
1017-
name: 1-32 character name of the context menu
1030+
name: 1-32 character name of the context menu, defaults to the name of the coroutine.
10181031
context_type: The type of context menu
10191032
scopes: The scope this command exists within
10201033
default_member_permissions: What permissions members need to have by default to use this command.
@@ -1036,8 +1049,12 @@ def wrapper(func: AsyncCallable) -> ContextMenu:
10361049
else:
10371050
perm = func.default_member_permissions
10381051

1052+
_name = name
1053+
if _name is MISSING:
1054+
_name = func.__name__
1055+
10391056
cmd = ContextMenu(
1040-
name=name,
1057+
name=_name,
10411058
type=context_type,
10421059
scopes=scopes or [GLOBAL_SCOPE],
10431060
default_member_permissions=perm,
@@ -1049,6 +1066,64 @@ def wrapper(func: AsyncCallable) -> ContextMenu:
10491066
return wrapper
10501067

10511068

1069+
def user_context_menu(
1070+
name: Absent[str | LocalisedName] = MISSING,
1071+
*,
1072+
scopes: Absent[List["Snowflake_Type"]] = MISSING,
1073+
default_member_permissions: Optional["Permissions"] = None,
1074+
dm_permission: bool = True,
1075+
) -> Callable[[AsyncCallable], ContextMenu]:
1076+
"""
1077+
A decorator to declare a coroutine as a User Context Menu.
1078+
1079+
Args:
1080+
name: 1-32 character name of the context menu, defaults to the name of the coroutine.
1081+
scopes: The scope this command exists within
1082+
default_member_permissions: What permissions members need to have by default to use this command.
1083+
dm_permission: Should this command be available in DMs.
1084+
1085+
Returns:
1086+
ContextMenu object
1087+
1088+
"""
1089+
return context_menu(
1090+
name,
1091+
context_type=CommandType.USER,
1092+
scopes=scopes,
1093+
default_member_permissions=default_member_permissions,
1094+
dm_permission=dm_permission,
1095+
)
1096+
1097+
1098+
def message_context_menu(
1099+
name: Absent[str | LocalisedName] = MISSING,
1100+
*,
1101+
scopes: Absent[List["Snowflake_Type"]] = MISSING,
1102+
default_member_permissions: Optional["Permissions"] = None,
1103+
dm_permission: bool = True,
1104+
) -> Callable[[AsyncCallable], ContextMenu]:
1105+
"""
1106+
A decorator to declare a coroutine as a Message Context Menu.
1107+
1108+
Args:
1109+
name: 1-32 character name of the context menu, defaults to the name of the coroutine.
1110+
scopes: The scope this command exists within
1111+
default_member_permissions: What permissions members need to have by default to use this command.
1112+
dm_permission: Should this command be available in DMs.
1113+
1114+
Returns:
1115+
ContextMenu object
1116+
1117+
"""
1118+
return context_menu(
1119+
name,
1120+
context_type=CommandType.MESSAGE,
1121+
scopes=scopes,
1122+
default_member_permissions=default_member_permissions,
1123+
dm_permission=dm_permission,
1124+
)
1125+
1126+
10521127
def component_callback(*custom_id: str) -> Callable[[AsyncCallable], ComponentCommand]:
10531128
"""
10541129
Register a coroutine as a component callback.

0 commit comments

Comments
 (0)