aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZomatree <[email protected]>2020-11-23 10:09:20 +0000
committerGitHub <[email protected]>2020-11-23 05:09:20 -0500
commit344cb96c5dbdae438be8d99cf8dbd65b166db93f (patch)
tree5a4a5a854a0daffbbf81267454f0d2a24cf25125
parentSuppress the creation of a command window from ffmpeg on Windows (diff)
downloaddiscord.py-344cb96c5dbdae438be8d99cf8dbd65b166db93f.tar.xz
discord.py-344cb96c5dbdae438be8d99cf8dbd65b166db93f.zip
Add sticker support
-rw-r--r--discord/__init__.py1
-rw-r--r--discord/asset.py6
-rw-r--r--discord/enums.py6
-rw-r--r--discord/message.py10
-rw-r--r--discord/sticker.py134
-rw-r--r--docs/api.rst24
6 files changed, 178 insertions, 3 deletions
diff --git a/discord/__init__.py b/discord/__init__.py
index 079ade26..dd26436c 100644
--- a/discord/__init__.py
+++ b/discord/__init__.py
@@ -58,6 +58,7 @@ from .voice_client import VoiceClient, VoiceProtocol
from .audit_logs import AuditLogChanges, AuditLogEntry, AuditLogDiff
from .raw_models import *
from .team import *
+from .sticker import Sticker
VersionInfo = namedtuple('VersionInfo', 'major minor micro releaselevel serial')
diff --git a/discord/asset.py b/discord/asset.py
index f5c5eddc..bf8d8f9d 100644
--- a/discord/asset.py
+++ b/discord/asset.py
@@ -146,6 +146,12 @@ class Asset:
return cls(state, '/icons/{0.id}/{0.icon}.{1}?size={2}'.format(guild, format, size))
+ @classmethod
+ def _from_sticker_url(cls, state, sticker, *, size=1024):
+ if not utils.valid_icon_size(size):
+ raise InvalidArgument("size must be a power of 2 between 16 and 4096")
+
+ return cls(state, '/stickers/{0.id}/{0.image}.png?size={2}'.format(sticker, format, size))
def __str__(self):
return self.BASE + self._url if self._url is not None else ''
diff --git a/discord/enums.py b/discord/enums.py
index e74e6698..03ca4f97 100644
--- a/discord/enums.py
+++ b/discord/enums.py
@@ -52,6 +52,7 @@ __all__ = (
'WebhookType',
'ExpireBehaviour',
'ExpireBehavior',
+ 'StickerType'
)
def _create_value_cls(name):
@@ -455,3 +456,8 @@ def try_enum(cls, val):
return cls._enum_value_map_[val]
except (KeyError, TypeError, AttributeError):
return val
+
+class StickerType(Enum):
+ png = 1
+ apng = 2
+ lottie = 3
diff --git a/discord/message.py b/discord/message.py
index 8c3d9c78..e28bb8ae 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -43,6 +43,7 @@ from .file import File
from .utils import escape_mentions
from .guild import Guild
from .mixins import Hashable
+from .sticker import Sticker
class Attachment:
@@ -348,6 +349,10 @@ class Message(Hashable):
- ``description``: A string representing the application's description.
- ``icon``: A string representing the icon ID of the application.
- ``cover_image``: A string representing the embed's image asset ID.
+ stickers: List[:class:`Sticker`]
+ A list of stickers given to the message.
+
+ .. versionadded:: 1.6
"""
__slots__ = ('_edited_timestamp', 'tts', 'content', 'channel', 'webhook_id',
@@ -355,8 +360,8 @@ class Message(Hashable):
'_cs_channel_mentions', '_cs_raw_mentions', 'attachments',
'_cs_clean_content', '_cs_raw_channel_mentions', 'nonce', 'pinned',
'role_mentions', '_cs_raw_role_mentions', 'type', 'call', 'flags',
- '_cs_system_content', '_cs_guild', '_state', 'reactions', 'reference',
- 'application', 'activity')
+ '_cs_system_content', '_cs_guild', '_state', 'reactions', 'reference',
+ 'application', 'activity', 'stickers')
def __init__(self, *, state, channel, data):
self._state = state
@@ -376,6 +381,7 @@ class Message(Hashable):
self.tts = data['tts']
self.content = data['content']
self.nonce = data.get('nonce')
+ self.stickers = [Sticker(data=data, state=state) for data in data.get('stickers', [])]
ref = data.get('message_reference')
self.reference = MessageReference(state, **ref) if ref is not None else None
diff --git a/discord/sticker.py b/discord/sticker.py
new file mode 100644
index 00000000..e67314e1
--- /dev/null
+++ b/discord/sticker.py
@@ -0,0 +1,134 @@
+# -*- coding: utf-8 -*-
+
+"""
+The MIT License (MIT)
+
+Copyright (c) 2015-2020 Rapptz
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+"""
+
+from .mixins import Hashable
+from .asset import Asset
+from .utils import snowflake_time
+from .enums import StickerType, try_enum
+
+class Sticker(Hashable):
+ """Represents a sticker
+
+ .. versionadded:: 1.6
+
+ .. container:: operations
+
+ .. describe:: str(x)
+
+ Returns the name of the sticker
+
+ .. describe:: x == y
+
+ Checks if the sticker is equal to another sticker
+
+ .. describe:: x != y
+
+ Checks if the sticker is not equal to another sticker
+
+ Attributes
+ ----------
+ name: :class:`str`
+ The sticker's name
+ id: :class:`int`
+ The id of the sticker
+ description: :class:`str`
+ The description of the sticker
+ pack_id: :class:`int`
+ The id of the sticker's pack
+ format: :class:`StickerType`
+ The format for the sticker's image
+ image: :class:`str`
+ The sticker's image
+ tags: List[:class:`str`]
+ A list of tags for the sticker
+ preview_asset: Optional[:class:`str`]
+ The sticker's preview asset hash
+ """
+ __slots__ = ('_state', 'id', 'name', 'description', 'pack_id', 'format', 'image', 'tags', 'preview_asset')
+
+ def __init__(self, *, state, data):
+ self._state = state
+ self.id = int(data['id'])
+ self.name = data['name']
+ self.description = data['description']
+ self.pack_id = int(data['pack_id'])
+ self.format = try_enum(StickerType, data['format_type'])
+ self.image = data['asset']
+ self.tags = [tag.strip() for tag in data.get('tags', '').split(',')]
+ self.preview_asset = data.get('preview_asset')
+
+ def __repr__(self):
+ return '<{0.__class__.__name__} id={0.id} name={0.name!r}>'.format(self)
+
+ def __str__(self):
+ return self.name
+
+ @property
+ def created_at(self):
+ """:class:`datetime.datetime`: Returns the sticker's creation time in UTC as a naive datetime."""
+ return snowflake_time(self.id)
+
+ @property
+ def image_url(self):
+ """Returns an :class:`Asset` for the sticker's image.
+
+ .. note::
+ This will return ``None`` if the format is ``StickerType.lottie``
+
+ Returns
+ -------
+ Optional[:class:`Asset`]
+ The resulting CDN asset.
+ """
+ return self.image_url_as()
+
+ def image_url_as(self, *, size=1024):
+ """Optionally returns an :class:`Asset` for the sticker's image.
+
+ The size must be a power of 2 between 16 and 4096.
+
+ .. note::
+ This will return ``None`` if the format is ``StickerType.lottie``.
+
+ Parameters
+ -----------
+ size: :class:`int`
+ The size of the image to display.
+
+ Raises
+ ------
+ InvalidArgument
+ Invalid ``size``.
+
+ Returns
+ -------
+ Optional[:class:`Asset`]
+ The resulting CDN asset or ``None``.
+ """
+ if self.format is StickerType.lottie:
+ return None
+
+ return Asset._from_sticker_url(self._state, self, size=size)
diff --git a/docs/api.rst b/docs/api.rst
index 81e35720..60923557 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -1993,6 +1993,23 @@ of :class:`enum.Enum`.
Represents the default avatar with the color red.
See also :attr:`Colour.red`
+.. class:: StickerType
+
+ Represents the type of sticker images.
+
+ .. versionadded:: 1.6
+
+ .. attribute:: png
+
+ Represents a sticker with a png image.
+
+ .. attribute:: apng
+
+ Represents a sticker with an apng image.
+
+ .. attribute:: lottie
+
+ Represents a sticker with a lottie image.
Async Iterator
----------------
@@ -2815,6 +2832,12 @@ Widget
.. autoclass:: Widget()
:members:
+Sticker
+~~~~~~~~~~~~~~~
+
+.. autoclass:: Sticker()
+ :members:
+
MessageReference
~~~~~~~~~~~~~~~~~
.. autoclass:: MessageReference()
@@ -2982,7 +3005,6 @@ PublicUserFlags
.. autoclass:: PublicUserFlags()
:members:
-
Exceptions
------------