Skip to content

Commit 1acf36f

Browse files
committed
Added cog command remove function
1 parent c800536 commit 1acf36f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def _test(ctx: SlashContext):
2222
bot.run("discord_token")
2323
```
2424

25-
Cog (__Not Recommended__):
25+
Cog:
2626
```py
2727
import discord
2828
from discord.ext import commands
@@ -33,12 +33,15 @@ from discord_slash import SlashContext
3333

3434
class Slash(commands.Cog):
3535
def __init__(self, bot):
36-
self.bot = bot
3736
if not hasattr(bot, "slash"):
3837
# Creates new SlashCommand instance to bot if bot doesn't have.
39-
self.bot.slash = SlashCommand(bot, override_type=True)
38+
bot.slash = SlashCommand(bot, override_type=True)
39+
self.bot = bot
4040
self.bot.slash.get_cog_commands(self)
4141

42+
def cog_unload(self):
43+
self.bot.slash.remove_cog_commands(self)
44+
4245
@cog_ext.cog_slash(name="test")
4346
async def _test(self, ctx: SlashContext):
4447
embed = discord.Embed(title="embed test")

discord_slash/client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def remove(self):
5252
"""
5353
Removes :func:`on_socket_response` event listener from discord.py Client.
5454
55+
.. warning::
56+
This is deprecated and will be removed soon.
57+
5558
.. note::
5659
This only works if it is :class:`discord.ext.commands.Bot` or
5760
:class:`discord.ext.commands.AutoShardedBot`.
@@ -61,6 +64,12 @@ def remove(self):
6164
self._discord.remove_listener(self.on_socket_response)
6265

6366
def get_cog_commands(self, cog: commands.Cog):
67+
"""
68+
Gets slash command from :class:`discord.ext.commands.Cog`.
69+
70+
:param cog: Cog that has slash commands.
71+
:type cog: discord.ext.commands.Cog
72+
"""
6473
func_list = [getattr(cog, x) for x in dir(cog)]
6574
res = [x for x in func_list if
6675
isinstance(x, model.CogCommandObject) or isinstance(x, model.CogSubcommandObject)]
@@ -92,9 +101,39 @@ def get_cog_commands(self, cog: commands.Cog):
92101
self.subcommands[x.base][x.name] = x
93102

94103
def remove_cog_commands(self, cog):
104+
"""
105+
Removes slash command from :class:`discord.ext.commands.Cog`.
106+
107+
:param cog: Cog that has slash commands.
108+
:type cog: discord.ext.commands.Cog
109+
"""
95110
func_list = [getattr(cog, x) for x in dir(cog)]
96111
res = [x for x in func_list if
97112
isinstance(x, model.CogCommandObject) or isinstance(x, model.CogSubcommandObject)]
113+
for x in res:
114+
if isinstance(x, model.CogCommandObject):
115+
if x.name not in self.commands.keys():
116+
continue # Just in case it is removed due to subcommand.
117+
if x.name in self.subcommands.keys():
118+
self.commands[x.name].func = None
119+
continue # Let's remove completely when every subcommand is removed.
120+
del self.commands[x.name]
121+
else:
122+
if x.base not in self.subcommands.keys():
123+
continue # Just in case...
124+
if x.subcommand_group:
125+
del self.subcommands[x.base][x.subcommand_group][x.name]
126+
if not self.subcommands[x.base][x.subcommand_group]:
127+
del self.subcommands[x.base][x.subcommand_group]
128+
else:
129+
del self.subcommands[x.base][x.name]
130+
if not self.subcommands[x.base]:
131+
del self.subcommands[x.base]
132+
if x.base in self.commands.keys():
133+
if self.commands[x.base].func:
134+
self.commands[x.base].has_subcommands = False
135+
else:
136+
del self.commands[x.base]
98137

99138
async def register_all_commands(self):
100139
"""

discord_slash/error.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ class IncorrectFormat(SlashCommandError):
2525
"""
2626
def __init__(self, msg: str):
2727
super().__init__(msg)
28+
29+
30+
class DuplicateCommand(SlashCommandError):
31+
"""
32+
There is a duplicate command name.
33+
"""
34+
def __init__(self, name: str):
35+
super().__init__(f"Duplicate command name detected: {name}")

0 commit comments

Comments
 (0)