Skip to content

Commit 062d571

Browse files
SoheabPaillat-dev
authored andcommitted
feat: ✨ Add support for new member fields for bots (Pycord-Development#2910)
* Add support for new current member fields discord/discord-api-docs#7807 * chore: add changelog entry * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md Co-authored-by: plun1331 <[email protected]> Signed-off-by: Soheab <[email protected]> * style(pre-commit): auto fixes from pre-commit.com hooks * chore: do requested change Pycord-Development#2910 (comment) * Update discord/member.py Co-authored-by: DA344 <[email protected]> Signed-off-by: Soheab <[email protected]> --------- Signed-off-by: Soheab <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: plun1331 <[email protected]> Co-authored-by: Paillat <[email protected]> Co-authored-by: DA344 <[email protected]> (cherry picked from commit dda0daa) Signed-off-by: Paillat-dev <[email protected]>
1 parent cf0e203 commit 062d571

File tree

3 files changed

+65
-34
lines changed

3 files changed

+65
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ These changes are available on the `master` branch, but have not yet been releas
1717
([#2711](https://github.com/Pycord-Development/pycord/pull/2711))
1818
- Added `RawMessageUpdateEvent.new_message` - message update events now contain full
1919
message objects ([#2780](https://github.com/Pycord-Development/pycord/pull/2780))
20+
- Added support for setting guild-specific `avatar`, `banner`, and `bio` for the bot
21+
user through `Member.edit`.
22+
([#2908](https://github.com/Pycord-Development/pycord/pull/2908))
2023

2124
### Changed
2225

discord/http.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -972,38 +972,6 @@ def guild_voice_state(
972972
def edit_profile(self, payload: dict[str, Any]) -> Response[user.User]:
973973
return self.request(Route("PATCH", "/users/@me"), json=payload)
974974

975-
def change_my_nickname(
976-
self,
977-
guild_id: Snowflake,
978-
nickname: str,
979-
*,
980-
reason: str | None = None,
981-
) -> Response[member.Nickname]:
982-
r = Route("PATCH", "/guilds/{guild_id}/members/@me", guild_id=guild_id)
983-
payload = {
984-
"nick": nickname,
985-
}
986-
return self.request(r, json=payload, reason=reason)
987-
988-
def change_nickname(
989-
self,
990-
guild_id: Snowflake,
991-
user_id: Snowflake,
992-
nickname: str,
993-
*,
994-
reason: str | None = None,
995-
) -> Response[member.Member]:
996-
r = Route(
997-
"PATCH",
998-
"/guilds/{guild_id}/members/{user_id}",
999-
guild_id=guild_id,
1000-
user_id=user_id,
1001-
)
1002-
payload = {
1003-
"nick": nickname,
1004-
}
1005-
return self.request(r, json=payload, reason=reason)
1006-
1007975
def edit_my_voice_state(self, guild_id: Snowflake, payload: dict[str, Any]) -> Response[None]:
1008976
r = Route("PATCH", "/guilds/{guild_id}/voice-states/@me", guild_id=guild_id)
1009977
return self.request(r, json=payload)

discord/member.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .asset import Asset
4040
from .colour import Colour
4141
from .enums import Status, try_enum
42+
from .errors import InvalidArgument
4243
from .flags import MemberFlags
4344
from .object import Object
4445
from .permissions import Permissions
@@ -752,6 +753,9 @@ async def edit(
752753
reason: str | None = None,
753754
communication_disabled_until: datetime.datetime | None | utils.Undefined = MISSING,
754755
bypass_verification: bool | None | utils.Undefined = MISSING,
756+
banner: bytes | None = MISSING,
757+
avatar: bytes | None = MISSING,
758+
bio: str | None = MISSING,
755759
) -> Member | None:
756760
"""|coro|
757761
@@ -787,6 +791,14 @@ async def edit(
787791
788792
- Client has ALL THREE of :attr:`Permissions.moderate_members`, :attr:`Permissions.kick_members`, and :attr:`Permissions.ban_members`
789793
794+
.. note::
795+
796+
The following parameters are only available when editing the bot's own member:
797+
798+
- ``avatar``
799+
- ``banner``
800+
- ``bio``
801+
790802
All parameters are optional.
791803
792804
.. versionchanged:: 1.1
@@ -824,6 +836,26 @@ async def edit(
824836
Indicates if the member should bypass the guild's verification requirements.
825837
826838
.. versionadded:: 2.6
839+
banner: Optional[:class:`bytes`]
840+
A :term:`py:bytes-like object` representing the banner.
841+
Could be ``None`` to denote removal of the banner.
842+
843+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
844+
845+
.. versionadded:: 2.7
846+
avatar: Optional[:class:`bytes`]
847+
A :term:`py:bytes-like object` representing the avatar.
848+
Could be ``None`` to denote removal of the avatar.
849+
850+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
851+
852+
.. versionadded:: 2.7
853+
bio: Optional[:class:`str`]
854+
The new bio for the member. Could be ``None`` to denote removal of the bio.
855+
856+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
857+
858+
.. versionadded:: 2.7
827859
828860
Returns
829861
-------
@@ -837,16 +869,19 @@ async def edit(
837869
You do not have the proper permissions to the action requested.
838870
HTTPException
839871
The operation failed.
872+
InvalidArgument
873+
You tried to edit the avatar, banner, or bio of a member that is not the bot.
840874
"""
841875
http = self._state.http
842876
guild_id = self.guild.id
843877
me = self._state.self_id == self.id
844878
payload: dict[str, Any] = {}
879+
bot_payload: dict[str, Any] = {}
845880

846881
if nick is not MISSING:
847882
nick = nick or ""
848883
if me:
849-
await http.change_my_nickname(guild_id, nick, reason=reason)
884+
bot_payload["nick"] = nick
850885
else:
851886
payload["nick"] = nick
852887

@@ -889,9 +924,34 @@ async def edit(
889924
flags.bypasses_verification = bypass_verification
890925
payload["flags"] = flags.value
891926

927+
if avatar is not MISSING:
928+
if avatar is None:
929+
bot_payload["avatar"] = None
930+
else:
931+
bot_payload["avatar"] = utils._bytes_to_base64_data(avatar)
932+
933+
if banner is not MISSING:
934+
if banner is None:
935+
bot_payload["banner"] = None
936+
else:
937+
bot_payload["banner"] = utils._bytes_to_base64_data(banner)
938+
939+
if bio is not MISSING:
940+
bot_payload["bio"] = bio or ""
941+
942+
if bot_payload and not me:
943+
raise InvalidArgument(
944+
"Can only edit avatar, banner, or bio for the bot's member."
945+
)
946+
892947
if payload:
893948
data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
894-
return Member(data=data, guild=self.guild, state=self._state)
949+
elif bot_payload:
950+
data = await http.edit_member(guild_id, "@me", reason=reason, **bot_payload)
951+
else:
952+
return None
953+
954+
return Member(data=data, guild=self.guild, state=self._state)
895955

896956
async def timeout(self, until: datetime.datetime | None, *, reason: str | None = None) -> None:
897957
"""|coro|

0 commit comments

Comments
 (0)