Skip to content

Commit 3215821

Browse files
committed
Rename client.SlashCommand http -> req, Updated docs
1 parent 750e41d commit 3215821

19 files changed

+261
-64
lines changed

discord_slash/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
from .model import SlashContext
1313
from .utils import manage_commands
1414

15-
__version__ = "1.0.0"
15+
__version__ = "1.0.1"

discord_slash/client.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class SlashCommand:
1212
1313
:param client: discord.py Bot class. Although it accepts :class:`discord.Client` at init, using it is not allowed since :class:`discord.Client` doesn't support :func:`add_listener`.
1414
:type client: Union[discord.Client, discord.ext.commands.Bot]
15+
16+
:ivar _discord: Discord client of this client.
17+
:ivar commands: Dictionary of the registered commands via :func:`.slash` decorator.
18+
:ivar req: :class:`.http.SlashCommandRequest` of this client.
19+
:ivar logger: Logger of this client.
1520
"""
1621
def __init__(self,
1722
client: typing.Union[discord.Client,
@@ -21,18 +26,38 @@ def __init__(self,
2126
raise Exception("Currently only commands.Bot is supported.")
2227
self._discord = client
2328
self.commands = {}
24-
self.http = http.SlashCommandRequest()
29+
self.req = http.SlashCommandRequest()
2530
self.logger = logging.getLogger("discord_slash")
2631
self._discord.add_listener(self.on_socket_response)
2732

28-
def slash(self, name=None):
33+
def slash(self, name=None, auto_convert: dict = None):
2934
"""
3035
Decorator that registers coroutine as a slash command.\n
31-
1 arg is required for ctx(:func:`discord_slash.model.SlashContext`), and if your slash command has some args, then those args are also required.\n
32-
All args are passed in order.\n
33-
Note that Role, User, and Channel types are passed as id, since API doesn't give type of the option for now.
36+
1 arg is required for ctx(:class:`.model.SlashContext`), and if your slash command has some args, then those args are also required.\n
37+
All args are passed in order.
38+
39+
.. note::
40+
Role, User, and Channel types are passed as id, since API doesn't give type of the option for now.
41+
42+
.. warning::
43+
Unlike discord.py's command, ``*args``, keyword-only args, converters, etc. are NOT supported.
44+
45+
Example:
46+
47+
.. code-block:: python
48+
49+
@slash.slash(name="ping")
50+
async def _slash(ctx): # Normal usage.
51+
await ctx.send(text=f"Pong! (`{round(bot.latency*1000)}`ms)")
52+
53+
54+
@slash.slash(name="pick")
55+
async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
56+
await ctx.send(text=str(random.choice([choice1, choice2])))
3457
3558
:param name: Name of the slash command.
59+
:param auto_convert: Not implemented.
60+
:type auto_convert: dict
3661
"""
3762
def wrapper(cmd):
3863
self.commands[cmd.__name__ if not name else name] = cmd
@@ -57,8 +82,10 @@ def process_options(self, options: dict) -> list:
5782

5883
async def on_socket_response(self, msg):
5984
"""
60-
This event listener is automatically registered at initialization of this class.\n
61-
DO NOT MANUALLY REGISTER, OVERRIDE, OR WHATEVER ACTION TO THIS COROUTINE UNLESS YOU KNOW WHAT YOU ARE DOING.
85+
This event listener is automatically registered at initialization of this class.
86+
87+
.. warning::
88+
DO NOT MANUALLY REGISTER, OVERRIDE, OR WHATEVER ACTION TO THIS COROUTINE UNLESS YOU KNOW WHAT YOU ARE DOING.
6289
6390
:param msg: Gateway message.
6491
"""
@@ -67,5 +94,5 @@ async def on_socket_response(self, msg):
6794
to_use = msg["d"]
6895
if to_use["data"]["name"] in self.commands.keys():
6996
args = [x["value"] for x in to_use["data"]["options"]] if "options" in to_use["data"] else []
70-
ctx = model.SlashContext(self.http, to_use, self._discord)
97+
ctx = model.SlashContext(self.req, to_use, self._discord)
7198
await self.commands[to_use["data"]["name"]](ctx, *args)

discord_slash/error.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class SlashCommandError(Exception):
99
class RequestFailure(SlashCommandError):
1010
"""
1111
Request to Discord API has failed.
12+
13+
:ivar status: Status code of failed response.
14+
:ivar msg: Message of failed response.
1215
"""
1316
def __init__(self, status: int, msg: str):
1417
self.status = status

discord_slash/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def post(self, _resp, _id, token):
1515
:param _id: Command message id.
1616
:param token: Command message token.
1717
:return: True if succeeded.
18-
:raises: :class:`error.RequestFailure` - Requesting to API has failed.
18+
:raises: :class:`.error.RequestFailure` - Requesting to API has failed.
1919
"""
2020
req_url = f"https://discord.com/api/v8/interactions/{_id}/{token}/callback"
2121
async with aiohttp.ClientSession() as session:

discord_slash/model.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ class SlashContext:
88
"""
99
Context of the slash command.\n
1010
Kinda similar with discord.ext.commands.Context.
11+
12+
:ivar name: Name of the command.
13+
:ivar id: ID of the command message.
14+
:ivar command_id: ID of the command.
15+
:ivar _http: :class:`.http.SlashCommandRequest` of the client.
16+
:ivar guild: :class:`discord.Guild` instance of the command message.
17+
:ivar author: :class:`discord.Member` instance representing author of the command message.
18+
:ivar channel: :class:`discord.TextChannel` instance representing channel of the command message.
1119
"""
1220
def __init__(self,
1321
_http: http.SlashCommandRequest,
Binary file not shown.
1.44 KB
Binary file not shown.
8 Bytes
Binary file not shown.
5.35 KB
Binary file not shown.
1.59 MB
Binary file not shown.

0 commit comments

Comments
 (0)