aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNCPlayz <[email protected]>2019-05-18 22:29:22 +0100
committerRapptz <[email protected]>2019-06-07 19:12:59 -0400
commitf7c6c5995daef14b4d471b6eaea5cb96305bfc88 (patch)
tree89d0c932c9bf6a0713b285dddd95da09ef9ca0b8
parentAdd support for system channel flags (diff)
downloaddiscord.py-f7c6c5995daef14b4d471b6eaea5cb96305bfc88.tar.xz
discord.py-f7c6c5995daef14b4d471b6eaea5cb96305bfc88.zip
Implementing GET '/channels/:id' & '/guilds/:id/channels'
Signed-off-by: NCPlayz <[email protected]>
-rw-r--r--discord/client.py45
-rw-r--r--discord/errors.py6
-rw-r--r--discord/guild.py36
-rw-r--r--discord/http.py7
4 files changed, 94 insertions, 0 deletions
diff --git a/discord/client.py b/discord/client.py
index fd12d7ec..425b1b60 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -39,6 +39,8 @@ from .asset import Asset
from .invite import Invite
from .widget import Widget
from .guild import Guild
+from .channel import _channel_factory
+from .enums import ChannelType
from .member import Member
from .errors import *
from .enums import Status, VoiceRegion
@@ -48,6 +50,7 @@ from .voice_client import VoiceClient
from .http import HTTPClient
from .state import ConnectionState
from . import utils
+from .object import Object
from .backoff import ExponentialBackoff
from .webhook import Webhook
from .iterators import GuildIterator
@@ -1185,6 +1188,48 @@ class Client:
user=User(data=user, state=state),
connected_accounts=data['connected_accounts'])
+ async def fetch_channel(self, channel_id):
+ """|coro|
+
+ Retrieves a :class:`.abc.GuildChannel` or :class:`.abc.PrivateChannel` with the specified ID.
+
+ .. note::
+
+ This method is an API call. For general usage, consider :meth:`get_channel` instead.
+
+ .. versionadded:: 1.2.0
+
+ Raises
+ -------
+ TypeError
+ An unknown channel type was received from Discord.
+ :exc:`.HTTPException`
+ Retrieving the channel failed.
+ :exc:`.NotFound`
+ Invalid Channel ID.
+ :exc:`.Forbidden`
+ You do not have permission to fetch this channel.
+
+ Returns
+ --------
+ Union[:class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`]
+ The channel from the ID.
+ """
+ data = await self.http.get_channel(channel_id)
+
+ factory, ch_type = _channel_factory(data['type'])
+ if factory is None:
+ raise InvalidData('Unknown channel type {type} for channel ID {id}.'.format_map(data))
+
+ if ch_type in (ChannelType.group, ChannelType.private):
+ channel = factory(me=self.user, data=data, state=self._connection)
+ else:
+ guild_id = int(data['guild_id'])
+ guild = self.get_guild(guild_id) or Object(id=guild_id)
+ channel = factory(guild=guild, state=self._connection, data=data)
+
+ return channel
+
async def fetch_webhook(self, webhook_id):
"""|coro|
diff --git a/discord/errors.py b/discord/errors.py
index c908a8ac..b0f91cf8 100644
--- a/discord/errors.py
+++ b/discord/errors.py
@@ -125,6 +125,12 @@ class NotFound(HTTPException):
pass
+class InvalidData(ClientException):
+ """Exception that's raised when the library encounters unknown
+ or invalid data from Discord.
+ """
+ pass
+
class InvalidArgument(ClientException):
"""Exception that's thrown when an argument to a function
is invalid some way (e.g. wrong value or wrong type).
diff --git a/discord/guild.py b/discord/guild.py
index fc8d22b9..f6a5e2c5 100644
--- a/discord/guild.py
+++ b/discord/guild.py
@@ -32,6 +32,7 @@ from .role import Role
from .member import Member, VoiceState
from .activity import create_activity
from .emoji import Emoji
+from .errors import InvalidData
from .permissions import PermissionOverwrite
from .colour import Colour
from .errors import InvalidArgument, ClientException
@@ -1104,6 +1105,41 @@ class Guild(Hashable):
fields['system_channel_flags'] = system_channel_flags.value
await http.edit_guild(self.id, reason=reason, **fields)
+ async def fetch_channels(self):
+ """|coro|
+
+ Retrieves all :class:`abc.GuildChannel` that the guild has.
+
+ .. note::
+
+ This method is an API call. For general usage, consider :attr:`channels` instead.
+
+ .. versionadded:: 1.2.0
+
+ Raises
+ -------
+ TypeError
+ An unknown channel type was received from Discord.
+ HTTPException
+ Retrieving the channels failed.
+
+ Returns
+ -------
+ List[:class:`abc.GuildChannel`]
+ All channels in the guild.
+ """
+ data = await self._state.http.get_all_guild_channels(self.id)
+
+ def convert(d):
+ factory, ch_type = _channel_factory(d['type'])
+ if factory is None:
+ raise InvalidData('Unknown channel type {type} for channel ID {id}.'.format_map(data))
+
+ channel = factory(guild=self, state=self._state, data=d)
+ return channel
+
+ return [convert(d) for d in data]
+
async def fetch_member(self, member_id):
"""|coro|
diff --git a/discord/http.py b/discord/http.py
index d3ef0b87..16b65717 100644
--- a/discord/http.py
+++ b/discord/http.py
@@ -408,6 +408,10 @@ class HTTPClient:
r = Route('GET', '/channels/{channel_id}/messages/{message_id}', channel_id=channel_id, message_id=message_id)
return self.request(r)
+ def get_channel(self, channel_id):
+ r = Route('GET', '/channels/{channel_id}', channel_id=channel_id)
+ return self.request(r)
+
def logs_from(self, channel_id, limit, before=None, after=None, around=None):
params = {
'limit': limit
@@ -614,6 +618,9 @@ class HTTPClient:
payload = {'code': code}
return self.request(Route('PATCH', '/guilds/{guild_id}/vanity-url', guild_id=guild_id), json=payload, reason=reason)
+ def get_all_guild_channels(self, guild_id):
+ return self.request(Route('GET', '/guilds/{guild_id}/channels', guild_id=guild_id))
+
def get_member(self, guild_id, member_id):
return self.request(Route('GET', '/guilds/{guild_id}/members/{member_id}', guild_id=guild_id, member_id=member_id))