Skip to content

Commit

Permalink
made Message compact with msg/view and quoted message
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence114 committed Jan 10, 2024
1 parent d19cb14 commit 81ff109
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 186 deletions.
8 changes: 8 additions & 0 deletions khl/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ def list(
):
...

@staticmethod
@req('GET')
def view(
chat_code,
msg_id
):
...

@staticmethod
@req('POST')
def create(
Expand Down
10 changes: 5 additions & 5 deletions khl/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def __init__(self, **kwargs):
self.is_friend: bool = kwargs.get('is_friend')
self.is_blocked: bool = kwargs.get('is_blocked')
self.is_target_blocked: bool = kwargs.get('is_target_blocked')
self.target_info: Dict = kwargs.get('target_info')
self.target_info: User = kwargs.get('target_info')

self._loaded = kwargs.get('_lazy_loaded_', False)
self.gate = kwargs.get('_gate_')
Expand All @@ -231,22 +231,22 @@ def id(self) -> str:
@property
def target_user_id(self) -> str:
"""prop, the target's id"""
return self.target_info.get('id') if self.target_info else None
return self.target_info.id if self.target_info else None

@property
def target_user_name(self) -> str:
"""prop, the target's name"""
return self.target_info.get('username') if self.target_info else None
return self.target_info.username if self.target_info else None

@property
def is_target_user_online(self) -> bool:
"""prop, is the target online"""
return self.target_info.get('online') if self.target_info else None
return self.target_info.online if self.target_info else None

@property
def target_user_avatar(self) -> str:
"""prop, the target's avatar"""
return self.target_info.get('avatar') if self.target_info else None
return self.target_info.avatar if self.target_info else None

async def send(self, content: Union[str, List], *, type: MessageTypes = None, **kwargs):
return await User(id=self.id, _gate_=self.gate).send(content, type=type, **kwargs)
8 changes: 8 additions & 0 deletions khl/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ async def fetch_blocked_friends(self) -> List[Friend]:
friends = (await self.gate.exec_req(api.friend(type='blocked')))['blocked']
return [Friend(_gate_=self.gate, user_id=i['friend_info']['id'], **i) for i in friends]

async def fetch_message(self, msg_id: str):
req = api.Message.view(msg_id=msg_id)
return PublicMessage(_gate_=self.gate, **(await self.gate.exec_req(req)))

async def fetch_direct_message(self, chat_code: str, msg_id: str):
req = api.DirectMessage.view(chat_code=chat_code, msg_id=msg_id)
return PrivateMessage(code=chat_code, _gate_=self.gate, **(await self.gate.exec_req(req)))

async def offline(self):
"""offline bot"""
await self.gate.exec_req(api.User.offline())
Expand Down
4 changes: 2 additions & 2 deletions khl/command/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import copy
import inspect
import logging
from typing import Dict, Any, Callable, List, Union
from typing import Dict, Any, Callable, List, Union, Type

from khl import User, Channel, Client, Message, Role
from .exception import Exceptions
Expand Down Expand Up @@ -54,7 +54,7 @@ class Parser:
"""
deal with a list of tokens made from Lexer, convert their type to match the command.handler
"""
_parse_funcs: Dict[Any, Callable] = {
_parse_funcs: Dict[Type[Any], Callable[[Message, Client, str], Any]] = {
str: lambda msg, client, token: token,
int: lambda msg, client, token: int(token),
float: lambda msg, client, token: float(token),
Expand Down
14 changes: 10 additions & 4 deletions khl/command/rule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Union, Callable, Coroutine, Any

from khl import Message, User
from khl import Message, PublicMessage, User

TypeRule = Callable[[Message], Union[bool, Coroutine[Any, Any, bool]]]

Expand All @@ -13,7 +13,9 @@ def is_bot_mentioned(bot) -> TypeRule:
""":return: a Rule that checks if the bot is mentioned"""

async def rule(msg: Message) -> bool:
return (await bot.fetch_me()).id in msg.extra.get('mention')
if not isinstance(msg, PublicMessage):
return False
return (await bot.fetch_me()).id in msg.mention

return rule

Expand All @@ -22,14 +24,18 @@ def is_user_mentioned(user: User) -> TypeRule:
""":return: a Rule that checks if the user is mentioned"""

def rule(msg: Message) -> bool:
return user.id in msg.extra.get('mention')
if not isinstance(msg, PublicMessage):
return False
return user.id in msg.mention

return rule

@staticmethod
def is_mention_all(msg: Message) -> bool:
""":return: a Rule that checks if the msg mentioned all members"""
return msg.extra.get('mention_all', None) is not None
if not isinstance(msg, PublicMessage):
return False
return msg.mention_all

@staticmethod
def is_not_bot(msg: Message) -> bool:
Expand Down
Loading

0 comments on commit 81ff109

Please sign in to comment.