Skip to content

Commit 245180a

Browse files
committed
Add ctx.slash, include type hinting + refine documentation.
1 parent 7e5d781 commit 245180a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

discord_slash/context.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import typing
5+
from typing import TYPE_CHECKING
36
from warnings import warn
47

58
import discord
@@ -9,6 +12,9 @@
912
from . import error, http, model
1013
from .dpy_overrides import ComponentMessage
1114

15+
if TYPE_CHECKING: # circular import sucks for typehinting
16+
from . import client
17+
1218

1319
class InteractionContext:
1420
"""
@@ -115,18 +121,21 @@ def channel(self) -> typing.Optional[typing.Union[discord.TextChannel, discord.D
115121
return self.bot.get_channel(self.channel_id)
116122

117123
@property
118-
def voice_client(self):
124+
def voice_client(self) -> typing.Optional[discord.VoiceProtocol]:
119125
"""
120126
VoiceClient instance of the command invoke. If the command was invoked in DM, then it is ``None``.
121127
If the bot is not connected to any Voice/Stage channels, then it is ``None``.
122128
129+
:return: Optional[discord.VoiceProtocol]
123130
"""
124131
return self.guild.voice_client if self.guild else None
125132

126133
@property
127-
def me(self):
134+
def me(self) -> typing.Union[discord.Member, discord.ClientUser]:
128135
"""
129136
Similar to :attr:`.Guild.me` except that it may also return the bot user in DM context.
137+
138+
:return: Union[discord.Member, discord.ClientUser]
130139
"""
131140
return self.guild.me if self.guild is not None else self.bot.user
132141

@@ -359,13 +368,24 @@ def __init__(
359368

360369
super().__init__(_http=_http, _json=_json, _discord=_discord, logger=logger)
361370

371+
@property
372+
def slash(self) -> client.SlashCommand:
373+
"""
374+
Returns the associated SlashCommand object created during Runtime.
375+
376+
:return: client.SlashCommand
377+
"""
378+
return self.bot.slash # noqa
379+
362380
@property
363381
def cog(self) -> typing.Optional[commands.Cog]:
364382
"""
365383
Returns the cog associated with the command invoked, if any.
384+
385+
:return: Optional[commands.Cog]
366386
"""
367387

368-
cmd_obj = self.bot.slash.commands[self.command] # noqa
388+
cmd_obj = self.slash.commands[self.command]
369389

370390
if isinstance(cmd_obj, (model.CogBaseCommandObject, model.CogSubcommandObject)):
371391
return cmd_obj.cog

0 commit comments

Comments
 (0)