Skip to content

Commit b673483

Browse files
committed
Add functions to remove all commands
1 parent cf7d107 commit b673483

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

discord_slash/client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from discord.ext import commands
55
from . import http
66
from . import model
7+
from . import error
78
from .utils import manage_commands
89
from inspect import iscoroutinefunction
910

@@ -166,6 +167,47 @@ async def register_all_commands(self):
166167
selected.options)
167168
self.logger.info("Completed registering all commands!")
168169

170+
async def remove_all_commands(self):
171+
"""
172+
Remove all slash commands except subcommands to Discord API.
173+
"""
174+
175+
await self.remove_all_commands_in('global')
176+
177+
for guild in self._discord.guilds:
178+
try: await self.remove_all_commands_in(guild.id)
179+
except error.RequestFailure:
180+
pass
181+
182+
async def remove_all_commands_in(self, area):
183+
"""
184+
Remove all slash commands in area except subcommands to Discord API.
185+
186+
:param area: 'global' or guild ID where removing all commands.
187+
:type area: Union[str, int]
188+
"""
189+
await self._discord.wait_until_ready() # In case commands are still not registered to SlashCommand.
190+
191+
self.logger.info("Removing commands...")
192+
193+
for x in self.commands.keys():
194+
195+
commands = await manage_commands.get_all_commands(
196+
self._discord.user.id,
197+
self._discord.http.token,
198+
None if area == 'global' else area
199+
)
200+
201+
for command in commands:
202+
await manage_commands.remove_slash_command(
203+
self._discord.user.id,
204+
self._discord.http.token,
205+
None if area == 'global' else area,
206+
command['id']
207+
)
208+
209+
self.logger.info("Completed removing all commands !")
210+
169211
def add_slash_command(self,
170212
cmd,
171213
name: str = None,

0 commit comments

Comments
 (0)