diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ed38b0797c..e0cd7bbdfe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ ci: repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -19,7 +19,7 @@ repos: # - --remove-duplicate-keys # - --remove-unused-variables - repo: https://github.com/asottile/pyupgrade - rev: v3.13.0 + rev: v3.15.0 hooks: - id: pyupgrade args: [--py38-plus] @@ -28,7 +28,7 @@ repos: hooks: - id: isort - repo: https://github.com/psf/black - rev: 23.9.1 + rev: 23.11.0 hooks: - id: black args: [--safe, --quiet] @@ -77,7 +77,7 @@ repos: # - id: mypy - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.3 + rev: v3.1.0 hooks: - id: prettier args: [--prose-wrap=always, --print-width=88] diff --git a/CHANGELOG.md b/CHANGELOG.md index bd7b286bce..0ce874899d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2206](https://github.com/Pycord-Development/pycord/pull/2206)) - Added function `Guild.delete_auto_moderation_rule`. ([#2153](https://github.com/Pycord-Development/pycord/pull/2153)) +- Added `VoiceChannel.slowmode_delay`. + ([#2112](https://github.com/Pycord-Development/pycord/pull/2112)) ### Changed @@ -169,6 +171,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2156](https://github.com/Pycord-Development/pycord/pull/2156)) - Fixed `ScheduledEvent.creator_id` returning `str` instead of `int`. ([#2162](https://github.com/Pycord-Development/pycord/pull/2162)) +- Fixed `_bytes_to_base64_data` not defined. + ([#2185](https://github.com/Pycord-Development/pycord/pull/2185)) - Fixed type-hinting of `values` argument of `basic_autocomplete` to include type-hinting of `Iterable[OptionChoice]`. ([#2164](https://github.com/Pycord-Development/pycord/pull/2164)) @@ -178,6 +182,14 @@ These changes are available on the `master` branch, but have not yet been releas ([#2192](https://github.com/Pycord-Development/pycord/pull/2192)) - Fixed `DMChannel.recipient` being `None` and consequently `User.dm_channel` also being `None`. ([#2219](https://github.com/Pycord-Development/pycord/pull/2219)) +- Fixed ffmpeg being terminated prematurely when piping audio stream. + ([#2240](https://github.com/Pycord-Development/pycord/pull/2240)) +- Fixed tasks looping infinitely when `tzinfo` is neither `None` nor UTC. + ([#2196](https://github.com/Pycord-Development/pycord/pull/2196)) +- Fixed `AttributeError` when running permission checks without the `bot` scope. + ([#2113](https://github.com/Pycord-Development/pycord/issues/2113)) +- Fixed `Option` not working on bridge commands because `ext.commands.Command` doesn't + recognize them. ([#2256](https://github.com/Pycord-Development/pycord/pull/2256)) ## [2.4.1] - 2023-03-20 diff --git a/discord/abc.py b/discord/abc.py index 71a307ab87..a7aba7ecc3 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -712,7 +712,7 @@ def permissions_for(self, obj: Member | Role, /) -> Permissions: return Permissions.all() default = self.guild.default_role - base = Permissions(default.permissions.value) + base = Permissions(default.permissions.value if default else 0) # Handle the role case first if isinstance(obj, Role): diff --git a/discord/channel.py b/discord/channel.py index 6121dd5890..5cd70f52f7 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -1329,6 +1329,7 @@ class VocalGuildChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hasha "user_limit", "_state", "position", + "slowmode_delay", "_overwrites", "category_id", "rtc_region", @@ -1376,6 +1377,7 @@ def _update( data, "last_message_id" ) self.position: int = data.get("position") + self.slowmode_delay = data.get("rate_limit_per_user", 0) self.bitrate: int = data.get("bitrate") self.user_limit: int = data.get("user_limit") self.flags: ChannelFlags = ChannelFlags._from_value(data.get("flags", 0)) @@ -1483,6 +1485,13 @@ class VoiceChannel(discord.abc.Messageable, VocalGuildChannel): The ID of the last message sent to this channel. It may not always point to an existing or valid message. .. versionadded:: 2.0 + slowmode_delay: :class:`int` + The number of seconds a member must wait between sending messages + in this channel. A value of `0` denotes that it is disabled. + Bots and users with :attr:`~Permissions.manage_channels` or + :attr:`~Permissions.manage_messages` bypass slowmode. + + .. versionadded:: 2.5 flags: :class:`ChannelFlags` Extra features of the channel. @@ -1791,6 +1800,7 @@ async def edit( overwrites: Mapping[Role | Member, PermissionOverwrite] = ..., rtc_region: VoiceRegion | None = ..., video_quality_mode: VideoQualityMode = ..., + slowmode_delay: int = ..., reason: str | None = ..., ) -> VoiceChannel | None: ... diff --git a/discord/enums.py b/discord/enums.py index f4b44623b0..f2bb6d3452 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -67,6 +67,7 @@ "AutoModActionType", "AutoModKeywordPresetType", "ApplicationRoleConnectionMetadataType", + "ReactionType", ) @@ -944,6 +945,13 @@ class ApplicationRoleConnectionMetadataType(Enum): boolean_not_equal = 8 +class ReactionType(Enum): + """The reaction type""" + + normal = 0 + burst = 1 + + T = TypeVar("T") diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index d3ce2e07db..4adb27cad0 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -45,6 +45,7 @@ from ...commands import ( ApplicationCommand, + Option, _BaseCommand, message_command, slash_command, @@ -562,7 +563,13 @@ async def dispatch_error(self, ctx: Context, error: Exception) -> None: ctx.bot.dispatch("command_error", ctx, error) async def transform(self, ctx: Context, param: inspect.Parameter) -> Any: - required = param.default is param.empty + if isinstance(param.annotation, Option): + default = param.annotation.default + required = param.annotation.required or default is None + else: + default = param.default + required = default is param.empty + converter = get_converter(param) consume_rest_is_special = ( param.kind == param.KEYWORD_ONLY and not self.rest_is_raw @@ -599,7 +606,7 @@ async def transform(self, ctx: Context, param: inspect.Parameter) -> Any: ): return await converter._construct_default(ctx) raise MissingRequiredArgument(param) - return param.default + return default previous = view.index if consume_rest_is_special: diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 400786d8a4..81d39ab8d1 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -575,7 +575,7 @@ def _get_next_sleep_time(self) -> datetime.datetime: if self._current_loop == 0: # if we're at the last index on the first iteration, we need to sleep until tomorrow return datetime.datetime.combine( - datetime.datetime.now(datetime.timezone.utc) + datetime.datetime.now(self._time[0].tzinfo or datetime.timezone.utc) + datetime.timedelta(days=1), self._time[0], ) @@ -584,18 +584,26 @@ def _get_next_sleep_time(self) -> datetime.datetime: if self._current_loop == 0: self._time_index += 1 - if next_time > datetime.datetime.now(datetime.timezone.utc).timetz(): + if ( + next_time + > datetime.datetime.now( + next_time.tzinfo or datetime.timezone.utc + ).timetz() + ): return datetime.datetime.combine( - datetime.datetime.now(datetime.timezone.utc), next_time + datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc), + next_time, ) else: return datetime.datetime.combine( - datetime.datetime.now(datetime.timezone.utc) + datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc) + datetime.timedelta(days=1), next_time, ) - next_date = cast(datetime.datetime, self._last_iteration) + next_date = cast( + datetime.datetime, self._last_iteration.astimezone(next_time.tzinfo) + ) if next_time < next_date.timetz(): next_date += datetime.timedelta(days=1) diff --git a/discord/guild.py b/discord/guild.py index 5d74590dca..2f449beb60 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -2908,7 +2908,7 @@ async def create_role( if icon is None: fields["icon"] = None else: - fields["icon"] = _bytes_to_base64_data(icon) + fields["icon"] = utils._bytes_to_base64_data(icon) fields["unicode_emoji"] = None if unicode_emoji is not MISSING: diff --git a/discord/http.py b/discord/http.py index 9eb7837d13..40d9ebd928 100644 --- a/discord/http.py +++ b/discord/http.py @@ -757,6 +757,7 @@ def get_reaction_users( emoji: str, limit: int, after: Snowflake | None = None, + type: int | None = None, ) -> Response[list[user.User]]: r = Route( "GET", @@ -771,6 +772,8 @@ def get_reaction_users( } if after: params["after"] = after + if type: + params["type"] = type return self.request(r, params=params) def clear_reactions( diff --git a/discord/interactions.py b/discord/interactions.py index 83ca14f128..19b5363082 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -149,6 +149,8 @@ class Interaction: "custom_id", "_channel_data", "_message_data", + "_guild_data", + "_guild", "_permissions", "_app_permissions", "_state", @@ -188,6 +190,11 @@ def _from_data(self, data: InteractionPayload): self.user: User | Member | None = None self._permissions: int = 0 + self._guild: Guild | None = None + self._guild_data = data.get("guild") + if self.guild is None and self._guild_data: + self._guild = Guild(data=self._guild_data, state=self) + # TODO: there's a potential data loss here if self.guild_id: guild = ( @@ -246,6 +253,8 @@ def client(self) -> Client: @property def guild(self) -> Guild | None: """The guild the interaction was sent from.""" + if self._guild: + return self._guild return self._state and self._state._get_guild(self.guild_id) def is_command(self) -> bool: diff --git a/discord/iterators.py b/discord/iterators.py index 78edb7570d..b171d70ed6 100644 --- a/discord/iterators.py +++ b/discord/iterators.py @@ -182,10 +182,11 @@ async def next(self) -> T: class ReactionIterator(_AsyncIterator[Union["User", "Member"]]): - def __init__(self, message, emoji, limit=100, after=None): + def __init__(self, message, emoji, limit=100, after=None, type=None): self.message = message self.limit = limit self.after = after + self.type = type state = message._state self.getter = state.http.get_reaction_users self.state = state @@ -212,7 +213,12 @@ async def fill_users(self): after = self.after.id if self.after else None data: list[PartialUserPayload] = await self.getter( - self.channel_id, self.message.id, self.emoji, retrieve, after=after + self.channel_id, + self.message.id, + self.emoji, + retrieve, + after=after, + type=self.type, ) if data: diff --git a/discord/player.py b/discord/player.py index 87b0f5718a..0b4dc417c3 100644 --- a/discord/player.py +++ b/discord/player.py @@ -227,7 +227,7 @@ def _pipe_writer(self, source: io.BufferedIOBase) -> None: # arbitrarily large read size data = source.read(8192) if not data: - self._process.terminate() + self._stdin.close() return try: self._stdin.write(data) diff --git a/discord/raw_models.py b/discord/raw_models.py index 1cd88ea0c2..79c8091d98 100644 --- a/discord/raw_models.py +++ b/discord/raw_models.py @@ -29,7 +29,7 @@ from typing import TYPE_CHECKING from .automod import AutoModAction, AutoModTriggerType -from .enums import AuditLogAction, ChannelType, try_enum +from .enums import AuditLogAction, ChannelType, ReactionType, try_enum from .types.user import User if TYPE_CHECKING: @@ -214,6 +214,15 @@ class RawReactionActionEvent(_RawReprMixin): ``REACTION_REMOVE`` for reaction removal. .. versionadded:: 1.3 + burst: :class:`bool` + Whether this reaction is a burst (super) reaction. + burst_colours: Optional[:class:`list`] + A list of hex codes this reaction can be. Only available if `event_type` is `REACTION_ADD` + and this emoji has super reactions available. + burst_colors: Optional[:class:`list`] + Alias for :attr:`burst_colours`. + type: :class:`ReactionType` + The type of reaction added. data: :class:`dict` The raw data sent by the `gateway `_. @@ -226,6 +235,10 @@ class RawReactionActionEvent(_RawReprMixin): "channel_id", "guild_id", "emoji", + "burst", + "burst_colours", + "burst_colors", + "type", "event_type", "member", "data", @@ -240,6 +253,10 @@ def __init__( self.emoji: PartialEmoji = emoji self.event_type: str = event_type self.member: Member | None = None + self.burst: bool = data.get("burst") + self.burst_colours: list = data.get("burst_colors", []) + self.burst_colors: list = self.burst_colours + self.type: ReactionType = try_enum(ReactionType, data.get("type", 0)) try: self.guild_id: int | None = int(data["guild_id"]) @@ -293,18 +310,30 @@ class RawReactionClearEmojiEvent(_RawReprMixin): The guild ID where the reactions got cleared. emoji: :class:`PartialEmoji` The custom or unicode emoji being removed. + burst: :class:`bool` + Whether this reaction was a burst (super) reaction. + burst_colours: :class:`list` + The available HEX codes of the removed super reaction. + burst_colors: Optional[:class:`list`] + Alias for :attr:`burst_colours`. + type: :class:`ReactionType` + The type of reaction removed. data: :class:`dict` The raw data sent by the `gateway `_. .. versionadded:: 2.5 """ - __slots__ = ("message_id", "channel_id", "guild_id", "emoji", "data") + __slots__ = ("message_id", "channel_id", "guild_id", "emoji", "burst", "data") def __init__(self, data: ReactionClearEmojiEvent, emoji: PartialEmoji) -> None: self.emoji: PartialEmoji = emoji self.message_id: int = int(data["message_id"]) self.channel_id: int = int(data["channel_id"]) + self.burst: bool = data.get("burst") + self.burst_colours: list = data.get("burst_colors", []) + self.burst_colors: list = self.burst_colours + self.type: ReactionType = try_enum(ReactionType, data.get("type", 0)) try: self.guild_id: int | None = int(data["guild_id"]) diff --git a/discord/reaction.py b/discord/reaction.py index ea1bcbded9..426b5474ef 100644 --- a/discord/reaction.py +++ b/discord/reaction.py @@ -27,9 +27,11 @@ from typing import TYPE_CHECKING, Any +from .colour import Colour +from .enums import ReactionType from .iterators import ReactionIterator -__all__ = ("Reaction",) +__all__ = ("Reaction", "ReactionCountDetails") if TYPE_CHECKING: from .abc import Snowflake @@ -37,6 +39,7 @@ from .message import Message from .partial_emoji import PartialEmoji from .types.message import Reaction as ReactionPayload + from .types.message import ReactionCountDetails as ReactionCountDetailsPayload class Reaction: @@ -70,14 +73,27 @@ class Reaction: emoji: Union[:class:`Emoji`, :class:`PartialEmoji`, :class:`str`] The reaction emoji. May be a custom emoji, or a unicode emoji. count: :class:`int` - Number of times this reaction was made + The combined total of normal and super reactions for this emoji. me: :class:`bool` - If the user sent this reaction. + If the user sent this as a normal reaction. + me_burst: :class:`bool` + If the user sent this as a super reaction. message: :class:`Message` Message this reaction is for. + burst: :class:`bool` + Whether this reaction is a burst (super) reaction. """ - __slots__ = ("message", "count", "emoji", "me") + __slots__ = ( + "message", + "count", + "emoji", + "me", + "burst", + "me_burst", + "_count_details", + "_burst_colours", + ) def __init__( self, @@ -91,7 +107,35 @@ def __init__( emoji or message._state.get_reaction_emoji(data["emoji"]) ) self.count: int = data.get("count", 1) + self._count_details: ReactionCountDetailsPayload = data.get("count_details", {}) self.me: bool = data.get("me") + self.burst: bool = data.get("burst") + self.me_burst: bool = data.get("me_burst") + self._burst_colours: list[Colour] = data.get("burst_colors", []) + + @property + def burst_colours(self) -> list[Colour]: + """Returns a list possible :class:`Colour` this super reaction can be. + + There is an alias for this named :attr:`burst_colors`. + """ + + # We recieve a list of #FFFFFF, so omit the # and convert to base 16 + return [Colour(int(c[1:], 16)) for c in self._burst_colours] + + @property + def burst_colors(self) -> list[Colour]: + """Returns a list possible :class:`Colour` this super reaction can be. + + There is an alias for this named :attr:`burst_colours`. + """ + + return self.burst_colours + + @property + def count_details(self): + """Returns :class:`ReactionCountDetails` for the individual counts of normal and super reactions made.""" + return ReactionCountDetails(self._count_details) # TODO: typeguard def is_custom_emoji(self) -> bool: @@ -166,7 +210,11 @@ async def clear(self) -> None: await self.message.clear_reaction(self.emoji) def users( - self, *, limit: int | None = None, after: Snowflake | None = None + self, + *, + limit: int | None = None, + after: Snowflake | None = None, + type: ReactionType | None = None, ) -> ReactionIterator: """Returns an :class:`AsyncIterator` representing the users that have reacted to the message. @@ -181,6 +229,8 @@ def users( reacted to the message. after: Optional[:class:`abc.Snowflake`] For pagination, reactions are sorted by member. + type: Optional[:class:`ReactionType`] + The type of reaction to get users for. Defaults to `normal`. Yields ------ @@ -210,6 +260,10 @@ def users( # users is now a list of User... winner = random.choice(users) await channel.send(f'{winner} has won the raffle.') + + Getting super reactors: :: + + users = await reaction.users(type=ReactionType.burst).flatten() """ if not isinstance(self.emoji, str): @@ -220,4 +274,23 @@ def users( if limit is None: limit = self.count - return ReactionIterator(self.message, emoji, limit, after) + if isinstance(type, ReactionType): + type = type.value + + return ReactionIterator(self.message, emoji, limit, after, type) + + +class ReactionCountDetails: + """Represents a breakdown of the normal and burst reaction counts for the emoji. + + Attributes + ---------- + normal: :class:`int` + The number of normal reactions for this emoji. + burst: :class:`bool` + The number of super reactions for this emoji. + """ + + def __init__(self, data: ReactionCountDetailsPayload): + self.normal = data.get("normal", 0) + self.burst = data.get("burst", 0) diff --git a/discord/types/message.py b/discord/types/message.py index b141531e9a..93e99ba7ab 100644 --- a/discord/types/message.py +++ b/discord/types/message.py @@ -54,6 +54,12 @@ class Reaction(TypedDict): count: int me: bool emoji: PartialEmoji + burst: bool + + +class ReactionCountDetails(TypedDict): + normal: int + burst: int class Attachment(TypedDict): diff --git a/discord/types/raw_models.py b/discord/types/raw_models.py index 453e75cf17..44ac45363d 100644 --- a/discord/types/raw_models.py +++ b/discord/types/raw_models.py @@ -62,6 +62,9 @@ class ReactionActionEvent(_ReactionEventOptional): channel_id: Snowflake message_id: Snowflake emoji: PartialEmoji + burst: bool + burst_colors: list + type: int class ReactionClearEvent(_ReactionEventOptional): @@ -73,6 +76,9 @@ class ReactionClearEmojiEvent(_ReactionEventOptional): channel_id: int message_id: int emoji: PartialEmoji + burst: bool + burst_colors: list + type: int class IntegrationDeleteEvent(TypedDict): diff --git a/docs/api/enums.rst b/docs/api/enums.rst index e2620ed8e5..45317a37ce 100644 --- a/docs/api/enums.rst +++ b/docs/api/enums.rst @@ -2292,3 +2292,17 @@ of :class:`enum.Enum`. .. attribute:: slurs Represents the slurs keyword preset rule. + +.. class:: ReactionType + + Represents a Reaction's type. + + .. versionadded:: 2.5 + + .. attribute:: normal + + Represents a normal reaction. + + .. attribute:: burst + + Represents a super reaction. diff --git a/docs/api/models.rst b/docs/api/models.rst index 909853a769..4d11fed63e 100644 --- a/docs/api/models.rst +++ b/docs/api/models.rst @@ -98,6 +98,9 @@ Messages .. automethod:: users :async-for: +.. autoclass:: ReactionCountDetails() + :members: + Guild ----- diff --git a/requirements/_.txt b/requirements/_.txt index 26e696d0f2..2488d446e6 100644 --- a/requirements/_.txt +++ b/requirements/_.txt @@ -1,2 +1,2 @@ -aiohttp>=3.6.0,<3.9.0 +aiohttp>=3.6.0,<3.10.0 typing_extensions>=4,<5; python_version < "3.11" diff --git a/requirements/dev.txt b/requirements/dev.txt index 93998f2d6d..bb28382928 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,11 +1,11 @@ -r _.txt pylint~=2.17.6 -pytest~=7.4.2 +pytest~=7.4.3 pytest-asyncio~=0.21.1 # pytest-order~=1.0.1 -mypy~=1.5.1 +mypy~=1.7.1 coverage~=7.3 -pre-commit==3.4.0 -codespell==2.2.5 +pre-commit==3.5.0 +codespell==2.2.6 bandit==1.7.5 flake8==6.1.0 diff --git a/requirements/docs.txt b/requirements/docs.txt index 453c4fcf7b..2c8cb6ede3 100644 --- a/requirements/docs.txt +++ b/requirements/docs.txt @@ -2,7 +2,7 @@ sphinx==5.3.0 sphinxcontrib_trio==1.1.2 sphinxcontrib-websupport==1.2.4 myst-parser==1.0.0 -sphinxext-opengraph==0.8.2 +sphinxext-opengraph==0.9.0 sphinx-copybutton==0.5.2 furo@ git+https://github.com/pradyunsg/furo@193643f sphinx-autodoc-typehints==1.23.0 diff --git a/requirements/speed.txt b/requirements/speed.txt index 32c37acb7a..5664d336cf 100644 --- a/requirements/speed.txt +++ b/requirements/speed.txt @@ -1,2 +1,2 @@ -msgspec~=0.18.0 +msgspec~=0.18.4 aiohttp[speedups]