Skip to content

Commit 9362d1a

Browse files
DamegoEepyElvyrapre-commit-ci[bot]
authored
feat: Add helper methods for sticker (#1047)
* feat: Add helper methods for sticker * fix: add missed return * fix: lmao discord * refactor: add missed client check * ref: optional return Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * docs: optional return Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * docs: improve docstings Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ci: correct from checks. * feat: add support for `str` type Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * docs: improve docstings Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ref: remove check for `Snowflake` type Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ci: correct from checks. * docs: improve docstings Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ref: remove check for `Snowflake` type Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ci: correct from checks. * feat: add `str` support Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * ci: correct from checks. * fix: description is optional but not optional? * ci: correct from checks. * ref: use None * refactor: add missed converter * refactor: remove sticker from guild and add some checks * refactor: add `StickerPack` to `__all__` * ci: correct from checks. Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7409248 commit 9362d1a

File tree

3 files changed

+188
-4
lines changed

3 files changed

+188
-4
lines changed

interactions/api/http/sticker.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from aiohttp import FormData
44

55
from ...api.cache import Cache
6+
from ..models.misc import File
67
from .request import _Request
78
from .route import Route
89

@@ -54,18 +55,24 @@ async def get_guild_sticker(self, guild_id: int, sticker_id: int) -> dict:
5455
return await self._req.request(Route("GET", f"/guilds/{guild_id}/stickers/{sticker_id}"))
5556

5657
async def create_guild_sticker(
57-
self, payload: FormData, guild_id: int, reason: Optional[str] = None
58+
self, payload: dict, file: File, guild_id: int, reason: Optional[str] = None
5859
) -> dict:
5960
"""
6061
Create a new sticker for the guild. Requires the MANAGE_EMOJIS_AND_STICKERS permission.
6162
62-
:param payload: the payload to send.
63+
:param payload: The payload to send.
64+
:param file: The file to send.
6365
:param guild_id: The guild to create sticker at.
6466
:param reason: The reason for this action.
6567
:return: The new sticker data on success.
6668
"""
69+
data = FormData()
70+
data.add_field("file", file._fp, filename=file._filename)
71+
for key, value in payload.items():
72+
data.add_field(key, value)
73+
6774
return await self._req.request(
68-
Route("POST", f"/guilds/{guild_id}/stickers"), json=payload, reason=reason
75+
Route("POST", f"/guilds/{guild_id}/stickers"), data=data, reason=reason
6976
)
7077

7178
async def modify_guild_sticker(

interactions/api/models/guild.py

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
from .channel import Channel, ChannelType, Thread, ThreadMember
1616
from .emoji import Emoji
1717
from .member import Member
18-
from .message import Sticker
18+
from .message import Sticker, StickerPack
1919
from .misc import (
2020
AutoModAction,
2121
AutoModTriggerMetadata,
2222
AutoModTriggerType,
23+
File,
2324
IDMixin,
2425
Image,
2526
Overwrite,
@@ -2019,6 +2020,158 @@ async def delete_emoji(
20192020
if int(item.id) == int(emoji_id):
20202021
return self.emojis.remove(item)
20212022

2023+
async def get_stickers(self) -> Optional[List[Sticker]]:
2024+
"""
2025+
Get the stickers for a guild.
2026+
2027+
:return: List of stickers of the guild.
2028+
:rtype: Optional[List[Sticker]]
2029+
"""
2030+
if not self._client:
2031+
raise LibraryException(code=13)
2032+
2033+
res = await self._client.list_guild_stickers(guild_id=int(self.id))
2034+
2035+
stickers = [Sticker(**sticker) for sticker in res]
2036+
self.stickers = stickers
2037+
2038+
return self.stickers
2039+
2040+
async def get_nitro_sticker_packs(self) -> List[StickerPack]:
2041+
"""
2042+
Gets the list of sticker packs available to Nitro subscribers.
2043+
2044+
:return: List of sticker packs.
2045+
:rtype: List[StickerPack]
2046+
"""
2047+
if not self._client:
2048+
raise LibraryException(code=13)
2049+
2050+
res = await self._client.list_nitro_sticker_packs()
2051+
2052+
return [StickerPack(**sticker_pack) for sticker_pack in res["sticker_packs"]]
2053+
2054+
async def create_sticker(
2055+
self,
2056+
file: File,
2057+
tags: str,
2058+
name: Optional[str] = MISSING,
2059+
description: Optional[str] = MISSING,
2060+
reason: Optional[str] = None,
2061+
) -> Sticker:
2062+
"""
2063+
Creates a new sticker for the guild.
2064+
2065+
:param file: The file of the sticker.
2066+
:type file: File
2067+
:param tags: The tags of the sticker.
2068+
:type tags: str
2069+
:param name?: The name of the sticker.
2070+
:type name?: Optional[str]
2071+
:param description?: The description of the sticker.
2072+
:type description?: Optional[str]
2073+
:param reason?: The reason of the creation.
2074+
:type reason?: Optional[str]
2075+
:return: Created sticker for the guild.
2076+
:rtype: Sticker
2077+
"""
2078+
if not self._client:
2079+
raise LibraryException(code=13)
2080+
2081+
_name = name if name is not MISSING else file._filename
2082+
2083+
payload: dict = {
2084+
"name": _name,
2085+
"tags": tags,
2086+
"description": description if description is not MISSING else None,
2087+
}
2088+
2089+
res = await self._client.create_guild_sticker(
2090+
payload=payload, file=file, guild_id=int(self.id), reason=reason
2091+
)
2092+
2093+
if self.stickers is None:
2094+
self.stickers = []
2095+
2096+
_sticker = Sticker(**res)
2097+
self.stickers.append(_sticker)
2098+
return _sticker
2099+
2100+
async def modify_sticker(
2101+
self,
2102+
sticker_id: Union[Sticker, Snowflake, int, str],
2103+
name: Optional[str] = MISSING,
2104+
description: Optional[str] = MISSING,
2105+
reason: Optional[str] = None,
2106+
) -> Sticker:
2107+
"""
2108+
Modifies the sticker of the guild.
2109+
2110+
:param sticker_id: The sticker or ID of the sticker.
2111+
:type sticker_id: Union[Sticker, Snowflake, int]
2112+
:param name?: The name of the sticker.
2113+
:type name?: Optional[str]
2114+
:param description?: The description of the sticker.
2115+
:type description?: Optional[str]
2116+
:param reason?: The reason of the modification.
2117+
:type reason?: Optional[str]
2118+
:return: Modified sticker.
2119+
:rtype: Sticker
2120+
"""
2121+
if not self._client:
2122+
raise LibraryException(code=13)
2123+
2124+
_id = int(sticker_id.id) if isinstance(sticker_id, Sticker) else int(sticker_id)
2125+
2126+
payload: dict = {}
2127+
2128+
if name is not MISSING:
2129+
payload["name"] = name
2130+
if description is not MISSING:
2131+
payload["description"] = description
2132+
2133+
res = await self._client.modify_guild_sticker(
2134+
payload=payload, guild_id=int(self.id), sticker_id=_id, reason=reason
2135+
)
2136+
_sticker = Sticker(**res)
2137+
2138+
if not self.stickers:
2139+
self.stickers = [_sticker]
2140+
return _sticker
2141+
2142+
for sticker in self.stickers:
2143+
if sticker.id == _sticker.id:
2144+
sticker.update(res)
2145+
return sticker
2146+
2147+
async def delete_sticker(
2148+
self,
2149+
sticker_id: Union[Sticker, Snowflake, int, str],
2150+
reason: Optional[str] = None,
2151+
):
2152+
"""Deletes the sticker of the guild.
2153+
2154+
:param sticker_id: The sticker or ID of the sticker.
2155+
:type sticker_id: Union[Sticker, Snowflake, int]
2156+
:param reason?: The reason of the deletion.
2157+
:type reason?: Optional[str]
2158+
"""
2159+
if not self._client:
2160+
raise LibraryException(code=13)
2161+
2162+
_id = int(sticker_id.id) if isinstance(sticker_id, Sticker) else int(sticker_id)
2163+
2164+
await self._client.delete_guild_sticker(
2165+
guild_id=int(self.id), sticker_id=_id, reason=reason
2166+
)
2167+
2168+
if not self.stickers:
2169+
return
2170+
for sticker in self.stickers:
2171+
if int(sticker.id) == _id:
2172+
self.stickers.remove(sticker)
2173+
break
2174+
20222175
async def get_list_of_members(
20232176
self,
20242177
limit: Optional[int] = 1,

interactions/api/models/message.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"ReactionObject",
4444
"PartialSticker",
4545
"Sticker",
46+
"StickerPack",
4647
)
4748

4849

@@ -704,6 +705,29 @@ class Sticker(PartialSticker, IDMixin):
704705
sort_value: Optional[int] = field(default=None)
705706

706707

708+
@define()
709+
class StickerPack(DictSerializerMixin, IDMixin):
710+
"""
711+
A class objects representing a pack of stickers.
712+
713+
:ivar Snowflake id: ID of the sticker pack.
714+
:ivar List[Sticker] stickers: The stickers in the pack.
715+
:ivar str name: The name of sticker pack.
716+
:ivar Snowflake sku_id: ID of the pack's SKU.
717+
:ivar Optional[Snowflake] cover_sticker_id?: ID of a sticker in the pack which is shown as the pack's icon.
718+
:ivar str description: The description of sticker pack.
719+
:ivar Optional[Snowflake] banned_asset_id?: ID of the sticker pack's banner image.
720+
"""
721+
722+
id: Snowflake = field(converter=Snowflake)
723+
stickers: List[Sticker] = field(converter=convert_list(Sticker))
724+
name: str = field()
725+
sku_id: Snowflake = field(converter=Snowflake)
726+
cover_sticker_id: Optional[Snowflake] = field(converter=Snowflake, default=None)
727+
description: str = field()
728+
banned_asset_id: Optional[Snowflake] = field(converter=Snowflake, default=None)
729+
730+
707731
@define()
708732
class ReactionObject(DictSerializerMixin):
709733
"""The reaction object.

0 commit comments

Comments
 (0)