Skip to content

Commit 18b6d7f

Browse files
authored
Merge branch 'Pycord-Development:feature/slash' into feature/slash
2 parents ecc32d0 + f18dcab commit 18b6d7f

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
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,

discord/app/commands.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from __future__ import annotations
2626

2727
import asyncio
28+
import types
2829
from discord.types.channel import TextChannel, VoiceChannel
2930
import functools
3031
import inspect
@@ -336,7 +337,6 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
336337
def parse_options(self, params) -> List[Option]:
337338
final_options = []
338339

339-
params = self._get_signature_parameters()
340340
if list(params.items())[0][0] == "self":
341341
temp = list(params.items())
342342
temp.pop(0)
@@ -387,7 +387,10 @@ def parse_options(self, params) -> List[Option]:
387387
return final_options
388388

389389
def _is_typing_union(self, annotation):
390-
return getattr(annotation, "__origin__", None) is Union # type: ignore
390+
return (
391+
getattr(annotation, '__origin__', None) is Union
392+
or type(annotation) is getattr(types, "UnionType", Union)
393+
) # type: ignore
391394

392395
def _is_typing_optional(self, annotation):
393396
return self._is_typing_union(annotation) and type(None) in annotation.__args__ # type: ignore

discord/ext/commands/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import functools
4545
import inspect
4646
import datetime
47+
import types
4748

4849
import discord
4950

@@ -1019,8 +1020,11 @@ def short_doc(self) -> str:
10191020
return ''
10201021

10211022
def _is_typing_optional(self, annotation: Union[T, Optional[T]]) -> TypeGuard[Optional[T]]:
1022-
return getattr(annotation, '__origin__', None) is Union and type(None) in annotation.__args__ # type: ignore
1023-
1023+
return (
1024+
(getattr(annotation, '__origin__', None) is Union
1025+
or type(annotation) is getattr(types, "UnionType", Union))
1026+
and type(None) in annotation.__args__ # type: ignore
1027+
)
10241028
@property
10251029
def signature(self) -> str:
10261030
""":class:`str`: Returns a POSIX-like signature useful for help command output."""

discord/message.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,6 @@ class Message(Hashable):
503503
504504
Returns the message's hash.
505505
506-
.. describe:: len(x)
507-
508-
Returns the length of the message's content.
509506
510507
Attributes
511508
-----------
@@ -716,8 +713,6 @@ def __repr__(self) -> str:
716713
f'<{name} id={self.id} channel={self.channel!r} type={self.type!r} author={self.author!r} flags={self.flags!r}>'
717714
)
718715

719-
def __len__(self):
720-
return len(self.content)
721716

722717
def _try_patch(self, data, key, transform=None) -> None:
723718
try:

0 commit comments

Comments
 (0)