aboutsummaryrefslogtreecommitdiff
path: root/discord/channel.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-02-09 20:47:47 -0500
committerRapptz <[email protected]>2017-02-09 20:50:02 -0500
commitca81f0c3fcdd29b16adbf39c616ab15231d21a7f (patch)
treeaf1ca89e7e79e44b1a69333f3aa1658adf1b3f89 /discord/channel.py
parentCall message edit handlers after attempting to patch individual fields (diff)
downloaddiscord.py-ca81f0c3fcdd29b16adbf39c616ab15231d21a7f.tar.xz
discord.py-ca81f0c3fcdd29b16adbf39c616ab15231d21a7f.zip
Better group DM support.
Diffstat (limited to 'discord/channel.py')
-rw-r--r--discord/channel.py105
1 files changed, 104 insertions, 1 deletions
diff --git a/discord/channel.py b/discord/channel.py
index 8699c25e..af11eea2 100644
--- a/discord/channel.py
+++ b/discord/channel.py
@@ -359,7 +359,6 @@ class GroupChannel(discord.abc.Messageable, Hashable):
def __init__(self, *, me, state, data):
self._state = state
- self.recipients = [state.store_user(u) for u in data['recipients']]
self.id = int(data['id'])
self.me = me
self._update_group(data)
@@ -369,6 +368,11 @@ class GroupChannel(discord.abc.Messageable, Hashable):
self.icon = data.get('icon')
self.name = data.get('name')
+ try:
+ self.recipients = [state.store_user(u) for u in data['recipients']]
+ except KeyError:
+ pass
+
if owner_id == self.me.id:
self.owner = self.me
else:
@@ -438,6 +442,105 @@ class GroupChannel(discord.abc.Messageable, Hashable):
return base
+ @asyncio.coroutine
+ def add_recipients(self, *recipients):
+ """|coro|
+
+ Adds recipients to this group.
+
+ A group can only have a maximum of 10 members.
+ Attempting to add more ends up in an exception. To
+ add a recipient to the group, you must have a relationship
+ with the user of type :attr:`RelationshipType.friend`.
+
+ Parameters
+ -----------
+ \*recipients: :class:`User`
+ An argument list of users to add to this group.
+
+ Raises
+ -------
+ HTTPException
+ Adding a recipient to this group failed.
+ """
+
+ # TODO: wait for the corresponding WS event
+
+ req = self._state.http.add_group_recipient
+ for recipient in recipients:
+ yield from req(self.id, recipient.id)
+
+ @asyncio.coroutine
+ def remove_recipients(self, *recipients):
+ """|coro|
+
+ Removes recipients from this group.
+
+ Parameters
+ -----------
+ \*recipients: :class:`User`
+ An argument list of users to remove from this group.
+
+ Raises
+ -------
+ HTTPException
+ Removing a recipient from this group failed.
+ """
+
+ # TODO: wait for the corresponding WS event
+
+ req = self._state.http.remove_group_recipient
+ for recipient in recipients:
+ yield from req(self.id, recipient.id)
+
+ @asyncio.coroutine
+ def edit(self, **fields):
+ """|coro|
+
+ Edits the group.
+
+ Parameters
+ -----------
+ name: Optional[str]
+ The new name to change the group to.
+ Could be ``None`` to remove the name.
+ icon: Optional[bytes]
+ A bytes-like object representing the new icon.
+ Could be ``None`` to remove the icon.
+
+ Raises
+ -------
+ HTTPException
+ Editing the group failed.
+ """
+
+ try:
+ icon_bytes = fields['icon']
+ except KeyError:
+ pass
+ else:
+ if icon_bytes is not None:
+ fields['icon'] = utils._bytes_to_base64_data(icon_bytes)
+
+ data = yield from self._state.http.edit_group(self.id, **fields)
+ self._update_group(data)
+
+ @asyncio.coroutine
+ def leave(self):
+ """|coro|
+
+ Leave the group.
+
+ If you are the only one in the group, this deletes it as well.
+
+ Raises
+ -------
+ HTTPException
+ Leaving the group failed.
+ """
+
+ yield from self._state.http.leave_group(self.id)
+
def _channel_factory(channel_type):
value = try_enum(ChannelType, channel_type)
if value is ChannelType.text: