From b6aa4791ec2967c002e3c6727cc03591fdf9ef28 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Thu, 7 Apr 2022 20:56:29 +0300 Subject: [PATCH] Use message model in some methods --- melisa/models/guild/channel.py | 51 +++++++++++++++++++++++-------- melisa/models/message/__init__.py | 11 ++++++- melisa/models/message/message.py | 16 +++++----- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/melisa/models/guild/channel.py b/melisa/models/guild/channel.py index 2fdab38..fb5ea1a 100644 --- a/melisa/models/guild/channel.py +++ b/melisa/models/guild/channel.py @@ -6,12 +6,15 @@ from __future__ import annotations import asyncio from dataclasses import dataclass from enum import IntEnum -from typing import List, Any, Optional, AsyncIterator, Union, Dict, overload +from typing import List, Any, Optional, AsyncIterator, Union, Dict, overload, TYPE_CHECKING +from ..message.message import Message from ...utils import Snowflake, Timestamp from ...utils import APIModelBase from ...utils.types import APINullable -from .thread import ThreadMember, ThreadMetadata + +if TYPE_CHECKING: + from .thread import ThreadMember, ThreadMetadata class ChannelType(IntEnum): @@ -165,7 +168,7 @@ class Channel(APIModelBase): owner_id: APINullable[Snowflake] = None application_id: APINullable[Snowflake] = None parent_id: APINullable[Snowflake] = None - last_pin_timestamp: APINullable[int] = None + last_pin_timestamp: APINullable[Timestamp] = None rtc_region: APINullable[str] = None video_quality_mode: APINullable[int] = None message_count: APINullable[int] = None @@ -314,14 +317,15 @@ class MessageableChannel(Channel): before: Optional[Snowflake] = None, after: Optional[Snowflake] = None, around: Optional[Snowflake] = None, - ) -> AsyncIterator[Dict[str, Any]]: + ) -> AsyncIterator[Message]: """|coro| Returns a list of messages in this channel. Examples --------- - Flattening messages into a list: :: + Flattening messages into a list: + .. code-block:: python messages = [message async for message in channel.history(limit=111)] All parameters are optional. @@ -346,7 +350,7 @@ class MessageableChannel(Channel): Returns ------- - AsyncIterator[Dict[:class:`str`, Any]] + AsyncIterator[:class:`~melisa.Message`] An iterator of messages. """ @@ -372,7 +376,7 @@ class MessageableChannel(Channel): break for message_data in raw_messages: - yield message_data + yield Message.from_dict(message_data) before = raw_messages[-1]["id"] limit -= search_limit @@ -380,7 +384,7 @@ class MessageableChannel(Channel): async def fetch_message( self, message_id: Optional[Snowflake, int, str], - ) -> Dict[str, Any]: + ) -> Message: """|coro| Returns a specific message in the channel. @@ -399,7 +403,7 @@ class MessageableChannel(Channel): Returns ------- - Dict[:class:`str`, Any] + :class:`~melisa.Message` Message object. """ @@ -407,7 +411,30 @@ class MessageableChannel(Channel): f"/channels/{self.id}/messages/{message_id}", ) - return message + return Message.from_dict(message) + + async def pins(self) -> AsyncIterator[Message]: + """|coro| + + Retrieves all messages that are currently pinned in the channel. + + Raises + ------- + HTTPException + The request to perform the action failed with other http exception. + + Returns + ------- + AsyncIterator[:class:`~melisa.Message`] + AsyncIterator of Message objects. + """ + + messages = await self._http.get( + f"/channels/{self.id}/pins", + ) + + for message in messages: + yield Message.from_dict(message) async def bulk_delete_messages( self, messages: List[Snowflake], *, reason: Optional[str] = None @@ -719,9 +746,9 @@ class ThreadsList(APIModelBase): Attributes ---------- - threads: List[:class:`~melisa.models.guild.channel.Thread`] + threads: List[:class:`~melisa.Thread`] Async iterator of threads. To get their type use them `.type` attribute. - members: List[:class:`Any`] + members: List[:class:`~melisa.ThreadMember`] Async iterator of thread members. has_more: Optional[:class:`bool`] Whether there are potentially additional threads that could be returned on a subsequent cal diff --git a/melisa/models/message/__init__.py b/melisa/models/message/__init__.py index c2ecb2d..29c3db0 100644 --- a/melisa/models/message/__init__.py +++ b/melisa/models/message/__init__.py @@ -1,4 +1,13 @@ # Copyright MelisaDev 2022 - Present # Full MIT License can be found in `LICENSE.txt` at the project root. -from .message import * +from .message import ( + MessageActivityType, MessageFlags, MessageType, Message +) + +__all__ = ( + "MessageActivityType", + "MessageFlags", + "MessageType", + "Message" +) diff --git a/melisa/models/message/message.py b/melisa/models/message/message.py index 710ea48..baa1de5 100644 --- a/melisa/models/message/message.py +++ b/melisa/models/message/message.py @@ -5,16 +5,18 @@ from __future__ import annotations from dataclasses import dataclass from enum import IntEnum -from typing import List, Any, Optional +from typing import List, TYPE_CHECKING -from ... import Thread from ...utils import Snowflake, Timestamp from ...utils import APIModelBase from ...utils.types import APINullable +if TYPE_CHECKING: + from ..guild.channel import Thread -class MessageTypes(IntEnum): - """Message Types + +class MessageType(IntEnum): + """Message Type NOTE: Type `19` and `20` are only in API v8. In v6, they are still type `0`. Type `21` is only in API v9. """ @@ -46,8 +48,8 @@ class MessageTypes(IntEnum): return self.value -class MessageActivityTypes(IntEnum): - """Message Activity Types""" +class MessageActivityType(IntEnum): + """Message Activity Type""" JOIN = 1 SPECTATE = 2 @@ -191,7 +193,7 @@ class Message(APIModelBase): message_reference: APINullable[List] = None flags: APINullable[int] = None interaction: APINullable[List] = None - thread: APINullable[List[Thread]] = None + thread: APINullable[Thread] = None components: APINullable[List] = None sticker_items: APINullable[List] = None stickers: APINullable[List] = None