From 37b0fdb898a0f242f68d30abb6c58bd1a15a6524 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 21 Aug 2017 01:54:33 -0400 Subject: Add webhook support. Allows for usage of either `requests` and `aiohttp` when used in "Standalone" mode. Fixes #704 --- discord/channel.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'discord/channel.py') diff --git a/discord/channel.py b/discord/channel.py index 946ed3bc..d81ad273 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -28,6 +28,7 @@ from .enums import ChannelType, try_enum from .mixins import Hashable from . import utils from .errors import ClientException, NoMoreItems +from .webhook import Webhook import discord.abc @@ -321,6 +322,66 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): count += 1 ret.append(msg) + @asyncio.coroutine + def webhooks(self): + """|coro| + + Gets the list of webhooks from this channel. + + Requires :attr:`~.Permissions.manage_webhooks` permissions. + + Raises + ------- + Forbidden + You don't have permissions to get the webhooks. + + Returns + -------- + List[:class:`Webhook`] + The webhooks for this channel. + """ + + data = yield from self._state.http.channel_webhooks(self.id) + return [Webhook.from_state(d, state=self._state) for d in data] + + @asyncio.coroutine + def create_webhook(self, *, name=None, avatar=None): + """|coro| + + Creates a webhook for this channel. + + Requires :attr:`~.Permissions.manage_webhooks` permissions. + + Parameters + ------------- + name: Optional[str] + The webhook's name. + avatar: Optional[bytes] + A *bytes-like* object representing the webhook's default avatar. + This operates similarly to :meth:`~ClientUser.edit`. + + Raises + ------- + HTTPException + Creating the webhook failed. + Forbidden + You do not have permissions to create a webhook. + + Returns + -------- + :class:`Webhook` + The created webhook. + """ + + if avatar is not None: + avatar = utils._bytes_to_base64_data(avatar) + + if name is not None: + name = str(name) + + data = yield from self._state.http.create_webhook(self.id, name=name, avatar=avatar) + return Webhook.from_state(data, state=self._state) + class VoiceChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable): """Represents a Discord guild voice channel. -- cgit v1.2.3