Skip to content

Commit 0d1b9a4

Browse files
authored
Merge pull request #245 from Pycord-Development/revert-243-revert-183-patch-4
Revert "Revert "Add `can_send` method to Messageable""
2 parents 8cc0a7a + b5421ad commit 0d1b9a4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

discord/abc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,59 @@ async def pins(self) -> List[Message]:
15291529
state = self._state
15301530
data = await state.http.pins_from(channel.id)
15311531
return [state.create_message(channel=channel, data=m) for m in data]
1532+
1533+
def can_send(self, *objects) -> bool:
1534+
"""Returns a :class:`bool` indicating whether you have the permissions to send the object(s).
1535+
1536+
Raises
1537+
------
1538+
TypeError
1539+
An invalid type has been passed.
1540+
1541+
Returns
1542+
--------
1543+
:class:`bool`
1544+
Indicates whether you have the permissions to send the object(s).
1545+
"""
1546+
mapping = {
1547+
'Message': 'send_messages',
1548+
'Embed': 'embed_links',
1549+
'File': 'attach_files',
1550+
'Emoji': 'use_external_emojis',
1551+
'GuildSticker': 'use_external_stickers',
1552+
}
1553+
# Can't use channel = await self._get_channel() since its async
1554+
if hasattr(self, 'permissions_for'):
1555+
channel = self
1556+
elif hasattr(self, 'channel') and not type(self.channel).__name__ in ('DMChannel', 'GroupChannel'):
1557+
channel = self.channel
1558+
else:
1559+
return True # Permissions don't exist for User DMs
1560+
1561+
1562+
objects = (None, ) + objects # Makes sure we check for send_messages first
1563+
1564+
for obj in objects:
1565+
try:
1566+
if obj is None:
1567+
permission = mapping['Message']
1568+
else:
1569+
permission = mapping.get(type(obj).__name__) or mapping[obj.__name__]
1570+
1571+
if type(obj).__name__ == 'Emoji':
1572+
if obj._to_partial().is_unicode_emoji or obj.guild_id == channel.guild.id:
1573+
continue
1574+
elif type(obj).__name__ == 'GuildSticker':
1575+
if obj.guild_id == channel.guild.id:
1576+
continue
1577+
1578+
except (KeyError, AttributeError):
1579+
raise TypeError(f'The object {obj} is of an invalid type.')
1580+
1581+
if not getattr(channel.permissions_for(channel.guild.me), permission):
1582+
return False
1583+
1584+
return True
15321585

15331586
def history(
15341587
self,

0 commit comments

Comments
 (0)