diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index d64c44f2a5..ed9651b025 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -9,7 +9,7 @@ jobs: codespell: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: "3.11" @@ -25,7 +25,7 @@ jobs: bandit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: "3.11" diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 63eb6a6ff7..65da74cfb0 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -41,7 +41,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3.1.0 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index eebcd0b41e..35cb6d490d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -9,7 +9,7 @@ jobs: pylint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: "3.11" @@ -31,7 +31,7 @@ jobs: mypy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: "3.11" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ad535184a..9a2b03aa55 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,7 +21,7 @@ jobs: OS: ${{ matrix.os }} PYTHON: ${{ matrix.python-version }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} @@ -48,7 +48,7 @@ jobs: docs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3.1.0 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: diff --git a/.github/workflows/todo.yml b/.github/workflows/todo.yml index fec76a1f57..508f34218f 100644 --- a/.github/workflows/todo.yml +++ b/.github/workflows/todo.yml @@ -4,7 +4,7 @@ jobs: todo: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Run tdg-github-action uses: ribtoks/tdg-github-action@v0.4.7-beta with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 43519b50cd..ed38b0797c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: trailing-whitespace - id: end-of-file-fixer - repo: https://github.com/PyCQA/autoflake - rev: v2.2.0 + rev: v2.2.1 hooks: - id: autoflake # args: @@ -19,7 +19,7 @@ repos: # - --remove-duplicate-keys # - --remove-unused-variables - repo: https://github.com/asottile/pyupgrade - rev: v3.9.0 + rev: v3.13.0 hooks: - id: pyupgrade args: [--py38-plus] @@ -28,7 +28,7 @@ repos: hooks: - id: isort - repo: https://github.com/psf/black - rev: 23.7.0 + rev: 23.9.1 hooks: - id: black args: [--safe, --quiet] @@ -77,7 +77,7 @@ repos: # - id: mypy - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.0 + rev: v3.0.3 hooks: - id: prettier args: [--prose-wrap=always, --print-width=88] diff --git a/CHANGELOG.md b/CHANGELOG.md index a67f2fc4f2..62726ba3b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,9 +14,6 @@ These changes are available on the `master` branch, but have not yet been releas - Added possibility to start bot via async context manager. ([#1801](https://github.com/Pycord-Development/pycord/pull/1801)) -- Change default for all `name_localizations` & `description_localizations` attributes - from being `None` to be `MISSING`. - ([#1866](https://github.com/Pycord-Development/pycord/pull/1866)) - Added new parameters (`author`, `footer`, `image`, `thumbnail`) to `discord.Embed`. ([#1996](https://github.com/Pycord-Development/pycord/pull/1996)) - Added new events `on_bridge_command`, `on_bridge_command_completion`, and @@ -28,7 +25,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1936](https://github.com/Pycord-Development/pycord/pull/1936)) - Added support for one-time event listeners in `@client.listen()`. ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) -- Added `current_page` argument to Paginator.update(). +- Added `current_page` argument to `Paginator.update()`. ([#1983](https://github.com/Pycord-Development/pycord/pull/1983)) - Added new `application_auto_moderation_rule_create_badge` to `ApplicationFlags`. ([#1992](https://github.com/Pycord-Development/pycord/pull/1992)) @@ -46,11 +43,11 @@ These changes are available on the `master` branch, but have not yet been releas - Added and documented missing `AuditLogAction` enums. ([#2030](https://github.com/Pycord-Development/pycord/pull/2030), [#2171](https://github.com/Pycord-Development/pycord/pull/2171)) -- `AuditLogDiff` now supports AutoMod related models. +- Added AutoMod-related models for `AuditLogDiff` enums. ([#2030](https://github.com/Pycord-Development/pycord/pull/2030)) - Added `Interaction.respond` and `Interaction.edit` as shortcut responses. ([#2026](https://github.com/Pycord-Development/pycord/pull/2026)) -- Added `view.parent` which is set when the view was sent by +- Added `view.parent` which is set when the view is sent by `interaction.response.send_message`. ([#2036](https://github.com/Pycord-Development/pycord/pull/2036)) - Added functions (`bridge.Bot.walk_bridge_commands` & @@ -67,34 +64,47 @@ These changes are available on the `master` branch, but have not yet been releas ([#2102](https://github.com/Pycord-Development/pycord/pull/2102)) - Added `bridge.Context` as a shortcut to `Union` of subclasses. ([#2106](https://github.com/Pycord-Development/pycord/pull/2106)) -- Added Annotated forms support for typehinting slash command options. +- Added Annotated forms support for type-hinting slash command options. ([#2124](https://github.com/Pycord-Development/pycord/pull/2124)) - 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)) +- 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 -- Suppressed FFMPEG output when recording voice channels. +- Changed default for all `name_localizations` & `description_localizations` attributes + from being `None` to being `MISSING`. + ([#1866](https://github.com/Pycord-Development/pycord/pull/1866)) +- Changed FFMPEG output suppression when recording voice channels. ([#1993](https://github.com/Pycord-Development/pycord/pull/1993)) - Changed file-upload size limit from 8 MB to 25 MB accordingly. ([#2014](https://github.com/Pycord-Development/pycord/pull/2014)) -- `Interaction.channel` is received from the gateway, so it can now be `DMChannel` and - `GroupChannel`. ([#2025](https://github.com/Pycord-Development/pycord/pull/2025)) -- `DMChannel.recipients` can now be `None`. +- Changed the behavior of retrieving bans to accurately reflect the API. + ([#1922](https://github.com/Pycord-Development/pycord/pull/1922)) +- Changed `Interaction.channel` to be received from the gateway, allowing it to be + `DMChannel` or `GroupChannel`. ([#2025](https://github.com/Pycord-Development/pycord/pull/2025)) -- Store `view.message` on receiving Interaction for a component. - ([#2036](https://github.com/Pycord-Development/pycord/pull/2036)) -- Attributes shared between ext and slash commands are now dynamically fetched on bridge - commands. ([#1867](https://github.com/Pycord-Development/pycord/pull/1867)) -- Embed attribues like author, footer, etc now return `None` when not set, and return - their respective classes when set. +- Changed `DMChannel.recipients` to potentially be `None`. + ([#2025](https://github.com/Pycord-Development/pycord/pull/2025)) +- Changed the behavior to store `view.message` when receiving an Interaction for a + component. ([#2036](https://github.com/Pycord-Development/pycord/pull/2036)) +- Changed the fetching of attributes shared between text-based and Slash Commands in + Bridge Commands to be dynamic. + ([#1867](https://github.com/Pycord-Development/pycord/pull/1867)) +- Changed `discord.Embed` attributes (such as author, footer, etc.) now return `None` + when not set, and return their respective classes when set. ([#2063](https://github.com/Pycord-Development/pycord/pull/2063)) -- `default_avatar` behavior changes depending on the user's username migration status. +- Changed `default_avatar` behavior depending on the user's username migration status. ([#2087](https://github.com/Pycord-Development/pycord/pull/2087)) -- Typehinted `command_prefix` and `help_command` arguments properly. +- Changed type-hinted `command_prefix` and `help_command` arguments properly. ([#2099](https://github.com/Pycord-Development/pycord/pull/2099)) -- Replace `orjson` support with `msgspec` support. +- Changed the support from `orjson` to `msgspec` in the codebase. ([#2170](https://github.com/Pycord-Development/pycord/pull/2170)) - Replaces former rate limit system with a rate limit prediction and easing system. ([#2188](https://github.com/Pycord-Development/pycord/pull/2188)) @@ -125,29 +135,30 @@ These changes are available on the `master` branch, but have not yet been releas ([#1994](https://github.com/Pycord-Development/pycord/pull/1994)) - Fixed scheduled events breaking when changing the location from external to a channel. ([#1998](https://github.com/Pycord-Development/pycord/pull/1998)) -- Fixed boolean converter breaking for bridge commands. Fix bridge command Options not - working. ([#1999](https://github.com/Pycord-Development/pycord/pull/1999)) +- Fixed boolean converter breaking for Bridge Commands. + ([#1999](https://github.com/Pycord-Development/pycord/pull/1999)) +- Fixed Bridge Command Options not working. + ([#1999](https://github.com/Pycord-Development/pycord/pull/1999)) - Fixed `TypeError` being raised when passing `name` argument to bridge groups. ([#2000](https://github.com/Pycord-Development/pycord/pull/2000)) -- Fixed `TypeError` in AutoModRule. +- Fixed `TypeError` in `AutoModRule`. ([#2029](https://github.com/Pycord-Development/pycord/pull/2029)) -- Reflecting the api for gettings bans correctly. - ([#1922](https://github.com/Pycord-Development/pycord/pull/1922)) -- Restored functionality for overriding default `on_application_command_error` via - listeners. ([#2044](https://github.com/Pycord-Development/pycord/pull/2044)) -- Fixed unloading of cogs having bridge commands. +- Fixed the functionality to override the default `on_application_command_error` + behavior using listeners. + ([#2044](https://github.com/Pycord-Development/pycord/pull/2044)) +- Fixed unloading of cogs having Bridge Commands. ([#2048](https://github.com/Pycord-Development/pycord/pull/2048)) -- Fixed the Slash command syncronization method `indiviual`. +- Fixed the Slash Command synchronization method `individual`. ([#1925](https://github.com/Pycord-Development/pycord/pull/1925)) - Fixed an issue that occurred when `webhooks_update` event payload channel ID was `None`. ([#2078](https://github.com/Pycord-Development/pycord/pull/2078)) -- Fixed major TypeError when an AuditLogEntry has no user. +- Fixed major `TypeError` when an `AuditLogEntry` has no user. ([#2079](https://github.com/Pycord-Development/pycord/pull/2079)) - Fixed `HTTPException` when trying to create a forum thread with files. ([#2075](https://github.com/Pycord-Development/pycord/pull/2075)) - Fixed `before_invoke` not being run for `SlashCommandGroup`. ([#2091](https://github.com/Pycord-Development/pycord/pull/2091)) -- Fixed `AttributeError` when accessing a `Select`'s values when it hasn't been +- Fixed `AttributeError` when accessing a `Select` object's values when it hasn't been interacted with. ([#2104](https://github.com/Pycord-Development/pycord/pull/2104)) - Fixed `before_invoke` being run twice for slash subcommands. ([#2139](https://github.com/Pycord-Development/pycord/pull/2139)) @@ -155,17 +166,24 @@ These changes are available on the `master` branch, but have not yet been releas ([#2145](https://github.com/Pycord-Development/pycord/pull/2145)) - Fixed `Thread.applied_tags` not being updated. ([#2146](https://github.com/Pycord-Development/pycord/pull/2146)) -- Fixed type-hinting of `author` property of `ApplicationContext` to include typehinting - of `User` or `Member`. +- Fixed type-hinting of `author` property of `ApplicationContext` to include + type-hinting of `User` or `Member`. ([#2148](https://github.com/Pycord-Development/pycord/pull/2148)) -- Fixed missing `delete_after` parameter in overload type-hinting for `send` method in - `Webhook` class. ([#2156](https://github.com/Pycord-Development/pycord/pull/2156)) +- Fixed missing `delete_after` parameter in overload type-hinting for `Webhook.send()`. + ([#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)) - Fixed initial message inside of the create thread payload sending legacy beta payload. ([#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 @@ -179,10 +197,11 @@ These changes are available on the `master` branch, but have not yet been releas - Fixed the type-hinting of `SlashCommandGroup.walk_commands()` to reflect actual behavior. ([#1838](https://github.com/Pycord-Development/pycord/pull/1838)) -- Fixed the voice ip discovery due to the recent +- Fixed the voice IP discovery due to the recent [announced change](https://discord.com/channels/613425648685547541/697138785317814292/1080623873629884486). ([#1955](https://github.com/Pycord-Development/pycord/pull/1955)) -- Fixed `reason` being passed to wrong method in `guild.create_auto_moderation_rule`. +- Fixed `reason` being passed to the wrong method in + `guild.create_auto_moderation_rule`. ([#1960](https://github.com/Pycord-Development/pycord/pull/1960)) ## [2.4.0] - 2023-02-10 @@ -309,7 +328,7 @@ These changes are available on the `master` branch, but have not yet been releas ### Fixed -- Fixed `parent` attribute of second level subcommands being set to the base level +- Fixed `parent` attribute of second-level subcommands being set to the base level command instead of the direct parent. ([#1673](https://github.com/Pycord-Development/pycord/pull/1673)) @@ -337,7 +356,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1613](https://github.com/Pycord-Development/pycord/pull/1613)) - `suppress` kwarg to `Messageable.send()`. ([#1587](https://github.com/Pycord-Development/pycord/pull/1587)) -- `proxy` and `proxy_auth` params to many Webhook related methods. +- `proxy` and `proxy_auth` params to many Webhook-related methods. ([#1655](https://github.com/Pycord-Development/pycord/pull/1655)) - `delete_message_seconds` parameter in ban methods. ([#1557](https://github.com/Pycord-Development/pycord/pull/1557)) @@ -467,7 +486,7 @@ These changes are available on the `master` branch, but have not yet been releas `description`. ([#1546](https://github.com/Pycord-Development/pycord/pull/1546)) - Fix threads created with a base message being set to the wrong `message_reference`. ([#1551](https://github.com/Pycord-Development/pycord/pull/1551)) -- Avoid unnecessary call to `sync_commands` during runtime. +- Avoid unnecessary calls to `sync_commands` during runtime. ([#1563](https://github.com/Pycord-Development/pycord/pull/1563)) - Fix bug in `Modal.on_timeout()` by using `custom_id` to create timeout task. ([#1562](https://github.com/Pycord-Development/pycord/pull/1562)) @@ -562,7 +581,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1386](https://github.com/Pycord-Development/pycord/pull/1386)) - Fix `remove_application_command` not always removing commands. ([#1391](https://github.com/Pycord-Development/pycord/pull/1391)) -- Fix busy-loop in `DecodeManager` when decode queue is empty, causing 100% CPU +- Fix busy-loop in `DecodeManager` when the decode queue is empty, causing 100% CPU consumption. ([#1395](https://github.com/Pycord-Development/pycord/pull/1395)) - Fix incorrect activities and permissions on `Interaction` and `Option` objects. ([#1365](https://github.com/Pycord-Development/pycord/pull/1365)) @@ -582,7 +601,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1453](https://github.com/Pycord-Development/pycord/pull/1453)) - Update `thread.members` on `thread.fetch_members`. ([#1464](https://github.com/Pycord-Development/pycord/pull/1464)) -- Fix error when discord doesn't send the `app_permissions` data in `Interaction`. +- Fix the error when Discord doesn't send the `app_permissions` data in `Interaction`. ([#1467](https://github.com/Pycord-Development/pycord/pull/1467)) - Fix AttributeError when voice client `play()` function isn't completed yet. ([#1360](https://github.com/Pycord-Development/pycord/pull/1360)) @@ -604,10 +623,10 @@ These changes are available on the `master` branch, but have not yet been releas ([#1181](https://github.com/Pycord-Development/pycord/pull/1181)) - Option names and descriptions are now validated locally. ([#1271](https://github.com/Pycord-Development/pycord/pull/1271)) -- Component field limits are now enforced at library-level +- Component field limits are now enforced at the library level ([#1065](https://github.com/Pycord-Development/pycord/pull/1065) & [#1289](https://github.com/Pycord-Development/pycord/pull/1289)) -- Support providing option channel types as list. +- Support providing option channel types as a list. ([#1000](https://github.com/Pycord-Development/pycord/pull/1000)) - New `Guild.jump_url` property. ([#1282](https://github.com/Pycord-Development/pycord/pull/1282)) @@ -643,7 +662,7 @@ These changes are available on the `master` branch, but have not yet been releas - Removed implicit defer call in `View`. ([#1260](https://github.com/Pycord-Development/pycord/pull/1260)) -- `Option` class and usage was rewritten. +- `Option` class and usage were rewritten. ([#1251](https://github.com/Pycord-Development/pycord/pull/1251)) - `description` argument of `PageGroup` is now optional. ([#1330](https://github.com/Pycord-Development/pycord/pull/1330)) @@ -658,7 +677,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1256](https://github.com/Pycord-Development/pycord/pull/1256)) - Fix channel parsing in slash command invocations. ([#1257](https://github.com/Pycord-Development/pycord/pull/1257)) -- Make channel `position` attribute optional. +- Make the channel `position` attribute optional. ([#1257](https://github.com/Pycord-Development/pycord/pull/1257)) - Fix `PaginatorMenu` to use interaction routes for updates. ([#1267](https://github.com/Pycord-Development/pycord/pull/1267)) @@ -670,17 +689,17 @@ These changes are available on the `master` branch, but have not yet been releas ([#1262](https://github.com/Pycord-Development/pycord/pull/1262)) - Fix interaction response race condition. ([#1039](https://github.com/Pycord-Development/pycord/pull/1039)) -- Remove voice client when bot disconnects. +- Remove voice client when the bot disconnects. ([#1273](https://github.com/Pycord-Development/pycord/pull/1273)) -- Fix conversion exception in ext.bridge. +- Fix conversion exception in `ext.bridge`. ([#1250](https://github.com/Pycord-Development/pycord/pull/1250)) -- Context.me return ClientUser when guilds intent is absent. +- `Context.me` returns ClientUser when guilds intent is absent. ([#1286](https://github.com/Pycord-Development/pycord/pull/1286)) -- Updated `Message.edit` type hinting overload and removed resulting redundant +- Updated `Message.edit` type-hinting overload and removed resulting redundant overloads. ([#1299](https://github.com/Pycord-Development/pycord/pull/1299)) - Improved validation regex for command names & options. ([#1309](https://github.com/Pycord-Development/pycord/pull/1309)) -- Correct `Guild.fetch_members()` type hints. +- Correct `Guild.fetch_members()` type-hints. ([#1323](https://github.com/Pycord-Development/pycord/pull/1323)) - Multiple fixes and enhancements for `PageGroup` handling. ([#1350](https://github.com/Pycord-Development/pycord/pull/1350)) 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 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"]) diff --git a/discord/embeds.py b/discord/embeds.py index fb34acc33f..81424050e2 100644 --- a/discord/embeds.py +++ b/discord/embeds.py @@ -176,6 +176,12 @@ class EmbedMedia: # Thumbnail, Image, Video height: int | None width: int | None + def __init__(self, url: str): + self.url = url + self.proxy_url = None + self.height = None + self.width = None + @classmethod def from_dict(cls, data: dict[str, str | int]) -> EmbedMedia: self = cls.__new__(cls) @@ -355,8 +361,8 @@ def __init__( fields: list[EmbedField] | None = None, author: EmbedAuthor | None = None, footer: EmbedFooter | None = None, - image: str | None = None, - thumbnail: str | None = None, + image: str | EmbedMedia | None = None, + thumbnail: str | EmbedMedia | None = None, ): self.colour = colour if colour else color self.title = title @@ -378,17 +384,10 @@ def __init__( self._fields: list[EmbedField] = fields if fields is not None else [] - if author: - self.set_author(**author.to_dict()) - - if footer: - self.set_footer(**footer.to_dict()) - - if image: - self.set_image(url=image) - - if thumbnail: - self.set_thumbnail(url=thumbnail) + self.author = author + self.footer = footer + self.image = image + self.thumbnail = thumbnail @classmethod def from_dict(cls: type[E], data: Mapping[str, Any]) -> E: @@ -566,6 +565,18 @@ def footer(self) -> EmbedFooter | None: return None return EmbedFooter.from_dict(foot) + @footer.setter + def footer(self, value: EmbedFooter | None): + if value is None: + self.remove_footer() + elif isinstance(value, EmbedFooter): + self._footer = value.to_dict() + else: + raise TypeError( + "Expected EmbedFooter or None. Received" + f" {value.__class__.__name__} instead" + ) + def set_footer( self: E, *, @@ -628,6 +639,20 @@ def image(self) -> EmbedMedia | None: return None return EmbedMedia.from_dict(img) + @image.setter + def image(self, value: str | EmbedMedia | None): + if value is None: + self.remove_image() + elif isinstance(value, str): + self.set_image(url=value) + elif isinstance(value, EmbedMedia): + self.set_image(url=value.url) + else: + raise TypeError( + "Expected discord.EmbedMedia, or None but received" + f" {value.__class__.__name__} instead." + ) + def set_image(self: E, *, url: Any | None) -> E: """Sets the image for the embed content. @@ -688,6 +713,20 @@ def thumbnail(self) -> EmbedMedia | None: return None return EmbedMedia.from_dict(thumb) + @thumbnail.setter + def thumbnail(self, value: str | EmbedMedia | None): + if value is None: + self.remove_thumbnail() + elif isinstance(value, str): + self.set_thumbnail(url=value) + elif isinstance(value, EmbedMedia): + self.set_thumbnail(url=value.url) + else: + raise TypeError( + "Expected discord.EmbedMedia, or None but received" + f" {value.__class__.__name__} instead." + ) + def set_thumbnail(self: E, *, url: Any | None) -> E: """Sets the thumbnail for the embed content. @@ -773,6 +812,18 @@ def author(self) -> EmbedAuthor | None: return None return EmbedAuthor.from_dict(auth) + @author.setter + def author(self, value: EmbedAuthor | None): + if value is None: + self.remove_author() + elif isinstance(value, EmbedAuthor): + self._author = value.to_dict() + else: + raise TypeError( + "Expected discord.EmbedAuthor, or None but received" + f" {value.__class__.__name__} instead." + ) + def set_author( self: E, *, 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/guild.py b/discord/guild.py index 2bd77b2dd5..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: @@ -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) diff --git a/discord/http.py b/discord/http.py index 41b3600942..a30463a675 100644 --- a/discord/http.py +++ b/discord/http.py @@ -744,6 +744,7 @@ def get_reaction_users( emoji: str, limit: int, after: Snowflake | None = None, + type: int | None = None, ) -> Response[list[user.User]]: r = Route( "GET", @@ -758,6 +759,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/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/raw_models.py b/discord/raw_models.py index 1cd88ea0c2..39eab47443 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(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(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/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. 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/dev.txt b/requirements/dev.txt index cfdef98ec7..1831a6fd24 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,11 +1,11 @@ -r _.txt pylint~=2.17.5 -pytest~=7.4.0 +pytest~=7.4.3 pytest-asyncio~=0.21.1 # pytest-order~=1.0.1 -mypy~=1.4.1 -coverage~=7.2 -pre-commit==3.3.3 -codespell==2.2.5 +mypy~=1.5.1 +coverage~=7.3 +pre-commit==3.5.0 +codespell==2.2.6 bandit==1.7.5 flake8==6.1.0 diff --git a/requirements/speed.txt b/requirements/speed.txt index 1dd2e76a5d..5664d336cf 100644 --- a/requirements/speed.txt +++ b/requirements/speed.txt @@ -1,2 +1,2 @@ -msgspec~=0.17.0 +msgspec~=0.18.4 aiohttp[speedups]