Skip to content

Commit dda0daa

Browse files
Soheabpre-commit-ci[bot]plun1331Paillat-devDA-344
authored
feat: ✨ Add support for new member fields for bots (#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 #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]>
1 parent 14989c9 commit dda0daa

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
@@ -1020,38 +1020,6 @@ def guild_voice_state(
10201020
def edit_profile(self, payload: dict[str, Any]) -> Response[user.User]:
10211021
return self.request(Route("PATCH", "/users/@me"), json=payload)
10221022

1023-
def change_my_nickname(
1024-
self,
1025-
guild_id: Snowflake,
1026-
nickname: str,
1027-
*,
1028-
reason: str | None = None,
1029-
) -> Response[member.Nickname]:
1030-
r = Route("PATCH", "/guilds/{guild_id}/members/@me", guild_id=guild_id)
1031-
payload = {
1032-
"nick": nickname,
1033-
}
1034-
return self.request(r, json=payload, reason=reason)
1035-
1036-
def change_nickname(
1037-
self,
1038-
guild_id: Snowflake,
1039-
user_id: Snowflake,
1040-
nickname: str,
1041-
*,
1042-
reason: str | None = None,
1043-
) -> Response[member.Member]:
1044-
r = Route(
1045-
"PATCH",
1046-
"/guilds/{guild_id}/members/{user_id}",
1047-
guild_id=guild_id,
1048-
user_id=user_id,
1049-
)
1050-
payload = {
1051-
"nick": nickname,
1052-
}
1053-
return self.request(r, json=payload, reason=reason)
1054-
10551023
def edit_my_voice_state(
10561024
self, guild_id: Snowflake, payload: dict[str, Any]
10571025
) -> Response[None]:

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
@@ -769,6 +770,9 @@ async def edit(
769770
reason: str | None = None,
770771
communication_disabled_until: datetime.datetime | None = MISSING,
771772
bypass_verification: bool | None = MISSING,
773+
banner: bytes | None = MISSING,
774+
avatar: bytes | None = MISSING,
775+
bio: str | None = MISSING,
772776
) -> Member | None:
773777
"""|coro|
774778
@@ -804,6 +808,14 @@ async def edit(
804808
805809
- Client has ALL THREE of :attr:`Permissions.moderate_members`, :attr:`Permissions.kick_members`, and :attr:`Permissions.ban_members`
806810
811+
.. note::
812+
813+
The following parameters are only available when editing the bot's own member:
814+
815+
- ``avatar``
816+
- ``banner``
817+
- ``bio``
818+
807819
All parameters are optional.
808820
809821
.. versionchanged:: 1.1
@@ -841,6 +853,26 @@ async def edit(
841853
Indicates if the member should bypass the guild's verification requirements.
842854
843855
.. versionadded:: 2.6
856+
banner: Optional[:class:`bytes`]
857+
A :term:`py:bytes-like object` representing the banner.
858+
Could be ``None`` to denote removal of the banner.
859+
860+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
861+
862+
.. versionadded:: 2.7
863+
avatar: Optional[:class:`bytes`]
864+
A :term:`py:bytes-like object` representing the avatar.
865+
Could be ``None`` to denote removal of the avatar.
866+
867+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
868+
869+
.. versionadded:: 2.7
870+
bio: Optional[:class:`str`]
871+
The new bio for the member. Could be ``None`` to denote removal of the bio.
872+
873+
This is only available when editing the bot's own member (i.e. :attr:`Guild.me`).
874+
875+
.. versionadded:: 2.7
844876
845877
Returns
846878
-------
@@ -854,16 +886,19 @@ async def edit(
854886
You do not have the proper permissions to the action requested.
855887
HTTPException
856888
The operation failed.
889+
InvalidArgument
890+
You tried to edit the avatar, banner, or bio of a member that is not the bot.
857891
"""
858892
http = self._state.http
859893
guild_id = self.guild.id
860894
me = self._state.self_id == self.id
861895
payload: dict[str, Any] = {}
896+
bot_payload: dict[str, Any] = {}
862897

863898
if nick is not MISSING:
864899
nick = nick or ""
865900
if me:
866-
await http.change_my_nickname(guild_id, nick, reason=reason)
901+
bot_payload["nick"] = nick
867902
else:
868903
payload["nick"] = nick
869904

@@ -910,9 +945,34 @@ async def edit(
910945
flags.bypasses_verification = bypass_verification
911946
payload["flags"] = flags.value
912947

948+
if avatar is not MISSING:
949+
if avatar is None:
950+
bot_payload["avatar"] = None
951+
else:
952+
bot_payload["avatar"] = utils._bytes_to_base64_data(avatar)
953+
954+
if banner is not MISSING:
955+
if banner is None:
956+
bot_payload["banner"] = None
957+
else:
958+
bot_payload["banner"] = utils._bytes_to_base64_data(banner)
959+
960+
if bio is not MISSING:
961+
bot_payload["bio"] = bio or ""
962+
963+
if bot_payload and not me:
964+
raise InvalidArgument(
965+
"Can only edit avatar, banner, or bio for the bot's member."
966+
)
967+
913968
if payload:
914969
data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
915-
return Member(data=data, guild=self.guild, state=self._state)
970+
elif bot_payload:
971+
data = await http.edit_member(guild_id, "@me", reason=reason, **bot_payload)
972+
else:
973+
return None
974+
975+
return Member(data=data, guild=self.guild, state=self._state)
916976

917977
async def timeout(
918978
self, until: datetime.datetime | None, *, reason: str | None = None

0 commit comments

Comments
 (0)