|
4 | 4 | from discord.ext import commands |
5 | 5 | from . import http |
6 | 6 | from . import model |
| 7 | +from . import error |
7 | 8 | from .utils import manage_commands |
8 | 9 | from inspect import iscoroutinefunction |
9 | 10 |
|
@@ -166,6 +167,47 @@ async def register_all_commands(self): |
166 | 167 | selected.options) |
167 | 168 | self.logger.info("Completed registering all commands!") |
168 | 169 |
|
| 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 | + |
169 | 211 | def add_slash_command(self, |
170 | 212 | cmd, |
171 | 213 | name: str = None, |
|
0 commit comments