Skip to content
This repository was archived by the owner on Aug 29, 2021. It is now read-only.

Commit 52154c0

Browse files
committed
[v0.5.1@context] Handle invalid types in is_bot/_user_above better
1 parent 0dc9ac4 commit 52154c0

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

disctools/context.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import discord
2828
from discord.ext.commands import Context as _Context
29-
from discord.member import Member
3029

3130
T = TypeVar('T', bound=discord.abc.User)
3231

@@ -92,7 +91,7 @@ def _above_check(self, user: discord.Member,
9291
if self.guild is None:
9392
raise ValueError(f"Expected discord.Guild instance at {self.__class__.__qualname__}.guild instead got None")
9493

95-
targets = cast(Sequence[Member], self.targets)
94+
targets = cast(Sequence[discord.Member], self.targets) # type: ignore[redundant-cast]
9695

9796
if user == self.guild.owner:
9897
return True, None
@@ -128,7 +127,7 @@ def is_author_above(self,
128127
Raises
129128
------
130129
:exc:`TypeError`
131-
users argument is not of specified type.
130+
users argument is not of specified type or the attribute :attr:`_Context.author` is not a :class:`discord.Member` instance.
132131
:exc:`ValueError`
133132
guild attribute is None.
134133
@@ -140,7 +139,9 @@ def is_author_above(self,
140139
first element will be False and second element will be the first user who is above the author.
141140
Example output: ``(True, None)``, ``(False, <discord.Member Object>)``.
142141
"""
143-
return self._above_check(self.author, users)
142+
if isinstance(self.author, discord.Member):
143+
return self._above_check(self.author, users)
144+
raise TypeError(f"Message author is of type {type(self.author)}, expected discord.Member instance.")
144145

145146
def is_bot_above(self,
146147
users: Optional[MemberTargets] = None
@@ -155,7 +156,7 @@ def is_bot_above(self,
155156
Raises
156157
------
157158
:exc:`TypeError`
158-
users argument is not of specified type.
159+
users argument is not of specified type or the attribute :attr:`_Context.me` is not a :class:`discord.Member` instance.
159160
:exc:`ValueError`
160161
guild attribute is None.
161162
@@ -164,7 +165,9 @@ def is_bot_above(self,
164165
Tuple[:class:`bool`, Optional[:class:`discord.Member`]]
165166
Same as :meth:`TargetContext.is_author_above`. Only that the comparisn is with Bot.
166167
"""
167-
return self._above_check(self.me, users)
168+
if isinstance(self.me, discord.Member):
169+
return self._above_check(self.me, users)
170+
raise TypeError(f"{self.__class__.__qualname__}.me is of type {type(self.me)}, expected discord.Member instance.")
168171

169172
async def whisper(self, users: Optional[Union[Sequence[discord.User], discord.User]] = None,
170173
*args, **kwargs) -> None:
@@ -187,7 +190,7 @@ async def whisper(self, users: Optional[Union[Sequence[discord.User], discord.Us
187190
if users is None:
188191
# This could be a Member, but that doesn't matter
189192
# since Member virtually inherits User
190-
users = cast(Sequence[discord.User], self.targets)
193+
users = cast(Sequence[discord.User], self.targets) # type: ignore[redundant-cast]
191194
users = _maybe_sequence(users)
192195
for target in users:
193196
target.send(*args, **kwargs)

0 commit comments

Comments
 (0)