Skip to content

Commit

Permalink
修复 Heartbeat 不符合 OneBot V11 标准的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
This-is-XiaoDeng committed Oct 21, 2023
1 parent b2b4d10 commit c3a780b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
6 changes: 2 additions & 4 deletions basic_api_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from api import register_action
from discord.abc import PrivateChannel
from discord.channel import CategoryChannel, ForumChannel
from discord.types.channel import ThreadChannel
from logger import get_logger
import translator
import message_parser_v11
Expand All @@ -12,7 +11,6 @@
import message_parser_v11
import version


logger = get_logger()

@register_action("v11")
Expand Down Expand Up @@ -123,7 +121,7 @@ async def get_msg(message_id: int) -> dict:
async def set_group_kick(group_id: int, user_id: int, reason: str | None = None, reject_add_request: bool = False) -> dict:
if not (group := client.get_channel(group_id)):
return return_object.get(1400, f"不存在的频道:{group_id}")
if isinstance(group, PrivateChannel | ForumChannel | ThreadChannel | CategoryChannel):
if isinstance(group, PrivateChannel | ForumChannel | CategoryChannel):
return return_object.get(1400, f"不支持的操作:从 {type(group)} 中移除 {user_id}")
for user in group.members:
if user.id == user_id:
Expand All @@ -137,7 +135,7 @@ async def set_group_kick(group_id: int, user_id: int, reason: str | None = None,
async def set_group_ban(group_id: int, user_id: int, duration: int = 1800, reason: str | None = None) -> dict:
if not (group := client.get_channel(group_id)):
return return_object.get(1400, f"不存在的频道:{group_id}")
if isinstance(group, PrivateChannel | ForumChannel | ThreadChannel | CategoryChannel):
if isinstance(group, PrivateChannel | ForumChannel | CategoryChannel):
return return_object.get(1400, f"不支持的操作:从 {type(group)} 中禁言 {user_id}")
for user in group.members:
if user.id == user_id:
Expand Down
2 changes: 1 addition & 1 deletion http_post_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def push_event(self, _event: dict) -> None:
Args:
event (dict): 事件
"""
event = translator.translate_event(_event)
event = await translator.translate_event(_event)
async with httpx.AsyncClient(timeout=self.config["timeout"]) as client:
response = await client.post(
self.config["url"],
Expand Down
5 changes: 4 additions & 1 deletion translator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from config import config
import message_parser_v11
from client import client
import basic_api_v11
from logger import get_logger

logger = get_logger()


def translate_event(_event: dict) -> dict:
async def translate_event(_event: dict) -> dict:
event = _event.copy()
# 键名替换
event["time"] = int(event["time"])
Expand Down Expand Up @@ -49,6 +50,8 @@ def translate_event(_event: dict) -> dict:
"card": sender.display_name
})
event["message"] = message_parser_v11.parse_text(event["raw_message"])
elif event["post_type"] == "meta_event" and event["meta_event_type"] == "heartbeat":
event["status"] = (await basic_api_v11.get_status())["data"]
logger.debug(event)
return event

Expand Down
8 changes: 4 additions & 4 deletions ws_reverse_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import websockets.client
import websockets.exceptions
from logger import get_logger
from version import VERSION
# from version import VERSION

BASE_CONFIG = {
"url": None,
Expand Down Expand Up @@ -49,7 +49,7 @@ async def connect(self) -> None:
except Exception:
pass
await self.event_ws.send(json.dumps(
translator.translate_event(event.get_event_object(
await translator.translate_event(event.get_event_object(
"meta",
"lifecycle",
"connect"
Expand Down Expand Up @@ -88,7 +88,7 @@ async def push_event(self, event: dict) -> None:
try:
await self.event_ws.send(
json.dumps(
translator.translate_event(
await translator.translate_event(
event
)
)
Expand Down Expand Up @@ -122,4 +122,4 @@ def get_headers(self, role: str) -> dict:
return {
"X-Self-ID": client.user.id,
"X-Client-Role": role
} | ({"Authorization": f'Bearer {self.config["access_token"]}'} if self.config["access_token"] else {})
} | ({"Authorization": f'Bearer {self.config["access_token"]}'} if self.config["access_token"] else {})
4 changes: 2 additions & 2 deletions ws_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def handle_event_route(self, websocket: fastapi.WebSocket) -> None:
return
await websocket.accept()
self.clients_on_event_route.append(websocket)
await websocket.send_json(translator.translate_event(event.get_event_object(
await websocket.send_json(await translator.translate_event(event.get_event_object(
"meta",
"lifecycle",
"connect"
Expand Down Expand Up @@ -86,7 +86,7 @@ async def handle_root_route(self, websocket: fastapi.WebSocket) -> None:
await websocket.send_json(resp_data)

async def push_event(self, _event: dict) -> None:
event = translator.translate_event(_event)
event = await translator.translate_event(_event)
for client in self.clients_on_event_route:
try:
await client.send_json(event)
Expand Down

0 comments on commit c3a780b

Please sign in to comment.