Skip to content

Commit c94ba0b

Browse files
committed
Added auto-register feature
1 parent 7e46a5c commit c94ba0b

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

discord_slash/client.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def __init__(self,
3636
self.req = http.SlashCommandRequest(self.logger)
3737
self.auto_register = auto_register
3838
if self.auto_register:
39-
self.logger.warning("auto_register is NOT implemented! Please manually add commands to Discord API.")
40-
if not isinstance(client, commands.Bot) and not override_type:
39+
self._discord.loop.create_task(self.register_all_commands())
40+
if not isinstance(client, commands.Bot) or not isinstance(client, commands.AutoShardedBot) and not override_type:
4141
self.logger.info("Detected discord.Client! Overriding on_socket_response.")
4242
self._discord.on_socket_response = self.on_socket_response
4343
else:
@@ -46,6 +46,33 @@ def __init__(self,
4646
def remove(self):
4747
self._discord.remove_listener(self.on_socket_response)
4848

49+
async def register_all_commands(self):
50+
await self._discord.wait_until_ready() # In case commands are still not registered to SlashCommand.
51+
self.logger.info("Registering commands...")
52+
for x in self.commands.keys():
53+
selected = self.commands[x]
54+
if selected["has_subcommands"] and "func" not in selected.keys():
55+
# Just in case it has subcommands but also has base command.
56+
# More specific, it will skip if it has subcommands and doesn't have base command coroutine.
57+
self.logger.debug("Skipping registering subcommands.")
58+
continue
59+
if selected["guild_ids"]:
60+
for y in selected["guild_ids"]:
61+
await manage_commands.add_slash_command(self._discord.user.id,
62+
self._discord.http.token,
63+
y,
64+
x,
65+
selected["description"],
66+
selected["api_options"])
67+
else:
68+
await manage_commands.add_slash_command(self._discord.user.id,
69+
self._discord.http.token,
70+
None,
71+
x,
72+
selected["description"],
73+
selected["api_options"])
74+
self.logger.info("Completed registering all commands!")
75+
4976
def add_slash_command(self,
5077
cmd,
5178
name: str = None,
@@ -79,7 +106,7 @@ def add_slash_command(self,
79106
"description": description,
80107
"auto_convert": auto_convert,
81108
"guild_ids": guild_ids,
82-
"api_options": options,
109+
"api_options": options if options else [],
83110
"has_subcommands": has_subcommands
84111
}
85112
self.commands[name] = _cmd

0 commit comments

Comments
 (0)