Skip to content

Commit c800536

Browse files
committed
Added cog subcommand support
1 parent 03f0a25 commit c800536

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@ Cog (__Not Recommended__):
2626
```py
2727
import discord
2828
from discord.ext import commands
29+
from discord_slash import cog_ext
2930
from discord_slash import SlashCommand
3031
from discord_slash import SlashContext
3132

3233

3334
class Slash(commands.Cog):
3435
def __init__(self, bot):
3536
self.bot = bot
36-
self.slash = SlashCommand(bot, override_type=True)
37-
# Cog is only supported by commands ext, so just skip checking type.
37+
if not hasattr(bot, "slash"):
38+
# Creates new SlashCommand instance to bot if bot doesn't have.
39+
self.bot.slash = SlashCommand(bot, override_type=True)
40+
self.bot.slash.get_cog_commands(self)
3841

39-
# Make sure all commands should be inside `__init__`
40-
# or some other functions that can put commands.
41-
@self.slash.slash(name="test")
42-
async def _test(ctx: SlashContext):
43-
await ctx.send(content="Hello, World!")
44-
45-
def cog_unload(self):
46-
self.slash.remove()
42+
@cog_ext.cog_slash(name="test")
43+
async def _test(self, ctx: SlashContext):
44+
embed = discord.Embed(title="embed test")
45+
await ctx.send(content="test", embeds=[embed])
4746

4847

4948
def setup(bot):
@@ -72,5 +71,4 @@ Or you can ask at [Discussions](https://github.com/eunwoo1104/discord-py-slash-c
7271

7372
## TODO
7473
- Rewrite `http.py` and webhook part (Maybe use discord.py's webhook support?)
75-
- Properly support Cog
7674
- Try supporting most of the features supported by discord.py commands extension

discord_slash/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,30 @@ def get_cog_commands(self, cog: commands.Cog):
7272
if x.base in self.commands.keys():
7373
self.commands[x.base].allowed_guild_ids += x.allowed_guild_ids
7474
self.commands[x.base].has_subcommands = True
75+
else:
76+
_cmd = {
77+
"func": None,
78+
"description": "No description.",
79+
"auto_convert": {},
80+
"guild_ids": x.allowed_guild_ids,
81+
"api_options": [],
82+
"has_subcommands": True
83+
}
84+
self.commands[x.base] = model.CommandObject(x.base, _cmd)
7585
if x.base not in self.subcommands.keys():
7686
self.subcommands[x.base] = {}
7787
if x.subcommand_group:
7888
if x.subcommand_group not in self.subcommands:
7989
self.subcommands[x.base][x.subcommand_group] = {}
80-
else:
81-
self.subcommands[x.base][x.subcommand_group][x.name] = x
90+
self.subcommands[x.base][x.subcommand_group][x.name] = x
8291
else:
8392
self.subcommands[x.base][x.name] = x
8493

94+
def remove_cog_commands(self, cog):
95+
func_list = [getattr(cog, x) for x in dir(cog)]
96+
res = [x for x in func_list if
97+
isinstance(x, model.CogCommandObject) or isinstance(x, model.CogSubcommandObject)]
98+
8599
async def register_all_commands(self):
86100
"""
87101
Registers all slash commands except subcommands to Discord API.\n
@@ -188,7 +202,7 @@ def add_subcommand(self,
188202
name = name.lower()
189203
_cmd = {
190204
"func": None,
191-
"description": description if description else "No description.",
205+
"description": "No description.",
192206
"auto_convert": {},
193207
"guild_ids": guild_ids,
194208
"api_options": [],

discord_slash/cog_ext.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import typing
2-
from discord.ext import commands
32
from .model import CogCommandObject, CogSubcommandObject
43

54

@@ -22,6 +21,20 @@ def wrapper(cmd):
2221
return wrapper
2322

2423

25-
def register_cog_slash(cls):
26-
func_list = [getattr(cls, x) for x in dir(cls)]
27-
return [x for x in func_list if isinstance(x, CogCommandObject) or isinstance(x, CogSubcommandObject)]
24+
def cog_subcommand(*,
25+
base,
26+
subcommand_group=None,
27+
name=None,
28+
description: str = None,
29+
auto_convert: dict = None,
30+
guild_ids: typing.List[int] = None):
31+
def wrapper(cmd):
32+
_sub = {
33+
"func": cmd,
34+
"name": name,
35+
"description": description,
36+
"auto_convert": auto_convert,
37+
"guild_ids": guild_ids,
38+
}
39+
return CogSubcommandObject(_sub, base, name, subcommand_group)
40+
return wrapper

0 commit comments

Comments
 (0)