Skip to content

Commit c050ed0

Browse files
authored
Support new fields in Modify Current Member
1 parent 46300df commit c050ed0

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

discord/http.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,18 +1136,15 @@ def guild_voice_state(
11361136
def edit_profile(self, payload: Dict[str, Any]) -> Response[user.User]:
11371137
return self.request(Route('PATCH', '/users/@me'), json=payload)
11381138

1139-
def change_my_nickname(
1139+
def edit_my_member(
11401140
self,
11411141
guild_id: Snowflake,
1142-
nickname: str,
11431142
*,
11441143
reason: Optional[str] = None,
1145-
) -> Response[member.Nickname]:
1146-
r = Route('PATCH', '/guilds/{guild_id}/members/@me/nick', guild_id=guild_id)
1147-
payload = {
1148-
'nick': nickname,
1149-
}
1150-
return self.request(r, json=payload, reason=reason)
1144+
**fields: Any,
1145+
) -> Response[member.MemberWithUser]:
1146+
r = Route('PATCH', '/guilds/{guild_id}/members/@me', guild_id=guild_id)
1147+
return self.request(r, json=fields, reason=reason)
11511148

11521149
def change_nickname(
11531150
self,

discord/member.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,12 +815,22 @@ async def edit(
815815
voice_channel: Optional[VocalGuildChannel] = MISSING,
816816
timed_out_until: Optional[datetime.datetime] = MISSING,
817817
bypass_verification: bool = MISSING,
818+
avatar: Optional[bytes] = MISSING,
819+
banner: Optional[bytes] = MISSING,
820+
bio: Optional[str] = MISSING,
818821
reason: Optional[str] = None,
819822
) -> Optional[Member]:
820823
"""|coro|
821824
822825
Edits the member's data.
823826
827+
.. note::
828+
829+
To upload an avatar or banner, a :term:`py:bytes-like object` must be passed in that
830+
represents the image being uploaded. If this is done through a file
831+
then the file must be opened via ``open('some_filename', 'rb')`` and
832+
the :term:`py:bytes-like object` is given through the use of ``fp.read()``.
833+
824834
Depending on the parameter passed, this requires different permissions listed below:
825835
826836
+---------------------+---------------------------------------+
@@ -876,6 +886,20 @@ async def edit(
876886
Indicates if the member should be allowed to bypass the guild verification requirements.
877887
878888
.. versionadded:: 2.2
889+
avatar: Optional[:class:`bytes`]
890+
A :term:`py:bytes-like object` representing the image to upload. Could be ``None`` to denote no avatar.
891+
Only image formats supported for uploading are JPEG, PNG, GIF, and WEBP.
892+
This can only be set when editing the bot's own member.
893+
.. versionadded:: 2.7
894+
banner: Optional[:class:`bytes`]
895+
A :term:`py:bytes-like object` representing the image to upload. Could be ``None`` to denote no banner.
896+
Only image formats supported for uploading are JPEG, PNG, GIF and WEBP..
897+
This can only be set when editing the bot's own member.
898+
.. versionadded:: 2.7
899+
bio: Optional[:class:`str`]
900+
The new bio for the member. Use ``None`` to remove the bio.
901+
This can only be set when editing the bot's own member.
902+
.. versionadded:: 2.7
879903
880904
reason: Optional[:class:`str`]
881905
The reason for editing this member. Shows up on the audit log.
@@ -888,6 +912,9 @@ async def edit(
888912
The operation failed.
889913
TypeError
890914
The datetime object passed to ``timed_out_until`` was not timezone-aware.
915+
ValueError
916+
You tried to edit the bio, avatar or banner of a member that is not the bot's own member.
917+
Or the wrong image format passed for ``avatar`` or ``banner``.
891918
892919
Returns
893920
--------
@@ -899,14 +926,33 @@ async def edit(
899926
guild_id = self.guild.id
900927
me = self._state.self_id == self.id
901928
payload: Dict[str, Any] = {}
929+
self_payload: Dict[str, Any] = {}
902930

903931
if nick is not MISSING:
904932
nick = nick or ''
905933
if me:
906-
await http.change_my_nickname(guild_id, nick, reason=reason)
934+
self_payload['nick'] = nick
907935
else:
908936
payload['nick'] = nick
909937

938+
if avatar is not MISSING:
939+
if avatar is None:
940+
self_payload['avatar'] = None
941+
else:
942+
self_payload['avatar'] = utils._bytes_to_base64_data(avatar)
943+
944+
if banner is not MISSING:
945+
if banner is None:
946+
self_payload['banner'] = None
947+
else:
948+
self_payload['banner'] = utils._bytes_to_base64_data(banner)
949+
950+
if bio is not MISSING:
951+
self_payload['bio'] = bio or ''
952+
953+
if not me and self_payload:
954+
raise ValueError("Editing the bio, avatar or banner is only for the bot's own member.")
955+
910956
if deafen is not MISSING:
911957
payload['deaf'] = deafen
912958

@@ -954,7 +1000,12 @@ async def edit(
9541000

9551001
if payload:
9561002
data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
957-
return Member(data=data, guild=self.guild, state=self._state)
1003+
elif self_payload:
1004+
data = await http.edit_my_member(guild_id, reason=reason, **self_payload)
1005+
else:
1006+
return None
1007+
1008+
return Member(data=data, guild=self.guild, state=self._state)
9581009

9591010
async def request_to_speak(self) -> None:
9601011
"""|coro|

0 commit comments

Comments
 (0)