Skip to content

Commit

Permalink
🎨 优化代码结构,事件添加更多属性,更新Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
CMHopeSunshine committed Nov 15, 2023
1 parent b68866e commit afa5c87
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 296 deletions.
12 changes: 3 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@ ci:
autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
args: [--preview]
stages: [commit]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.4
rev: v0.1.2
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
stages: [commit]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand Down
14 changes: 10 additions & 4 deletions nonebot/adapters/discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ async def _check_reply(bot: "Bot", event: MessageEvent) -> None:
if not event.message_reference or not event.message_reference.message_id:
return
try:
event.reply = await bot.get_channel_message(
msg = await bot.get_channel_message(
channel_id=event.channel_id, message_id=event.message_reference.message_id
)
if event.reply.author.id == bot.self_info.id:
event.reply = Message.from_guild_message(msg)
if msg.author.id == bot.self_info.id:
event.to_me = True
except Exception as e:
log("WARNING", f"Error when getting message reply info: {repr(e)}", e)
Expand Down Expand Up @@ -90,6 +91,8 @@ class Bot(BaseBot, ApiClient):
Discord 协议 Bot 适配。
"""

adapter: "Adapter"

@override
def __init__(self, adapter: "Adapter", self_id: str, bot_info: BotInfo):
super().__init__(adapter, self_id)
Expand Down Expand Up @@ -186,6 +189,9 @@ async def send(
tts: bool = False,
nonce: Union[int, str, None] = None,
allowed_mentions: Optional[AllowedMention] = None,
mention_sender: Optional[bool] = None,
at_sender: Optional[bool] = None,
reply_message: bool = False,
**params: Any,
) -> Optional[MessageGet]:
"""send message.
Expand Down Expand Up @@ -221,9 +227,9 @@ async def send(
if not isinstance(event, MessageEvent) or not event.channel_id or not event.id:
raise RuntimeError("Event cannot be replied to!")
message = message if isinstance(message, Message) else Message(message)
if (params.get("mention_sender") or params.get("at_sender")) is True:
if mention_sender or at_sender:
message.insert(0, MessageSegment.mention_user(event.user_id))
if params.get("reply_message") is True:
if reply_message:
message += MessageSegment.reference(MessageReference(message_id=event.id))

message_data = parse_message(message)
Expand Down
17 changes: 16 additions & 1 deletion nonebot/adapters/discord/event.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from enum import Enum
from typing import Dict, List, Literal, Optional, Type, Union
from typing_extensions import override
Expand Down Expand Up @@ -124,6 +125,11 @@ class Event(BaseEvent):
"""Event"""

__type__: EventType
timestamp: datetime = Field(default_factory=datetime.now)

@property
def time(self) -> datetime:
return self.timestamp

@override
def get_event_name(self) -> str:
Expand Down Expand Up @@ -179,7 +185,15 @@ class MessageEvent(Event, MessageGet):

to_me: bool = False

reply: Optional[MessageGet] = None
reply: Optional[Message] = None

@property
def message(self) -> Message:
return self.get_message()

@property
def origin_message(self) -> Message:
return getattr(self, "_origin_message", self.get_message())

@override
def get_type(self) -> str:
Expand All @@ -197,6 +211,7 @@ def get_session_id(self) -> str:
def get_message(self) -> Message:
if not hasattr(self, "_message"):
setattr(self, "_message", Message.from_guild_message(self))
setattr(self, "_origin_message", Message.from_guild_message(self))
return getattr(self, "_message")

@override
Expand Down
Loading

0 comments on commit afa5c87

Please sign in to comment.