mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-22 16:51:59 +00:00
Add sticker support
This commit is contained in:
parent
f9b5cead31
commit
344cb96c5d
@ -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')
|
||||
|
||||
|
@ -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 ''
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
134
discord/sticker.py
Normal file
134
discord/sticker.py
Normal file
@ -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)
|
24
docs/api.rst
24
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
|
||||
------------
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user