Skip to content

Commit 1d22e56

Browse files
committed
Restructured SlashContext's data objects
1 parent cffa3bc commit 1d22e56

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

discord_slash/context.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class SlashContext:
2626
:ivar bot: discord.py client.
2727
:ivar logger: Logger instance.
2828
:ivar sent: Whether you sent the initial response.
29-
:ivar guild: :class:`discord.Guild` instance or guild ID of the command message.
30-
:ivar author: :class:`discord.Member` instance or user ID representing author of the command message.
31-
:ivar channel: :class:`discord.TextChannel` instance or channel ID representing channel of the command message.
29+
:ivar guild_id: Guild ID of the command message. If the command was invoked in DM, then it is ``None``
30+
:ivar author_id: User ID representing author of the command message.
31+
:ivar channel_id: Channel ID representing channel of the command message.
3232
"""
3333

3434
def __init__(self,
@@ -47,18 +47,37 @@ def __init__(self,
4747
self.bot = _discord
4848
self.logger = logger
4949
self.sent = False
50-
self.guild: typing.Union[discord.Guild, int] = _discord.get_guild(int(_json["guild_id"])) if "guild_id" in _json.keys() else None
51-
self.author: typing.Union[discord.Member, int] = self.guild.get_member(int(_json["member"]["user"]["id"])) \
52-
if self.guild and "member" in _json.keys() else self.bot.get_user(int(_json["user"]["id"])) if self.guild else None
53-
self.channel: typing.Union[discord.TextChannel, int] = self.guild.get_channel(int(_json["channel_id"])) \
54-
if self.guild else None
55-
if not self.author and ("member" in _json.keys() or "user" in _json.keys()):
56-
self.author = int(_json["member"]["user"]["id"] if "member" in _json.keys() else _json["user"]["id"])
57-
if not self.channel:
58-
self.channel = int(_json["channel_id"])
59-
if not self.guild and "guild_id" in _json.keys():
60-
# Should be set after every others are set.
61-
self.guild = int(_json["guild_id"])
50+
self.guild_id = int(_json["guild_id"]) if "guild_id" in _json.keys() else None
51+
self.author_id = int(_json["member"]["user"]["id"] if "member" in _json.keys() else _json["user"]["id"])
52+
self.channel_id = int(_json["channel_id"])
53+
54+
@property
55+
def guild(self) -> typing.Optional[discord.Guild]:
56+
"""
57+
Guild instance of the command invoke. If the command was invoked in DM, then it is ``None``
58+
59+
:return: Optional[discord.Guild]
60+
"""
61+
return self.bot.get_guild(self.guild_id) if self.guild_id else None
62+
63+
@property
64+
def author(self) -> typing.Optional[typing.Union[discord.Member, discord.User]]:
65+
"""
66+
User or Member instance of the command invoke.
67+
68+
:return: Optional[Union[discord.Member, discord.User]]
69+
"""
70+
guild = self.guild
71+
return guild.get_member(self.author_id) if guild else self.bot.get_user(self.author_id)
72+
73+
@property
74+
def channel(self) -> typing.Optional[typing.Union[discord.abc.GuildChannel, discord.abc.PrivateChannel]]:
75+
"""
76+
Channel instance of the command invoke.
77+
78+
:return: Optional[Union[discord.abc.GuildChannel, discord.abc.PrivateChannel]]
79+
"""
80+
return self.bot.get_channel(self.channel_id)
6281

6382
async def respond(self, eat: bool = False):
6483
"""
@@ -76,9 +95,9 @@ async def respond(self, eat: bool = False):
7695
if not eat:
7796
with suppress(asyncio.TimeoutError):
7897
def check(message: discord.Message):
79-
user_id = self.author if isinstance(self.author, int) else self.author.id
98+
user_id = self.author_id
8099
is_author = message.author.id == user_id
81-
channel_id = self.channel if isinstance(self.channel, int) else self.channel.id
100+
channel_id = self.channel_id
82101
is_channel = channel_id == message.channel.id
83102
is_user_input = message.type == 20
84103
is_correct_command = message.content.startswith(f"</{self.name}:{self.command_id}>")
@@ -168,7 +187,7 @@ async def send(self,
168187
resp = await self._http.post(base, wait, self.interaction_id, self.__token, files=files)
169188
smsg = model.SlashMessage(state=self.bot._connection,
170189
data=resp,
171-
channel=self.channel if isinstance(self.channel, discord.TextChannel) else discord.Object(id=self.channel),
190+
channel=self.channel or discord.Object(id=self.channel),
172191
_http=self._http,
173192
interaction_token=self.__token)
174193
if delete_after:

0 commit comments

Comments
 (0)