Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit e92cf84

Browse files
committed
fixed some things
1 parent 5b94854 commit e92cf84

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ You can find more (and better) examples [here](https://github.com/discord-py-ui/
177177

178178
- <details>
179179

180+
## **Added**
181+
182+
- edit_subcomand
183+
184+
## **Fixed**
185+
186+
- print statements
187+
180188
## **Fixed**
181189

182190
- #94 (again 💀)

discord_ui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444

4545

4646
__title__ = "discord-ui"
47-
__version__ = "4.2.8"
47+
__version__ = "4.2.9"

discord_ui/client.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,94 @@ async def edit_command(self, old_name, typ: Literal["slash", 1, "user", 2, "mess
644644
else:
645645
await self.create_command(command)
646646
self._set_command(old_name, command)
647-
647+
async def edit_subcommand(self, base_names, old_name, guild_id=MISSING, *, name, description, options, guild_ids, default_permission, guild_permissions, callback=MISSING):
648+
"""
649+
Edits a subcommand
650+
651+
Parameters
652+
----------
653+
base_names: List[:class:`str`] | :class:`str`
654+
The base_names of the command
655+
old_name: :class:`str`
656+
The original name of the command
657+
guild_id: :class:`[type]`, optional
658+
The guild id of the command where the changes should be applied; default ``MISSING``
659+
name: :class:`str`, optional
660+
The new name of the command
661+
description: :class:`str`, optional
662+
The new description of the command
663+
options: List[:class:`~SlashOption`], optional
664+
The new options for the command
665+
guild_ids: List[:class:`int` | :class:`str`], optional
666+
The list of new guild_ids where the command is available
667+
default_permission: :class:`bool`, optional
668+
The new default permissions for the command
669+
guild_permissions: :class:`dict`, optional
670+
The new guild permissions for the command
671+
callback: :class:`function`, optional, optional
672+
The new command callback; default ``MISSING``
673+
674+
Raises
675+
------
676+
:raises: :class:`NotFound` : When a command in the internal cache doesn't exsist
677+
:raises: :class:`NotFound` : When a command in the api doesn't exist
678+
679+
"""
680+
if isinstance(base_names, str):
681+
base_names = [base_names]
682+
base = self.gather_commands()[base_names[0]]
683+
origin_base = base
684+
if len(base_names) > 1 and isinstance(base, dict):
685+
base = get(base.options, base_names[1], lambda x: x.getattr("name"))
686+
op: SlashOption = get(base, old_name, lambda x: x.getattr("name"))
687+
sub = SlashSubcommand.from_data(op.to_dict())
688+
689+
if guild_id is not MISSING:
690+
api_command = await self._get_guild_api_command(old_name, CommandType.SLASH, guild_id)
691+
else:
692+
api_command = await self._get_global_api_command(old_name, CommandType.Slash)
693+
694+
if name is not None:
695+
sub.name = name
696+
if description is not None:
697+
sub.description = description
698+
if options is not None:
699+
sub.options = options
700+
if guild_ids is not None:
701+
sub.guild_ids = guild_ids
702+
if default_permission is not None:
703+
sub.default_permission = default_permission
704+
if guild_permissions is not None:
705+
sub.guild_permissions = guild_permissions
706+
if callback is not MISSING:
707+
sub.callback = callback
708+
709+
base.guild_ids = sub.guild_ids
710+
base.guild_permissions = sub.guild_permissions
711+
base.default_permission = default_permission
712+
713+
# When command only should be edited in one guild
714+
if guild_id is not MISSING and guild_ids is MISSING or sub.guild_ids == guild_ids:
715+
await edit_guild_command(api_command["id"], self._discord, guild_id, base.to_dict(), sub.guild_permissions.get(guild_id))
716+
# When guild_ids were changed
717+
elif guild_ids is not MISSING and guild_ids != origin_base.guild_ids:
718+
for x in guild_ids:
719+
await self.add_guild_command(base, x)
720+
# When guild command is changed to global command
721+
elif guild_id is not MISSING and guild_ids is MISSING and base.guild_ids is not MISSING:
722+
for x in base.guild_ids:
723+
com = await self._get_guild_api_command(base.name, base.command_type, x)
724+
await delete_guild_command(self._discord, com["id"], x)
725+
await self.add_global_command(base)
726+
# When global command is changed to guild command
727+
elif origin_base.guild_ids is MISSING and base.guild_ids is not MISSING:
728+
com = await self._get_global_api_command(origin_base.name, origin_base.command_type)
729+
await delete_global_command(self._discord, com["id"])
730+
await self.create_command(base)
731+
else:
732+
await self.create_command(base)
733+
self._set_command(old_name, sub)
734+
648735
def _get_command(self, name, typ: Literal["slash", 1, "user", 2, "message", 3]) -> SlashCommand:
649736
typ = CommandType.from_string(typ)
650737
if typ == CommandType.SLASH:

discord_ui/receive.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,13 @@ class SlashedSubCommand(SlashedCommand, SlashSubcommand):
317317
"""A Sub-:class:`~SlashCommand` command that was used"""
318318
def __init__(self, client, command, data, user, args = None, guild_ids = None, guild_permissions=None) -> None:
319319
SlashedCommand.__init__(self, client, command, data, user, args, guild_ids=guild_ids, guild_permissions=guild_permissions)
320-
print(data)
321320
SlashSubcommand.__init__(self, None, ["EMPTY"], "EMPTY")
322321
self.base_names[0] = data["data"]["name"]
323322
_sub = data["data"]["options"][0]
324323
if _sub["type"] == OptionType.SUB_COMMAND_GROUP:
325-
print("group")
326324
self.base_names.append(_sub["name"])
327325
self.name = _sub["options"][0]["name"]
328326
else:
329-
print("sub")
330327
self.name = _sub["name"]
331328

332329

0 commit comments

Comments
 (0)