Skip to content

Commit b49bbb8

Browse files
committed
feat(docs): Documented GuildSticker and StickerPack
1 parent 5caee7e commit b49bbb8

File tree

3 files changed

+115
-28
lines changed

3 files changed

+115
-28
lines changed

discord/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from .audit_logs import AuditLogChanges, AuditLogEntry, AuditLogDiff
6666
from .raw_models import *
6767
from .team import *
68-
from .sticker import Sticker, GuildSticker
68+
from .sticker import Sticker, GuildSticker, StickerPack
6969
from .scheduled_event import GuildScheduledEvent
7070
from .automod import *
7171

discord/sticker.py

Lines changed: 98 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,31 @@
2323
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2424
DEALINGS IN THE SOFTWARE.
2525
"""
26-
from typing import Optional, List, Tuple, TYPE_CHECKING, Union, Dict
26+
from __future__ import annotations
2727

28-
from multidict import MultiDict
28+
from typing import (
29+
Optional,
30+
List,
31+
Tuple,
32+
TYPE_CHECKING,
33+
Union
34+
)
2935

3036
from .mixins import Hashable
3137
from .asset import Asset
3238
from .utils import snowflake_time, get as utils_get
3339
from .enums import StickerType, try_enum
3440

3541
if TYPE_CHECKING:
42+
from datetime import datetime
43+
44+
from .guild import Guild
45+
from .user import User
3646
from .state import ConnectionState
3747

3848

3949
class StickerPack(Hashable):
40-
"""Represents a Sticker Pack object.
50+
"""Represents a pack of build-in stickers
4151
4252
Attributes
4353
----------
@@ -53,9 +63,8 @@ class StickerPack(Hashable):
5363
The id of the sticker pack's banner image.
5464
cover_sticker_id: Optional[:class:`int`]
5565
The id of a sticker in the pack which is shown in the client as the pack's icon.
56-
5766
"""
58-
def __init__(self, state: 'ConnectionState', data):
67+
def __init__(self, state: ConnectionState, data) -> None:
5968
self._state = state
6069
self.id = int(data['id'])
6170
self.name: str = data['name']
@@ -66,21 +75,56 @@ def __init__(self, state: 'ConnectionState', data):
6675
self.__stickers: Tuple[Sticker] = tuple(map(lambda d: state.store_sticker(d, self), data['stickers']))
6776

6877
@property
69-
def stickers(self) -> Tuple['Sticker']:
78+
def stickers(self) -> Tuple[Sticker]:
79+
"Tuple[:class:`Sticker`]: The stickers of the pack"
7080
return self.__stickers
7181

7282
@stickers.setter
7383
def stickers(self, value):
7484
raise TypeError('Sticker Packs are immutable.')
7585

7686
@property
77-
def banner_url(self):
87+
def banner_url(self) -> Asset:
88+
"""Returns an :class:`Asset` for the sticker pack's banner.
89+
90+
Returns
91+
-------
92+
:class:`Asset`
93+
The resulting CDN asset.
94+
"""
7895
return self.banner_url_as()
7996

80-
def banner_url_as(self, format: str = 'png', size=1024):
97+
def banner_url_as(self, format: str = 'png', size=1024) -> Asset:
98+
"""Returns an :class:`Asset` for the sticker pack's banner.
99+
100+
The size must be a power of 2 between 16 and 4096.
101+
102+
Returns
103+
-------
104+
:class:`Asset`
105+
The resulting CDN asset.
106+
"""
81107
return Asset._from_sticker_pack(self._state, self, format=format, size=size)
82108

83-
def get_sticker(self, id: int) -> Optional['Sticker']:
109+
@property
110+
def cover_sticker(self) -> Sticker:
111+
""":class:`Sticker`: Returns the sticker in the pack which is shown in the client as the pack's icon."""
112+
return self.get_sticker(self.cover_sticker_id)
113+
114+
def get_sticker(self, id: int) -> Optional[Sticker]:
115+
"""
116+
Returns a sticker of the pack with the given id or :obj:`None` if not found.
117+
118+
Parameters
119+
-----------
120+
id: :class:`int`
121+
The id of the sticker to get
122+
123+
Returns
124+
--------
125+
Optional[:class:`Sticker`]
126+
The sticker or ``None`` if not found.
127+
"""
84128
return utils_get(self.stickers, id=id)
85129

86130

@@ -117,18 +161,21 @@ class Sticker(Hashable):
117161
The format for the sticker's image.
118162
tags: List[:class:`str`]
119163
A list of tags for the sticker.
164+
pack: Optional[:class:`StickerPack`]
165+
The pack the sticker belongs to. Could be :obj:`None` even if :attr:`Sticker.pack_id` is present.
166+
sort_value: :class:`int`
167+
The sticker's sort order within its :attr:`Sticker.pack`.
120168
"""
121-
__slots__ = ('_state', 'id', 'name', 'description', 'pack_id', 'format', 'tags', 'sort_value', 'available', 'pack')
169+
__slots__ = ('_state', 'id', 'name', 'description', 'pack_id', 'format', 'tags', 'sort_value' , 'pack')
122170

123-
def __init__(self, *, state, data, pack: Optional[StickerPack] = None):
171+
def __init__(self, *, state, data, pack: Optional[StickerPack] = None) -> None:
124172
self._state = state
125173
self.id = int(data['id'])
126174
self.name: str = data['name']
127175
self.description: str = data.get('description', None)
128176
self.pack_id: int = int(data.get('pack_id', 0))
129177
self.format: StickerType = try_enum(StickerType, data['format_type'])
130178
self.sort_value = data.get('sort_value', 0)
131-
self.available = data.get('available', True)
132179
try:
133180
self.tags = [tag.strip() for tag in data['tags'].split(',')]
134181
except KeyError:
@@ -142,12 +189,12 @@ def __str__(self):
142189
return self.name
143190

144191
@property
145-
def created_at(self):
192+
def created_at(self) -> datetime:
146193
""":class:`datetime.datetime`: Returns the sticker's creation time in UTC as a naive datetime."""
147194
return snowflake_time(self.id)
148195

149196
@property
150-
def image_url(self):
197+
def image_url(self) -> Asset:
151198
"""Returns an :class:`Asset` for the sticker's image.
152199
153200
.. note::
@@ -160,7 +207,7 @@ def image_url(self):
160207
"""
161208
return self.image_url_as()
162209

163-
def image_url_as(self):
210+
def image_url_as(self) -> Asset:
164211
"""Optionally returns an :class:`Asset` for the sticker's image.
165212
166213
The size must be a power of 2 between 16 and 4096.
@@ -180,27 +227,52 @@ def image_url_as(self):
180227

181228

182229
class GuildSticker(Sticker):
183-
"""Represents a "custom" Sticker in a guild"""
184-
def __init__(self, *, state: 'ConnectionState', data):
230+
"""
231+
Represents a "custom" sticker in a guild
232+
233+
Attributes
234+
----------
235+
name: :class:`str`
236+
The sticker's name.
237+
id: :class:`int`
238+
The id of the sticker.
239+
description: :class:`str`
240+
The description of the sticker.
241+
format: :class:`StickerType`
242+
The format for the sticker's image.
243+
tags: List[:class:`str`]
244+
A list of tags for the sticker.
245+
guild_id: :class:`int`
246+
The id of the guild wich this sticker belongs to.
247+
available: :class:`bool`
248+
Whether this guild sticker can be used, may be :obj:`False` due to loss of Server Boosts
249+
user: :class:`User`
250+
The user that uploaded the guild sticker
251+
"""
252+
__slots__ = ('_state', 'id', 'name', 'description', 'format', 'tags', 'available', 'guild_id', 'user')
253+
254+
def __init__(self, *, state: ConnectionState, data) -> None:
185255
super().__init__(state=state, data=data)
186-
self.guild_id = int(data['guild_id'])
256+
self.guild_id: int = int(data['guild_id'])
257+
self.available = data.get('available', True)
187258
try:
188259
user = data['user']
189260
except KeyError:
190-
self.user = None
261+
self.user: Optional[User] = None
191262
else:
192-
self.user = state.store_user(user)
263+
self.user: Optional[User] = state.store_user(user)
193264

194265
@property
195-
def guild(self):
266+
def guild(self) -> Guild:
267+
""":class:`Guild`: The guild the sticker belongs to"""
196268
return self._state._get_guild(self.guild_id)
197269

198270
def _update(self, data):
199271
for k, v in data.items():
200272
setattr(self, k, v)
201273
return self
202274

203-
async def edit(self, *, reason: Optional[str] = None, **fields):
275+
async def edit(self, *, reason: Optional[str] = None, **fields) -> GuildSticker:
204276
"""|coro|
205277
206278
Modify the sticker.
@@ -263,15 +335,15 @@ async def edit(self, *, reason: Optional[str] = None, **fields):
263335
data = await self._state.http.edit_guild_sticker(guild_id=self.guild_id, sticker_id=self.id, data=fields, reason=reason)
264336
return self._update(data)
265337

266-
async def delete(self, *, reason: Optional[str]):
338+
async def delete(self, *, reason: Optional[str]) -> GuildSticker:
267339
"""|coro|
268340
269341
Delete the sticker.
270342
271343
Requires the ``MANAGE_EMOJIS_AND_STICKERS`` permission.
272344
273345
Parameters
274-
----------
346+
-----------
275347
reason: Optional[:class:`str`]
276348
The reason for deleting the sticker, shows up in the audit-log.
277349
@@ -282,8 +354,8 @@ async def delete(self, *, reason: Optional[str]):
282354
discord.HTTPException:
283355
Deleting the sticker failed.
284356
285-
Return
286-
------
357+
Returns
358+
-------
287359
discord.GuildSticker:
288360
The sticker that was deleted.
289361
"""

docs/api.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3485,14 +3485,29 @@ Widget
34853485
.. autoclass:: Widget()
34863486
:members:
34873487

3488+
StickerPack
3489+
~~~~~~~~~~~~
3490+
3491+
.. attributetable:: StickerPack
3492+
3493+
.. autoclass:: StickerPack()
3494+
:members:
3495+
34883496
Sticker
3489-
~~~~~~~~~~~~~~~
3497+
~~~~~~~~
34903498

34913499
.. attributetable:: Sticker
34923500

34933501
.. autoclass:: Sticker()
34943502
:members:
34953503

3504+
.. attributetable:: GuildSticker
3505+
3506+
.. autoclass:: GuildSticker()
3507+
:inherited-members:
3508+
:members:
3509+
:exclude-members: pack, pack_id, sort_value
3510+
34963511
RawMessageDeleteEvent
34973512
~~~~~~~~~~~~~~~~~~~~~~~
34983513

0 commit comments

Comments
 (0)