Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
feat: request events
Browse files Browse the repository at this point in the history
See #17
  • Loading branch information
BlueGlassBlock committed May 2, 2023
1 parent 52302d7 commit 02107ff
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/+request-events.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
支持接收好友申请、加群申请与被邀请入群事件。
30 changes: 29 additions & 1 deletion python/ichika/core/events/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Literal, TypedDict
from typing import Literal, Optional, TypedDict

from graia.amnesia.message import MessageChain

Expand Down Expand Up @@ -109,5 +109,33 @@ class GroupInfoUpdate:
operator: Member
info: __GroupInfo

@dataclass
class NewFriendRequest:
seq: int
uin: int
nickname: str
message: str

@dataclass
class JoinGroupRequest:
seq: int
time: datetime
group_uin: int
group_name: str
request_uin: int
request_nickname: str
suspicious: bool
invitor_uin: Optional[int]
invitor_nickname: Optional[str]

@dataclass
class JoinGroupInvitation:
seq: int
time: datetime
group_uin: int
group_name: str
invitor_uin: int
invitor_nickname: str

@internal_repr
class UnknownEvent: ...
46 changes: 46 additions & 0 deletions src/events/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ use super::{
GroupMute,
GroupNudge,
GroupRecallMessage,
JoinGroupInvitation,
JoinGroupRequest,
LoginEvent,
MemberLeaveGroup,
MemberMute,
MemberPermissionChange,
NewFriend,
NewFriendRequest,
NewMember,
TempMessage,
UnknownEvent,
Expand Down Expand Up @@ -50,6 +53,9 @@ pub async fn convert(event: QEvent) -> PyRet {
QEvent::GroupMute(event) => handle_mute(event).await,
QEvent::MemberPermissionChange(event) => handle_permission_change(event).await,
QEvent::GroupNameUpdate(event) => handle_group_info_update(event).await,
QEvent::GroupRequest(event) => handle_group_request(event),
QEvent::SelfInvited(event) => handle_group_invitation(event),
QEvent::NewFriendRequest(event) => Ok(handle_friend_request(event)),
unknown => Ok(UnknownEvent { inner: unknown }.obj()),
}
}
Expand Down Expand Up @@ -442,3 +448,43 @@ async fn handle_group_info_update(event: rce::GroupNameUpdateEvent) -> PyRet {
}
.obj())
}

fn handle_group_request(event: rce::JoinGroupRequestEvent) -> PyRet {
let event = event.inner;
Ok(JoinGroupRequest {
seq: event.msg_seq,
time: py_try(|py| datetime_from_ts(py, event.msg_time).map(|v| v.into_py(py)))?,
group_uin: event.group_code,
group_name: event.group_name,
request_uin: event.req_uin,
request_nickname: event.req_nick,
suspicious: event.suspicious,
invitor_uin: event.invitor_uin,
invitor_nickname: event.invitor_nick,
}
.obj())
}

fn handle_group_invitation(event: rce::SelfInvitedEvent) -> PyRet {
let event = event.inner;
Ok(JoinGroupInvitation {
seq: event.msg_seq,
time: py_try(|py| datetime_from_ts(py, event.msg_time).map(|v| v.into_py(py)))?,
group_uin: event.group_code,
group_name: event.group_name,
invitor_uin: event.invitor_uin,
invitor_nickname: event.invitor_nick,
}
.obj())
}

fn handle_friend_request(event: rce::NewFriendRequestEvent) -> PyObject {
let event = event.inner;
NewFriendRequest {
seq: event.msg_seq,
uin: event.req_uin,
nickname: event.req_nick,
message: event.message,
}
.obj()
}
34 changes: 34 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,40 @@ pub struct GroupInfoUpdate {
info: Py<PyDict>, // GroupInfo
}

#[pyclass(get_all)]
#[derive(PyRepr, Clone)]
pub struct NewFriendRequest {
seq: i64,
uin: i64,
nickname: String,
message: String,
}

#[pyclass(get_all)]
#[derive(PyRepr, Clone)]
pub struct JoinGroupRequest {
seq: i64,
time: PyObject,
group_uin: i64,
group_name: String,
request_uin: i64,
request_nickname: String,
suspicious: bool,
invitor_uin: Option<i64>,
invitor_nickname: Option<String>,
}

#[pyclass(get_all)]
#[derive(PyRepr, Clone)]
pub struct JoinGroupInvitation {
seq: i64,
time: PyObject,
group_uin: i64,
group_name: String,
invitor_uin: i64,
invitor_nickname: String,
}

#[pyclass]
#[derive(PyRepr, Clone)]
pub struct UnknownEvent {
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn core(py: Python, m: &PyModule) -> PyResult<()> {
fn register_event_module(py: Python, parent: &PyModule) -> PyResult<()> {
let m = PyModule::new(py, "ichika.core.events")?;
add_batch!(@cls m,
crate::events::GroupMessage,
crate::events::GroupMessage,
crate::events::GroupRecallMessage,
crate::events::TempMessage,
Expand All @@ -75,7 +76,11 @@ fn register_event_module(py: Python, parent: &PyModule) -> PyResult<()> {
crate::events::FriendDeleted,
crate::events::GroupMute,
crate::events::MemberMute,
crate::events::MemberPermissionChange,
crate::events::GroupInfoUpdate,
crate::events::NewFriendRequest,
crate::events::JoinGroupRequest,
crate::events::JoinGroupInvitation,
crate::events::UnknownEvent
);
parent.add_submodule(m)?;
Expand Down

0 comments on commit 02107ff

Please sign in to comment.