Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

♻️ Using a Custom class for Generator to be awaitable #403

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions pincer/objects/guild/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
from asyncio import sleep, ensure_future
from dataclasses import dataclass
from enum import IntEnum
from urllib.parse import urlencode
from typing import AsyncIterator, overload, TYPE_CHECKING

from .invite import Invite, InviteTargetType
from ..message.user_message import UserMessage
from ..._config import GatewayConfig
from ...utils.api_data import APIDataGen
from ...utils.api_object import APIObject, GuildProperty
from ...utils.convert_message import convert_message
from ...utils.types import MISSING

if TYPE_CHECKING:
from typing import AsyncGenerator, Dict, List, Optional, Union
from typing import Dict, List, Optional, Union

from .member import GuildMember
from .overwrite import Overwrite
Expand Down Expand Up @@ -351,7 +351,7 @@ async def trigger_typing_indicator(self):
"""
await self._http.post(f"channels/{self.id}/typing")

async def get_pinned_messages(self) -> AsyncIterator[UserMessage]:
def get_pinned_messages(self) -> APIDataGen[UserMessage]:
"""|coro|
Fetches all pinned messages in the channel. Returns an iterator of
pinned messages.
Expand All @@ -361,9 +361,10 @@ async def get_pinned_messages(self) -> AsyncIterator[UserMessage]:
:class:`AsyncIterator[:class:`~pincer.objects.guild.message.UserMessage`]`
An iterator of pinned messages.
"""
data = await self._http.get(f"channels/{self.id}/pins")
for message in data:
yield UserMessage.from_dict(message)
return APIDataGen(
UserMessage,
self._http.get(f"channels/{self.id}/pins")
)

async def pin_message(
self, message: UserMessage, reason: Optional[str] = None
Expand Down Expand Up @@ -520,7 +521,7 @@ async def send(self, message: Union[Embed, Message, str]) -> UserMessage:
self.__post_sent(msg)
return msg

async def get_webhooks(self) -> AsyncGenerator[Webhook, None]:
def get_webhooks(self) -> APIDataGen[Webhook]:
"""|coro|
Get all webhooks in the channel.
Requires the ``MANAGE_WEBHOOKS`` permission.
Expand All @@ -529,11 +530,12 @@ async def get_webhooks(self) -> AsyncGenerator[Webhook, None]:
-------
AsyncGenerator[:class:`~.pincer.objects.guild.webhook.Webhook`, None]
"""
data = await self._http.get(f"channels/{self.id}/webhooks")
for webhook_data in data:
yield Webhook.from_dict(webhook_data)
return APIDataGen(
Webhook,
self._http.get(f"channels/{self.id}/webhooks")
)

async def get_invites(self) -> AsyncIterator[Invite]:
def get_invites(self) -> APIDataGen[Invite]:
"""|coro|
Fetches all the invite objects for the channel. Only usable for
guild channels. Requires the ``MANAGE_CHANNELS`` permission.
Expand All @@ -543,9 +545,10 @@ async def get_invites(self) -> AsyncIterator[Invite]:
AsyncIterator[:class:`~pincer.objects.guild.invite.Invite`]
Invites iterator.
"""
data = await self._http.get(f"channels/{self.id}/invites")
for invite in data:
yield Invite.from_dict(invite)
return APIDataGen(
Invite,
self._http.get(f"channels/{self.id}/invites")
)

async def create_invite(
self,
Expand Down Expand Up @@ -1092,7 +1095,7 @@ async def get_member(self, user: User) -> ThreadMember:
await self._http.get(f"channels/{self.id}/thread-members/{user.id}")
)

async def list_members(self) -> AsyncIterator[ThreadMember]:
def list_members(self) -> APIDataGen[ThreadMember]:
"""|coro|
Fetches all the thread members for the thread. Returns an iterator of
ThreadMember objects.
Expand All @@ -1102,9 +1105,10 @@ async def list_members(self) -> AsyncIterator[ThreadMember]:
AsyncIterator[:class:`~pincer.objects.channel.ThreadMember`]
An iterator of thread members.
"""
data = await self._http.get(f"channels/{self.id}/thread-members")
for member in data:
yield ThreadMember.from_dict(member)
return APIDataGen(
ThreadMember,
self._http.get(f"channels/{self.id}/thread-members")
)


class PublicThread(Thread):
Expand Down
Loading