Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加消息的引用内容 #218

Merged
merged 5 commits into from
Nov 12, 2023
Merged
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
97 changes: 96 additions & 1 deletion khl/message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, List, Dict, Union
from typing import Any, List, Dict, Union, Optional

import json

Expand All @@ -11,6 +11,65 @@
from ._types import MessageTypes, ChannelPrivacyTypes, EventTypes
from .user import User, GuildUser


class QuotedMessage(Requestable, ABC):
"""Basic quote message"""
_msg_id: str
_type: int
_author: User
content: str
create_at: int

def __init__(self, **kwargs):
self._msg_id = kwargs.get('rong_id')
self._type = kwargs.get('type')
self.content = kwargs.get('content')
self.create_at = kwargs.get('create_at')
self.gate = kwargs.get('_gate_', None)

@property
def id(self):
"""quote message's id"""
return self._msg_id

@property
def type(self):
"""quote message's type, refer to MessageTypes for enum detail"""
return MessageTypes(self._type)

@property
def author(self):
"""quote message author"""
return self._author


class PublicQuotedMessage(QuotedMessage):
"""quote messages sent in a `PublicTextChannel`"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self._author = GuildUser(**kwargs.get('author'), _gate_=self.gate, _lazy_loaded_=True)

@property
def author(self) -> GuildUser:
"""quote message author"""
return self._author


class PrivateQuotedMessage(QuotedMessage):
"""quote messages sent in a `PrivateChannel`"""
_author: User

def __init__(self, **kwargs):
super().__init__(**kwargs)
self._author = User(**kwargs.get('author'), _gate_=self.gate, _lazy_loaded_=True)

@property
def author(self) -> User:
"""quote message author"""
return self._author


class RawMessage(ABC):
"""
Basic and common features of kinds of messages.
Expand Down Expand Up @@ -69,6 +128,7 @@ class Message(RawMessage, Requestable, ABC):
"""
_ctx: Context
_author: User
_quote: Optional[QuotedMessage]

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -84,6 +144,15 @@ def ctx(self) -> Context:
"""message context: channel, guild etc."""
return self._ctx

@property
def quote(self):
"""
get quote of the message

If the quote does not exist, it will return None
"""
return self._quote

@abstractmethod
async def add_reaction(self, emoji: str):
"""add emoji to msg's reaction list
Expand Down Expand Up @@ -147,6 +216,10 @@ def __init__(self, **kwargs):
guild = Guild(id=self.extra['guild_id'], _gate_=self.gate)
self._ctx = Context(channel=channel, guild=guild, _gate_=self.gate)
self._author = GuildUser(**self.extra['author'], _gate_=self.gate, _lazy_loaded_=True)
if 'quote' in self.extra:
self._quote = PublicQuotedMessage(**self.extra['quote'], _gate_=self.gate, _lazy_loaded_=True)
else:
self._quote = None

@property
def author(self) -> GuildUser:
Expand Down Expand Up @@ -185,6 +258,15 @@ def mention_here(self) -> bool:
"""if the message mentioned(also call as at/tagged) all online users in the channel"""
return self.extra['mention_here']

@property
def quote(self) -> PublicQuotedMessage:
"""
get quote of the message

If the quote does not exist, it will return None
"""
return self._quote

async def add_reaction(self, emoji: str):
return await self.gate.exec_req(api.Message.addReaction(msg_id=self.id, emoji=emoji))

Expand Down Expand Up @@ -226,6 +308,10 @@ def __init__(self, **kwargs):
self._channel = PrivateChannel(code=self.extra['code'], target_info=self.extra['author'], _gate_=self.gate)
self._ctx = Context(channel=self._channel, _gate_=self.gate)
self._author = User(**self.extra['author'], _gate_=self.gate, _lazy_loaded_=True)
if 'quote' in self.extra:
self._quote = PrivateQuotedMessage(**self.extra['quote'], _gate_=self.gate, _lazy_loaded_=True)
else:
self._quote = None

@property
def chat_code(self) -> str:
Expand All @@ -237,6 +323,15 @@ def channel(self) -> PrivateChannel:
"""the message's channel"""
return self._channel

@property
def quote(self) -> PrivateQuotedMessage:
"""
get quote of the message

If the quote does not exist, it will return None
"""
return self._quote

async def add_reaction(self, emoji: str):
return await self.gate.exec_req(api.DirectMessage.addReaction(msg_id=self.id, emoji=emoji))

Expand Down
Loading