Skip to content

Commit 618a2cb

Browse files
committed
Changed system of adding command
1 parent 344d54e commit 618a2cb

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

discord_slash/client.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,37 @@ def __init__(self,
3636
else:
3737
self._discord.add_listener(self.on_socket_response)
3838

39+
def add_slash_command(self,
40+
cmd,
41+
name: str = None,
42+
description: str = None,
43+
auto_convert: dict = None,
44+
guild_id: int = None,
45+
options: list = None,
46+
subcommand: dict = None):
47+
"""
48+
Registers slash command to SlashCommand.
49+
50+
:param cmd: Command Coroutine.
51+
:param name: Name of the slash command. Default name of the coroutine.
52+
:param description: Description of the slash command. Default ``None``.
53+
:param auto_convert: Dictionary of how to convert option values. Default ``None``.
54+
:param guild_id: Guild ID of where the command will be used. Default ``None``, which will be global command.
55+
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
56+
:param subcommand: Subcommand if any.
57+
:return: ``None``
58+
"""
59+
_cmd = {
60+
"func": cmd,
61+
"description": description,
62+
"auto_convert": auto_convert,
63+
"guild_id": guild_id,
64+
"api_options": options,
65+
"subcommands": subcommand
66+
}
67+
self.commands[cmd.__name__ if not name else name] = _cmd
68+
self.logger.debug(f"Added command `{cmd.__name__ if not name else name}`")
69+
3970
def slash(self,
4071
*,
4172
name: str = None,
@@ -93,8 +124,7 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
93124
auto_convert[x["name"]] = x["type"]
94125

95126
def wrapper(cmd):
96-
self.commands[cmd.__name__ if not name else name] = [cmd, auto_convert, description, guild_id, options]
97-
self.logger.debug(f"Added command `{cmd.__name__ if not name else name}`")
127+
self.add_slash_command(cmd, name, description, auto_convert, guild_id, options)
98128
return cmd
99129
return wrapper
100130

@@ -155,6 +185,10 @@ async def on_socket_response(self, msg):
155185
if to_use["data"]["name"] in self.commands.keys():
156186
selected_cmd = self.commands[to_use["data"]["name"]]
157187
ctx = model.SlashContext(self.req, to_use, self._discord)
158-
args = self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd[1]) if "options" in to_use["data"] else []
188+
if selected_cmd["guild_id"]:
189+
if selected_cmd["guild_id"] != ctx.guild.id:
190+
return
191+
args = self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd["auto_convert"]) \
192+
if "options" in to_use["data"] else []
159193
self.logger.debug(f"Command {to_use['data']['name']} invoked.")
160-
await selected_cmd[0](ctx, *args)
194+
await selected_cmd["func"](ctx, *args)

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
# List of patterns, relative to source directory, that match files and
4848
# directories to ignore when looking for source files.
4949
# This pattern also affects html_static_path and html_extra_path.
50-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', "test.py", ".idea", "setup.py"]
50+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', "test.py", "test2.py", ".idea", "setup.py"]
5151

5252

5353
# -- Options for HTML output -------------------------------------------------

0 commit comments

Comments
 (0)