Skip to content

Commit cd4e8bf

Browse files
FayeDelnkstonksLordOfPolls
authored
String Snowflake ID in decorator args (#209)
* Update README.md * Fixes Guild ID mismatch. * Reorder logic due to iterating a None object. * Add custom Exception class. * Fix README.md (Wonky switch branch problem fix, fix accidental README line deletion) * Ran pre_push.py as requested. Co-authored-by: Kento <75509362+nkstonks@users.noreply.github.com> Co-authored-by: LordOfPolls <22540825+LordOfPolls@users.noreply.github.com>
1 parent 6a5cb29 commit cd4e8bf

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ def setup(bot):
8383
- [dispike](https://github.com/ms7m/dispike)
8484
- [discord-interactions-python](https://github.com/discord/discord-interactions-python)
8585
- Or for other languages:
86-
- [discord-api-docs Community Resources: Interactions](https://discord.com/developers/docs/topics/community-resources#interactions)
86+
- [discord-api-docs Community Resources: Interactions](https://discord.com/developers/docs/topics/community-resources#interactions)
87+

discord_slash/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(
6969
self.logger.warning(
7070
"Detected discord.Client! It is highly recommended to use `commands.Bot`. Do not add any `on_socket_response` event."
7171
)
72+
7273
self._discord.on_socket_response = self.on_socket_response
7374
self.has_listener = False
7475
else:
@@ -502,6 +503,10 @@ def add_slash_command(
502503
name = name or cmd.__name__
503504
name = name.lower()
504505
guild_ids = guild_ids if guild_ids else []
506+
if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []:
507+
raise error.IncorrectGuildIDType(
508+
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."
509+
)
505510
if name in self.commands:
506511
tgt = self.commands[name]
507512
if not tgt.has_subcommands:
@@ -580,6 +585,10 @@ def add_subcommand(
580585
name = name.lower()
581586
description = description or getdoc(cmd)
582587
guild_ids = guild_ids if guild_ids else []
588+
if not all(isinstance(item, int) for item in guild_ids) and guild_ids is not []:
589+
raise error.IncorrectGuildIDType(
590+
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."
591+
)
583592

584593
if base in self.commands:
585594
for x in guild_ids:
@@ -727,6 +736,7 @@ def wrapper(cmd):
727736
permissions,
728737
connector,
729738
)
739+
730740
return obj
731741

732742
return wrapper
@@ -825,6 +835,7 @@ def wrapper(cmd):
825835
options,
826836
connector,
827837
)
838+
828839
return obj
829840

830841
return wrapper

discord_slash/cog_ext.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import typing
33

4+
from .error import IncorrectGuildIDType
45
from .model import CogBaseCommandObject, CogSubcommandObject
56
from .utils import manage_commands
67

@@ -13,7 +14,7 @@ def cog_slash(
1314
options: typing.List[dict] = None,
1415
default_permission: bool = True,
1516
permissions: typing.Dict[int, list] = None,
16-
connector: dict = None
17+
connector: dict = None,
1718
):
1819
"""
1920
Decorator for Cog to add slash command.\n
@@ -54,6 +55,12 @@ def wrapper(cmd):
5455
else:
5556
opts = options
5657

58+
if guild_ids is not None:
59+
if not all(isinstance(item, int) for item in guild_ids):
60+
raise IncorrectGuildIDType(
61+
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."
62+
)
63+
5764
_cmd = {
5865
"func": cmd,
5966
"description": desc,
@@ -83,7 +90,7 @@ def cog_subcommand(
8390
sub_group_desc: str = None,
8491
guild_ids: typing.List[int] = None,
8592
options: typing.List[dict] = None,
86-
connector: dict = None
93+
connector: dict = None,
8794
):
8895
"""
8996
Decorator for Cog to add subcommand.\n
@@ -137,6 +144,12 @@ def wrapper(cmd):
137144
else:
138145
opts = options
139146

147+
if guild_ids is not None:
148+
if not all(isinstance(item, int) for item in guild_ids):
149+
raise IncorrectGuildIDType(
150+
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."
151+
)
152+
140153
_cmd = {
141154
"func": None,
142155
"description": base_description,

discord_slash/error.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class IncorrectType(SlashCommandError):
5858
"""
5959

6060

61+
class IncorrectGuildIDType(SlashCommandError):
62+
"""
63+
Guild ID type passed was incorrect
64+
"""
65+
66+
6167
class IncorrectCommandData(SlashCommandError):
6268
"""
6369
Incorrect data was passed to a slash command data object

0 commit comments

Comments
 (0)