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

Commit f0f3205

Browse files
author
kuso-senpai
committed
fixed global command adding
1 parent 86ca751 commit f0f3205

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

README.md

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

173173
# Changelog
174174

175+
- <details>
176+
<summary>4.1.1</summary>
177+
178+
## **Fixed**
179+
- Interaction.author.voice
180+
> For some reason the voice property of the creator or the interaction would be set
181+
182+
- Global slashcommands
183+
> They wouldn't be registered to the api
184+
185+
- Subcommands editing
186+
* not fixed yet
187+
> subcommand checks were wrong and this would result in errors like `In options.0.options.2: Option name 'name' is already used in these options`
188+
189+
</details>
190+
175191
- <details>
176192
<summary>4.1.0</summary>
177193

discord_ui/client.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ async def _on_response(self, msg):
164164
if int(data["type"]) not in [1, 2]:
165165
return
166166

167-
guild = None
168-
if data.get("guild_id") is not None:
169-
self._discord._connection._get_guild(int(data["guild_id"]))
170-
user = discord.Member(data=data["member"], guild=guild, state=self._discord._connection) if data.get("member") is not None else discord.User(state=self._discord._connection, data=data["user"])
167+
user = discord.Member(data=data["member"], guild=self._discord._connection._get_guild(int(data["guild_id"])), state=self._discord._connection) if data.get("member") is not None else discord.User(state=self._discord._connection, data=data["user"])
171168

172169
interaction = Interaction(self._discord._connection, data, user)
173170
if self.auto_defer[0] is True:
@@ -377,17 +374,17 @@ async def add_command(command):
377374
for guild in list(command.guild_permissions.keys()):
378375
await self.update_permissions(command.name, command.type, guild_id=guild, permissions=command.guild_permissions[guild], global_command=True)
379376
# global command
380-
else:
377+
elif command.guild_ids is MISSING and command.guild_permissions is MISSING:
381378
logging.debug("adding '" + str(command.name) + "' as global command")
382379
await global_stuff(command)
383-
380+
384381
for x in commands:
385382
await add_command(commands[x])
386383

387384
for x in self.context_commands:
388385
for com in self.context_commands[x]:
389386
await add_command(self.context_commands[x][com])
390-
387+
391388
if delete_unused:
392389
api_coms = await self._get_global_commands()
393390
for apic in api_coms:
@@ -509,6 +506,7 @@ async def add_guild_command(self, base, guild_id):
509506
await delete_global_command(self._discord, global_command["id"])
510507
await create_guild_command(base.to_dict(), self._discord, target_guild, base.permissions.to_dict())
511508
elif api_command != base:
509+
# print("api_command", api_command, "\n", "base", base.to_dict())
512510
await edit_guild_command(api_command["id"], self._discord, target_guild, base.to_dict(), base.permissions.to_dict())
513511
elif api_permissions != base.permissions:
514512
await update_command_permissions(self._discord.user.id, self._discord.http.token, guild_id, api_command["id"], base.permissions.to_dict())
@@ -1144,10 +1142,7 @@ async def _on_response(self, msg):
11441142
if data["type"] != 3:
11451143
return
11461144

1147-
guild = None
1148-
if data.get("guild_id") is not None:
1149-
guild = cache_data(data["guild_id"], AdditionalType.GUILD, data, self._discord._connection)
1150-
user = discord.Member(data=data["member"], guild=guild, state=self._discord._connection) if data.get("member") is not None else discord.User(state=self._discord._connection, data=data["user"])
1145+
user = discord.Member(data=data["member"], guild=self._discord._connection._get_guild(int(data["guild_id"])), state=self._discord._connection) if data.get("member") is not None else discord.User(state=self._discord._connection, data=data["user"])
11511146
msg = await getMessage(self._discord._connection, data=data, response=True)
11521147

11531148
interaction = Interaction(self._discord._connection, data, user, msg)

discord_ui/receive.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .errors import InvalidEvent, OutOfValidRange, WrongType
22
from .slash.errors import AlreadyDeferred, EphemeralDeletion
33
from .slash.types import ContextCommand, SlashCommand, SlashPermission, SlashSubcommand
4-
from .tools import MISSING, setup_logger
4+
from .tools import MISSING, setup_logger, _none
55
from .http import BetterRoute, jsonifyMessage, send_files
66
from .components import ActionRow, Button, LinkButton, SelectMenu, SelectOption, make_component
77

@@ -303,7 +303,7 @@ def __init__(self, client, command: SlashCommand, data, user, args = None, guild
303303
self.bot: Bot = client
304304
self._json = command.to_dict()
305305
self.author: discord.Member = user
306-
"""The channel where the slash command was used"""
306+
"""The user who used the command"""
307307
self.guild_ids = guild_ids
308308
"""The ids of the guilds where the command is available"""
309309
self.args: Dict[str, Union[str, int, bool, discord.Member, discord.TextChannel, discord.Role, float]] = args
@@ -569,11 +569,11 @@ async def wait_for(self, event_name: Literal["select", "button"], client, custom
569569
def _check(com):
570570
if com.message.id == self.id:
571571
statements = []
572-
if custom_id not in [MISSING, None]:
572+
if not _none(custom_id):
573573
statements.append(com.custom_id == custom_id)
574-
if by not in [MISSING, None]:
574+
if not _none(by):
575575
statements.append(com.member.id == (by.id if hasattr(by, "id") else int(by)))
576-
if check not in [MISSING, None]:
576+
if not _none(check):
577577
statements.append(check(com))
578578
return all(statements)
579579
return False

discord_ui/slash/types.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..tools import MISSING, _or
1+
from ..tools import MISSING, _or, _default, _none
22
from ..errors import InvalidLength, WrongType
33
from .errors import CallbackMissingContextCommandParameters, MissingOptionParameter, NoAsyncCallback, OptionalOptionParameter
44

@@ -414,21 +414,21 @@ async def my_function(command, parameter=None):
414414
raise OptionalOptionParameter(param.name)
415415

416416
self.callback: function = callback
417-
self.name = _or(name, self.callback.__name__ if self.callback not in [None, MISSING] else None)
418-
self.description = _or(description, (inspect.getdoc(self.callback) if self.callback not in [None, MISSING] else None), self.name)
417+
self.name = _or(name, self.callback.__name__ if not _none(self.callback) else None)
418+
self.description = _or(description, (inspect.getdoc(self.callback) if not _none(self.callback) else None), self.name)
419419
if default_permission is MISSING:
420420
default_permission = True
421421
self.default_permission: bool = default_permission
422-
if guild_permissions not in [MISSING, None]:
422+
if not _none(guild_permissions):
423423
for _id, perm in list(guild_permissions.items()):
424424
if type(_id) not in [str, int, discord.User, discord.Member, discord.Role]:
425425
raise WrongType("guild_permissions key " + str(_id), _id, ["str", "int", "discord.User", "discord.Member", "discord.Role"])
426426
if type(perm) is not SlashPermission:
427427
raise WrongType("guild_permission[" + ("'" if type(_id) is str else "") + str(_id) + ("'" if type(_id) is str else "") + "]", perm, "SlashPermission")
428428

429-
self.guild_permissions: typing.Dict[(typing.Union[str, int], SlashPermission)] = guild_permissions or MISSING
429+
self.guild_permissions: typing.Dict[(typing.Union[str, int], SlashPermission)] = _default(MISSING, guild_permissions)
430430
self.permissions: SlashPermission = SlashPermission()
431-
self.guild_ids: typing.List[int] = [int(x) for x in _or(guild_ids, [])]
431+
self.guild_ids: typing.List[int] = _default(MISSING, [int(x) for x in _or(guild_ids, [])])
432432

433433
def __str__(self) -> str:
434434
return str(self.to_dict())
@@ -469,7 +469,7 @@ def name(self) -> str:
469469
return self._json["name"]
470470
@name.setter
471471
def name(self, value):
472-
if value in [None, MISSING]:
472+
if _none(value):
473473
raise InvalidArgument("You have to specify a name")
474474
if type(value) is not str:
475475
raise WrongType("name", value, "str")

discord_ui/tools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ def get(self, *args):
1717

1818
MISSING = _MISSING()
1919

20+
def _none(*args, empty_array=False):
21+
return all(x in [None, MISSING] + ([[]] if empty_array is True else []) for x in args)
2022
def _or(*args, default=None):
2123
for i in range(len(args)):
22-
if args[i] not in [MISSING, None]:
24+
if not _none(args[i]):
2325
return args[i]
2426
return default
27+
def _default(default, *args, empty_array=True):
28+
if _none(*args, empty_array=empty_array):
29+
return default
30+
if len(args) == 1:
31+
return args[0]
32+
return args
33+
2534

2635
def get_index(l: list, elem: Any, mapping = lambda x: x, default: int = -1) -> int:
2736
"""Returns the index of an element in the list

0 commit comments

Comments
 (0)