Skip to content
Open
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
34 changes: 31 additions & 3 deletions khl/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .guild import Guild, GuildBoost, ChannelCategory
from .interface import AsyncRunnable
from .message import RawMessage, Message, Event, PublicMessage, PrivateMessage
from ._types import SoftwareTypes, MessageTypes, SlowModeTypes, GameTypes
from ._types import SoftwareTypes, MessageTypes, SlowModeTypes, GameTypes, MessageFlagModes
from .user import User, Friend, FriendRequest
from .util import unpack_id, unpack_value

Expand Down Expand Up @@ -148,10 +148,14 @@ def me(self) -> User:
return self._me
raise ValueError('not loaded, please call `await fetch_me()` first')

async def fetch_user(self, user: Union[User, str]) -> User:
async def fetch_user(self, user: Union[User, str], guild: Union[Guild, str] = None) -> User:
"""fetch detail of the specific user"""
user_id = unpack_id(user)
return User(_gate_=self.gate, _lazy_loaded_=True, **(await self.gate.exec_req(api.User.view(user_id))))
params = {}
if guild is not None:
params['guild_id'] = unpack_id(guild)
params['user_id'] = user_id
return User(_gate_=self.gate, _lazy_loaded_=True, **(await self.gate.exec_req(api.User.view(**params))))

async def fetch_guild(self, guild_id: str) -> Guild:
"""fetch details of a guild from khl"""
Expand Down Expand Up @@ -363,3 +367,27 @@ async def fetch_blocked_friends(self) -> List[Friend]:

async def start(self):
await asyncio.gather(self.handle_pkg(), self.gate.run(self._pkg_queue))

async def view_message(self, msg_id: str) -> Dict:
"""get message detail by message id"""
return await self.gate.exec_req(api.Message.view(msg_id))

async def list_messages(self,
channel: Union[Channel, str],
page_size: int = None,
pin: int = None,
flag: MessageFlagModes = None,
msg_id: str = None) -> Dict:
"""list the messages by guild id"""
target_id = unpack_id(channel)
params = {'target_id': target_id}
if page_size is not None:
params['page_size'] = page_size
if pin is not None:
params['pin'] = pin
if flag is not None:
params['flag'] = unpack_value(flag)
if msg_id is not None:
params['msg_id'] = msg_id
params['page'] = 0
return await self.gate.exec_req(api.Message.list(**params))
Comment on lines +382 to +393
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marked duplicated by pylint here, a small trick:

Suggested change
target_id = unpack_id(channel)
params = {'target_id': target_id}
if page_size is not None:
params['page_size'] = page_size
if pin is not None:
params['pin'] = pin
if flag is not None:
params['flag'] = unpack_value(flag)
if msg_id is not None:
params['msg_id'] = msg_id
params['page'] = 0
return await self.gate.exec_req(api.Message.list(**params))
if isinstance(channel, str):
channel = PublicChannel(id=channel, _gate_=self.gate)
return await channel.list_messages(page_size,pin,flag,msg_id)