From 017e4adb39bd04d92781f27c6d633c95dc7cd205 Mon Sep 17 00:00:00 2001 From: David Hozic Date: Mon, 14 Aug 2023 01:21:24 +0200 Subject: [PATCH 1/4] feat: VoiceClient.play wait_finish parameter for awaiting end of stream (#2194) * wait_end * style(pre-commit): auto fixes from pre-commit.com hooks * Changelog * style(pre-commit): auto fixes from pre-commit.com hooks * wait_finish * Changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil --- CHANGELOG.md | 2 ++ discord/voice_client.py | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceeaf33723..e2e3ef75f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,8 @@ These changes are available on the `master` branch, but have not yet been releas - Added `suppress` and `allowed_mentions` parameters to `Webhook` and `InteractionResponse` edit methods. ([#2138](https://github.com/Pycord-Development/pycord/pull/2138)) +- Added `wait_finish` parameter to `VoiceClient.play` for awaiting the end of a play. + ([#2194](https://github.com/Pycord-Development/pycord/pull/2194)) ### Changed diff --git a/discord/voice_client.py b/discord/voice_client.py index f1e16cf812..437311122a 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -624,7 +624,11 @@ def get_ssrc(self, user_id): ] def play( - self, source: AudioSource, *, after: Callable[[Exception | None], Any] = None + self, + source: AudioSource, + *, + after: Callable[[Exception | None], Any] = None, + wait_finish: bool = False, ) -> None: """Plays an :class:`AudioSource`. @@ -643,6 +647,14 @@ def play( The finalizer that is called after the stream is exhausted. This function must have a single parameter, ``error``, that denotes an optional exception that was raised during playing. + wait_finish: bool + If True, an awaitable will be returned, which can be used to wait for + audio to stop playing. This awaitable will return an exception if raised, + or None when no exception is raised. + + If False, None is returned and the function does not block. + + .. versionadded:: v2.5 Raises ------ @@ -668,8 +680,22 @@ def play( if not self.encoder and not source.is_opus(): self.encoder = opus.Encoder() + future = None + if wait_finish: + future = asyncio.Future() + after_callback = after + + def _after(exc: Exception | None): + if callable(after_callback): + after_callback(exc) + + future.set_result(exc) + + after = _after + self._player = AudioPlayer(source, self, after=after) self._player.start() + return future def unpack_audio(self, data): """Takes an audio packet received from Discord and decodes it into pcm audio data. From a26117691c4aa0330d59d78ae32d5cf765dadc4d Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 18 Aug 2023 14:39:31 +0300 Subject: [PATCH 2/4] feat: add support for custom status (#2206) --- CHANGELOG.md | 2 ++ discord/activity.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2e3ef75f9..45f8d1e2fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2138](https://github.com/Pycord-Development/pycord/pull/2138)) - Added `wait_finish` parameter to `VoiceClient.play` for awaiting the end of a play. ([#2194](https://github.com/Pycord-Development/pycord/pull/2194)) +- Added support for custom bot status. + ([#2206](https://github.com/Pycord-Development/pycord/pull/2206)) ### Changed diff --git a/discord/activity.py b/discord/activity.py index a00add5cd8..96b20255a8 100644 --- a/discord/activity.py +++ b/discord/activity.py @@ -160,7 +160,7 @@ class Activity(BaseActivity): type: :class:`ActivityType` The type of activity currently being done. state: Optional[:class:`str`] - The user's current state. For example, "In Game". + The user's current party status or text used for a custom status. details: Optional[:class:`str`] The detail of the user's current activity. timestamps: Dict[:class:`str`, :class:`int`] @@ -229,7 +229,6 @@ def __init__(self, **kwargs): self.assets: ActivityAssets = kwargs.pop("assets", {}) self.party: ActivityParty = kwargs.pop("party", {}) self.application_id: int | None = _get_as_snowflake(kwargs, "application_id") - self.name: str | None = kwargs.pop("name", None) self.url: str | None = kwargs.pop("url", None) self.flags: int = kwargs.pop("flags", 0) self.sync_id: str | None = kwargs.pop("sync_id", None) @@ -242,6 +241,9 @@ def __init__(self, **kwargs): if isinstance(activity_type, ActivityType) else try_enum(ActivityType, activity_type) ) + self.name: str | None = kwargs.pop( + "name", "Custom Status" if self.type == ActivityType.custom else None + ) emoji = kwargs.pop("emoji", None) self.emoji: PartialEmoji | None = ( @@ -252,6 +254,7 @@ def __repr__(self) -> str: attrs = ( ("type", self.type), ("name", self.name), + ("state", self.state), ("url", self.url), ("details", self.details), ("application_id", self.application_id), @@ -760,6 +763,8 @@ class CustomActivity(BaseActivity): The custom activity's name. emoji: Optional[:class:`PartialEmoji`] The emoji to pass to the activity, if any. + state: Optional[:class:`str`] + The text used for the custom activity. """ __slots__ = ("name", "emoji", "state") @@ -769,7 +774,7 @@ def __init__( ): super().__init__(**extra) self.name: str | None = name - self.state: str | None = extra.pop("state", None) + self.state: str | None = extra.pop("state", name) if self.name == "Custom Status": self.name = self.state From a3bd2a04fbd7ac0cec5a119cc9b360965aaaab8e Mon Sep 17 00:00:00 2001 From: Marc13 <83296140+llamaair@users.noreply.github.com> Date: Tue, 29 Aug 2023 03:37:00 +0200 Subject: [PATCH 3/4] feat: Add `Guild.delete_auto_moderation_rule` (#2153) * Update guild.py * Update guild.py * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md * Update discord/guild.py Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Signed-off-by: Marc13 <83296140+llamaair@users.noreply.github.com> * Update guild.py * Apply suggestions from code review Signed-off-by: Lala Sabathil * Update discord/guild.py Signed-off-by: Lala Sabathil * Apply suggestions from code review Signed-off-by: Lala Sabathil * Apply suggestions from code review Signed-off-by: Lala Sabathil * Apply suggestions from reviews * Apply suggestions from code review --------- Signed-off-by: Marc13 <83296140+llamaair@users.noreply.github.com> Signed-off-by: Lala Sabathil Signed-off-by: Lala Sabathil Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Co-authored-by: Lala Sabathil Co-authored-by: Lala Sabathil Co-authored-by: plun1331 --- CHANGELOG.md | 2 ++ discord/guild.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45f8d1e2fc..d0fca146d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2194](https://github.com/Pycord-Development/pycord/pull/2194)) - Added support for custom bot status. ([#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)) ### Changed diff --git a/discord/guild.py b/discord/guild.py index 2bd77b2dd5..5d74590dca 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -3816,3 +3816,29 @@ async def create_auto_moderation_rule( self.id, payload, reason=reason ) return AutoModRule(state=self._state, data=data) + + async def delete_auto_moderation_rule( + self, + id: int, + *, + reason: str | None = None, + ) -> None: + """ + Deletes an auto moderation rule. + + Parameters + ---------- + id: :class:`int` + The ID of the auto moderation rule. + reason: Optional[:class:`str`] + The reason for deleting the rule. Shows up in the audit log. + + Raises + ------ + HTTPException + Deleting the auto moderation rule failed. + Forbidden + You do not have the Manage Guild permission. + """ + + await self._state.http.delete_auto_moderation_rule(self.id, id, reason=reason) From 402f2bd3fb1bde2cc4b06c6d76188893bda4a9fa Mon Sep 17 00:00:00 2001 From: David Hozic Date: Tue, 29 Aug 2023 14:12:10 +0200 Subject: [PATCH 4/4] fix: DMChannel.recipient and User.dm_channel being None after calling User.create_dm (#2219) * Fix self.recipient * style(pre-commit): auto fixes from pre-commit.com hooks * CHANGELOG --------- Signed-off-by: David Hozic Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CHANGELOG.md | 2 ++ discord/channel.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0fca146d8..3674d773d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -168,6 +168,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2191](https://github.com/Pycord-Development/pycord/pull/2191)) - Fixed a misplaced payload object inside of the thread creation payload. ([#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)) ## [2.4.1] - 2023-03-20 diff --git a/discord/channel.py b/discord/channel.py index 076b2c704f..6121dd5890 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -2817,7 +2817,7 @@ def __init__( self._state: ConnectionState = state self.recipient: User | None = None if r := data.get("recipients"): - self.recipient: state.store_user(r[0]) + self.recipient = state.store_user(r[0]) self.me: ClientUser = me self.id: int = int(data["id"])