From 1a5e0321a87003d0c2a4af071b49c60b49ea5e91 Mon Sep 17 00:00:00 2001 From: Kento <75509362+nkstonks@users.noreply.github.com> Date: Mon, 17 May 2021 12:03:17 +1000 Subject: [PATCH 1/6] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc90f7167..a470f8efe 100644 --- a/README.md +++ b/README.md @@ -79,8 +79,8 @@ def setup(bot): ``` -------- -This library is based on gateway event. If you are looking for webserver based, have a look at this: -[dispike](https://github.com/ms7m/dispike) -[discord-interactions-python](https://github.com/discord/discord-interactions-python) -Or for other languages: -[discord-api-docs Community Resources: Interactions](https://discord.com/developers/docs/topics/community-resources#interactions) +- This library is based on gateway event. If you are looking for webserver based, have a look at this: + - [dispike](https://github.com/ms7m/dispike) + - [discord-interactions-python](https://github.com/discord/discord-interactions-python) +- Or for other languages: + - [discord-api-docs Community Resources: Interactions](https://discord.com/developers/docs/topics/community-resources#interactions) From 718898e3866bd88bc9d30ac04fdea32d1534ecf5 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sat, 12 Jun 2021 11:54:54 -0400 Subject: [PATCH 2/6] Fixes Guild ID mismatch. --- discord_slash/client.py | 49 ++++++++++++++++++++++++---------------- discord_slash/cog_ext.py | 6 +++++ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/discord_slash/client.py b/discord_slash/client.py index bb054d2ef..86df3bb37 100644 --- a/discord_slash/client.py +++ b/discord_slash/client.py @@ -60,8 +60,10 @@ def __init__(self, if self.sync_commands: self._discord.loop.create_task(self.sync_all_commands(delete_from_unused_guilds)) - if not isinstance(client, commands.Bot) and not isinstance(client, commands.AutoShardedBot) and not override_type: - self.logger.warning("Detected discord.Client! It is highly recommended to use `commands.Bot`. Do not add any `on_socket_response` event.") + if not isinstance(client, commands.Bot) and not isinstance(client, + commands.AutoShardedBot) and not override_type: + self.logger.warning( + "Detected discord.Client! It is highly recommended to use `commands.Bot`. Do not add any `on_socket_response` event.") self._discord.on_socket_response = self.on_socket_response self.has_listener = False else: @@ -306,7 +308,7 @@ async def to_dict(self): return cmds async def sync_all_commands( - self, delete_from_unused_guilds=False, delete_perms_from_unused_guilds=False + self, delete_from_unused_guilds=False, delete_perms_from_unused_guilds=False ): """ Matches commands registered on Discord to commands registered here. @@ -328,14 +330,14 @@ async def sync_all_commands( for scope in cmds_formatted: permissions = {} new_cmds = cmds_formatted[scope] - existing_cmds = await self.req.get_all_commands(guild_id = scope) + existing_cmds = await self.req.get_all_commands(guild_id=scope) existing_by_name = {} - to_send=[] + to_send = [] changed = False for cmd in existing_cmds: existing_by_name[cmd["name"]] = model.CommandData(**cmd) - if len(new_cmds) != len(existing_cmds): + if len(new_cmds) != len(existing_cmds): changed = True for command in new_cmds: @@ -345,17 +347,16 @@ async def sync_all_commands( cmd_data = model.CommandData(**command) existing_cmd = existing_by_name[cmd_name] if cmd_data != existing_cmd: - changed=True + changed = True to_send.append(command) else: command_with_id = command command_with_id["id"] = existing_cmd.id to_send.append(command_with_id) else: - changed=True + changed = True to_send.append(command) - if changed: self.logger.debug(f"Detected changes on {scope if scope is not None else 'global'}, updating them") existing_cmds = await self.req.put_slash_commands(slash_commands=to_send, guild_id=scope) @@ -379,7 +380,6 @@ async def sync_all_commands( } permissions_map[applicable_guild].append(permission) - self.logger.info("Syncing permissions...") self.logger.debug(f"Commands permission data are {permissions_map}") for scope in permissions_map: @@ -400,28 +400,26 @@ async def sync_all_commands( if existing_perms_model[new_perm["id"]] != model.GuildPermissionsData(**new_perm): changed = True break - + if changed: self.logger.debug(f"Detected permissions changes on {scope}, updating them") await self.req.update_guild_commands_permissions(scope, new_perms) else: self.logger.debug(f"Detected no permissions changes on {scope}, skipping") - if delete_from_unused_guilds: self.logger.info("Deleting unused guild commands...") other_guilds = [guild.id for guild in self._discord.guilds if guild.id not in cmds["guild"]] # This is an extremly bad way to do this, because slash cmds can be in guilds the bot isn't in # But it's the only way until discord makes an endpoint to request all the guild with cmds registered. - + for guild in other_guilds: with suppress(discord.Forbidden): - existing = await self.req.get_all_commands(guild_id = guild) + existing = await self.req.get_all_commands(guild_id=guild) if len(existing) != 0: self.logger.debug(f"Deleting commands from {guild}") await self.req.put_slash_commands(slash_commands=[], guild_id=guild) - if delete_perms_from_unused_guilds: self.logger.info("Deleting unused guild permissions...") other_guilds = [guild.id for guild in self._discord.guilds if guild.id not in permissions_map.keys()] @@ -473,6 +471,9 @@ def add_slash_command(self, name = name or cmd.__name__ name = name.lower() guild_ids = guild_ids if guild_ids else [] + if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: + raise Exception( + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") if name in self.commands: tgt = self.commands[name] if not tgt.has_subcommands: @@ -549,6 +550,9 @@ def add_subcommand(self, name = name.lower() description = description or getdoc(cmd) guild_ids = guild_ids if guild_ids else [] + if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: + raise Exception( + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") if base in self.commands: for x in guild_ids: @@ -680,7 +684,8 @@ def wrapper(cmd): if decorator_permissions: permissions.update(decorator_permissions) - obj = self.add_slash_command(cmd, name, description, guild_ids, options, default_permission, permissions, connector) + obj = self.add_slash_command(cmd, name, description, guild_ids, options, default_permission, permissions, + connector) return obj return wrapper @@ -763,7 +768,9 @@ def wrapper(cmd): if decorator_permissions: base_permissions.update(decorator_permissions) - obj = self.add_subcommand(cmd, base, subcommand_group, name, description, base_description, base_default_permission, base_permissions, subcommand_group_description, guild_ids, options, connector) + obj = self.add_subcommand(cmd, base, subcommand_group, name, description, base_description, + base_default_permission, base_permissions, subcommand_group_description, + guild_ids, options, connector) return obj return wrapper @@ -771,12 +778,13 @@ def wrapper(cmd): def permission(self, guild_id: int, permissions: list): """ Decorator that add permissions. This will set the permissions for a single guild, you can use it more than once for each command. - :param guild_id: ID of the guild for the permissions. + :param guild_id: ID of the guild for the permissions. :type guild_id: int :param permissions: Permission requirements of the slash command. Default ``None``. :type permissions: dict - + """ + def wrapper(cmd): if not getattr(cmd, "__permissions__", None): cmd.__permissions__ = {} @@ -917,7 +925,8 @@ async def on_socket_response(self, msg): for x in selected_cmd.options: temporary_auto_convert[x["name"].lower()] = x["type"] - args = await self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd.connector, temporary_auto_convert) \ + args = await self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd.connector, + temporary_auto_convert) \ if "options" in to_use["data"] else {} self._discord.dispatch("slash_command", ctx) diff --git a/discord_slash/cog_ext.py b/discord_slash/cog_ext.py index 1d5589d5c..d507864f2 100644 --- a/discord_slash/cog_ext.py +++ b/discord_slash/cog_ext.py @@ -50,6 +50,9 @@ def wrapper(cmd): else: opts = options + if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: + raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + _cmd = { "func": cmd, "description": desc, @@ -130,6 +133,9 @@ def wrapper(cmd): else: opts = options + if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: + raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + _cmd = { "func": None, "description": base_description, From 4b149ecbc9293a2f595c72c3387bf46f18af7ca3 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sat, 12 Jun 2021 13:04:14 -0400 Subject: [PATCH 3/6] Reorder logic due to iterating a None object. --- discord_slash/cog_ext.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/discord_slash/cog_ext.py b/discord_slash/cog_ext.py index d507864f2..d119c5f71 100644 --- a/discord_slash/cog_ext.py +++ b/discord_slash/cog_ext.py @@ -50,8 +50,9 @@ def wrapper(cmd): else: opts = options - if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: - raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + if guild_ids is not None: + if not all(isinstance(item, int) for item in guild_ids): + raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") _cmd = { "func": cmd, @@ -133,8 +134,9 @@ def wrapper(cmd): else: opts = options - if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: - raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + if guild_ids is not None: + if not all(isinstance(item, int) for item in guild_ids): + raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") _cmd = { "func": None, From 36ea7762eb0e746bc4a2b6b8fc5dddc104b6127b Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Sun, 13 Jun 2021 17:15:25 -0400 Subject: [PATCH 4/6] Add custom Exception class. --- discord_slash/client.py | 4 ++-- discord_slash/cog_ext.py | 6 ++++-- discord_slash/error.py | 5 +++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/discord_slash/client.py b/discord_slash/client.py index 86df3bb37..defa32141 100644 --- a/discord_slash/client.py +++ b/discord_slash/client.py @@ -472,7 +472,7 @@ def add_slash_command(self, name = name.lower() guild_ids = guild_ids if guild_ids else [] if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: - raise Exception( + raise error.IncorrectGuildIDType( f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") if name in self.commands: tgt = self.commands[name] @@ -551,7 +551,7 @@ def add_subcommand(self, description = description or getdoc(cmd) guild_ids = guild_ids if guild_ids else [] if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: - raise Exception( + raise error.IncorrectGuildIDType( f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") if base in self.commands: diff --git a/discord_slash/cog_ext.py b/discord_slash/cog_ext.py index d119c5f71..93d9da004 100644 --- a/discord_slash/cog_ext.py +++ b/discord_slash/cog_ext.py @@ -1,5 +1,7 @@ import typing import inspect + +from .error import IncorrectGuildIDType from .model import CogBaseCommandObject, CogSubcommandObject from .utils import manage_commands @@ -52,7 +54,7 @@ def wrapper(cmd): if guild_ids is not None: if not all(isinstance(item, int) for item in guild_ids): - raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + raise IncorrectGuildIDType(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") _cmd = { "func": cmd, @@ -136,7 +138,7 @@ def wrapper(cmd): if guild_ids is not None: if not all(isinstance(item, int) for item in guild_ids): - raise Exception(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + raise IncorrectGuildIDType(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") _cmd = { "func": None, diff --git a/discord_slash/error.py b/discord_slash/error.py index 8df1b92de..f93484dc6 100644 --- a/discord_slash/error.py +++ b/discord_slash/error.py @@ -55,6 +55,11 @@ class IncorrectType(SlashCommandError): Type passed was incorrect """ +class IncorrectGuildIDType(SlashCommandError): + """ + Guild ID type passed was incorrect + """ + class IncorrectCommandData(SlashCommandError): """ From 2eaaf7622954b22b817dd9b1ac431dd3b27bca3f Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Mon, 14 Jun 2021 13:31:51 -0400 Subject: [PATCH 5/6] Fix README.md (Wonky switch branch problem fix, fix accidental README line deletion) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e10fa7543..972abb999 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ def setup(bot): bot.add_cog(Slash(bot)) ``` +-------- - This library is based on gateway event. If you are looking for webserver based, have a look at this: - [dispike](https://github.com/ms7m/dispike) - [discord-interactions-python](https://github.com/discord/discord-interactions-python) From 30573a7c7a0707240492aed5649938ccfa7ffb49 Mon Sep 17 00:00:00 2001 From: DeltaXWizard <33706469+deltaxwizard@users.noreply.github.com> Date: Thu, 17 Jun 2021 16:47:28 -0400 Subject: [PATCH 6/6] Ran pre_push.py as requested. --- discord_slash/client.py | 14 +++++++------- discord_slash/cog_ext.py | 12 ++++++++---- discord_slash/error.py | 1 + 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/discord_slash/client.py b/discord_slash/client.py index e9fd7027f..6493c6b28 100644 --- a/discord_slash/client.py +++ b/discord_slash/client.py @@ -61,7 +61,6 @@ def __init__( if self.sync_commands: self._discord.loop.create_task(self.sync_all_commands(delete_from_unused_guilds)) - if ( not isinstance(client, commands.Bot) and not isinstance(client, commands.AutoShardedBot) @@ -325,7 +324,7 @@ async def to_dict(self): return cmds async def sync_all_commands( - self, delete_from_unused_guilds=False, delete_perms_from_unused_guilds=False + self, delete_from_unused_guilds=False, delete_perms_from_unused_guilds=False ): """ Matches commands registered on Discord to commands registered here. @@ -506,7 +505,8 @@ def add_slash_command( guild_ids = guild_ids if guild_ids else [] if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: raise error.IncorrectGuildIDType( - f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed." + ) if name in self.commands: tgt = self.commands[name] if not tgt.has_subcommands: @@ -587,7 +587,8 @@ def add_subcommand( guild_ids = guild_ids if guild_ids else [] if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []: raise error.IncorrectGuildIDType( - f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed.") + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name}' will be deactivated and broken until fixed." + ) if base in self.commands: for x in guild_ids: @@ -735,7 +736,7 @@ def wrapper(cmd): permissions, connector, ) - + return obj return wrapper @@ -834,7 +835,7 @@ def wrapper(cmd): options, connector, ) - + return obj return wrapper @@ -1025,7 +1026,6 @@ async def _on_slash(self, to_use): for x in selected_cmd.options: temporary_auto_convert[x["name"].lower()] = x["type"] - args = ( await self.process_options( ctx.guild, diff --git a/discord_slash/cog_ext.py b/discord_slash/cog_ext.py index 79e5c750b..7f3f9c75e 100644 --- a/discord_slash/cog_ext.py +++ b/discord_slash/cog_ext.py @@ -14,7 +14,7 @@ def cog_slash( options: typing.List[dict] = None, default_permission: bool = True, permissions: typing.Dict[int, list] = None, - connector: dict = None + connector: dict = None, ): """ Decorator for Cog to add slash command.\n @@ -57,7 +57,9 @@ def wrapper(cmd): if guild_ids is not None: if not all(isinstance(item, int) for item in guild_ids): - raise IncorrectGuildIDType(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + raise IncorrectGuildIDType( + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed." + ) _cmd = { "func": cmd, @@ -88,7 +90,7 @@ def cog_subcommand( sub_group_desc: str = None, guild_ids: typing.List[int] = None, options: typing.List[dict] = None, - connector: dict = None + connector: dict = None, ): """ Decorator for Cog to add subcommand.\n @@ -144,7 +146,9 @@ def wrapper(cmd): if guild_ids is not None: if not all(isinstance(item, int) for item in guild_ids): - raise IncorrectGuildIDType(f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed.") + raise IncorrectGuildIDType( + f"The snowflake IDs {guild_ids} given are not a list of integers. Because of discord.py convention, please use integer IDs instead. Furthermore, the command '{name or cmd.__name__}' will be deactivated and broken until fixed." + ) _cmd = { "func": None, diff --git a/discord_slash/error.py b/discord_slash/error.py index 296a44ccf..30e6041ed 100644 --- a/discord_slash/error.py +++ b/discord_slash/error.py @@ -57,6 +57,7 @@ class IncorrectType(SlashCommandError): Type passed was incorrect """ + class IncorrectGuildIDType(SlashCommandError): """ Guild ID type passed was incorrect