Update client.py#208
Conversation
Add a method 'fetch_guild_user' to obtain the user's nickname for a guild.
Add method ['message_view', 'list_messages] to get message, and fix 'list_messages' bug that defaults to the second page.
TWT233
left a comment
There was a problem hiding this comment.
besides, it's better to tear this commit into two
(one with fetch_guild_user() and message related methods another, since they are different conceptions enclosed)
| await guild.load() | ||
| return guild | ||
|
|
||
| async def fetch_guild_user(self, user: Union[User, str], guild_id: str) -> User: |
There was a problem hiding this comment.
there is impl for user.view() already: Client.fetch_user()
is this an alias?
would be better just wrap the fetch_user() rather than dup it again
| return await self.gate.exec_req(api.Message.view(msg_id)) | ||
|
|
||
| async def list_messages(self, | ||
| target_id: str = None, |
There was a problem hiding this comment.
- it is user-friendlier that expose wrapped concepts to user, rather than underlying mechanism
on the other side, it is user-friendlier to adapt user's kinds of intention
| target_id: str = None, | |
| channel: Union[Channel, str], |
channel is a more frequently talked concept in khl.py, user can recall where to get it/how to user it more easily
and user can pass channel obj or raw channel id on condition
| return await self.gate.exec_req(api.Message.view(msg_id)) | ||
|
|
||
| async def list_messages(self, | ||
| target_id: str = None, |
There was a problem hiding this comment.
- target id is required param in khl api doc
thus no default value for this
| if target_id is not None: | ||
| params['target_id'] = target_id |
There was a problem hiding this comment.
| if target_id is not None: | |
| params['target_id'] = target_id | |
| params['target_id'] = unpack_id(channel) |
accordingly
| 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], channel: Union[Channel, str] = None) -> User: |
There was a problem hiding this comment.
should be guild instead of channel?
according to https://developer.kookapp.cn/doc/http/user#%E8%8E%B7%E5%8F%96%E7%9B%AE%E6%A0%87%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF
| async def start(self): | ||
| await asyncio.gather(self.handle_pkg(), self.gate.run(self._pkg_queue)) | ||
|
|
||
| async def message_view(self, msg_id: str) -> Dict: |
There was a problem hiding this comment.
naming: method name would be better if it is a verb/verb phrase since can convey this is a action/method
| async def message_view(self, msg_id: str) -> Dict: | |
| async def view_message(self, msg_id: str) -> Dict: |
|
|
||
| async def message_view(self, msg_id: str) -> Dict: | ||
| """get message detail by message id""" | ||
| if len(msg_id) > 0: |
There was a problem hiding this comment.
no need to check, for that the kook API will fail if msg id not valid
user is clear that the param is invalid if API fails
but nothing is retern/raised if param not pass the check
| 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)) |
There was a problem hiding this comment.
marked duplicated by pylint here, a small trick:
| 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) |
Add a method 'fetch_guild_user' to obtain the user's nickname for a guild.