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

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LaoLittle committed Dec 12, 2022
1 parent 4d1400b commit 9fc8dd3
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 54 deletions.
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atri_bot"
version = "0.7.1"
version = "0.7.2"
edition = "2021"
authors = ["LaoLittle"]
description = "A simple bot"
Expand Down Expand Up @@ -52,6 +52,7 @@ features = [
]

[dependencies.ricq]
git = "https://github.com/AtriKawaii/ricq.git"
version = "0.1.19"

#[dependencies.ricq-guild]
Expand Down
62 changes: 49 additions & 13 deletions src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use tracing::{error, info, warn};
use crate::contact::friend::Friend;
use crate::contact::member::{AnonymousMember, NamedMember};
use crate::event::{
ClientLoginEvent, Event, FriendMessageEvent, GroupMessageEvent, NewFriendEvent,
ClientLoginEvent, DeleteFriendEvent, Event, FriendMessageEvent, FriendPokeEvent,
GroupMessageEvent, GroupPokeEvent, NewFriendEvent,
};
use crate::global_listener_worker;
use crate::{global_listener_runtime, global_status, Client};
Expand Down Expand Up @@ -80,7 +81,7 @@ impl ricq::handler::Handler for GlobalEventBroadcastHandler {

if client.id() == e.inner.from_uin {
info!(
"{client} >> 群#{}({}): {}",
"{client} >> 群[{}({})]: {}",
group_name(),
group_id,
message(),
Expand Down Expand Up @@ -126,7 +127,7 @@ impl ricq::handler::Handler for GlobalEventBroadcastHandler {
};

info!(
"{}({}) >> 群#{}({}) >> {client}: {}",
"{}({}) >> 群[{}({})] >> {client}: {}",
nick,
sender,
group_name(),
Expand All @@ -147,12 +148,7 @@ impl ricq::handler::Handler for GlobalEventBroadcastHandler {
return;
};

info!(
"好友#{}({}) >> {client}: {}",
friend.nickname(),
friend.id(),
e.inner.elements,
);
info!("{friend} >> {client}: {}", e.inner.elements,);

let base = FriendMessageEvent::from(friend, e);

Expand All @@ -164,14 +160,54 @@ impl ricq::handler::Handler for GlobalEventBroadcastHandler {
let f = Friend::from(client.clone(), e.inner);
client.cache_friend(f.clone());

info!("{f}添加");

Event::NewFriend(NewFriendEvent::from(f))
}
QEvent::DeleteFriend(e) => {
client = get_client!(e.client);

client.remove_friend_cache(e.inner.uin);
let id = e.inner.uin;
let Some(f) = client.remove_friend_cache(id) else {
return;
};

Event::DeleteFriend(DeleteFriendEvent::from(f))
}
QEvent::FriendPoke(e) => {
client = get_client!(e.client);
let friend_id = e.inner.sender;

let Some(f) = client.find_friend(friend_id) else {
error!("寻找好友{friend_id}失败");
return;
};

Event::FriendPoke(FriendPokeEvent::from(f))
}
QEvent::GroupPoke(e) => {
client = get_client!(e.client);
let group_id = e.inner.group_code;
let Some(group) = client.find_or_refresh_group(group_id).await else {
cannot_find_group(group_id);
error_more_info(&e);

return;
};

let sender = e.inner.sender;
let Some(sender) = group.find_member(sender).await else {
error!("无法找到群成员{sender}, Raw event: {:?}", e);
return;
};

Event::Unknown(QEvent::DeleteFriend(e).into())
let target = e.inner.sender;
let Some(target) = group.find_member(target).await else {
error!("无法找到群成员{target}, Raw event: {:?}", e);
return;
};

Event::GroupPoke(GroupPokeEvent::from(group, sender, target))
}
QEvent::GroupDisband(e) => {
client = get_client!(e.client);
Expand Down Expand Up @@ -219,8 +255,8 @@ impl ricq::handler::Handler for GlobalEventBroadcastHandler {
if member == client.id() {
client.remove_group_cache(group_id);
} else {
let Some(group) = client.find_or_refresh_group(group_id).await else {
return;
let Some(group) = client.find_group(group_id) else {
return; // already removed?
};

group.remove_member_cache(member);
Expand Down
54 changes: 27 additions & 27 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,33 +269,6 @@ impl Client {
None
}

pub(crate) async fn find_or_refresh_group(&self, id: i64) -> Option<Group> {
if let Some(g) = self.find_group(id) {
return Some(g);
}

self.refresh_group(id)
.await
.map_err(|e| {
error!("获取群成员({})时发生错误: {}", id, e);

e
})
.unwrap_or(None)
}

pub async fn find_or_refresh_friend_list(&self, id: i64) -> Option<Friend> {
if let Some(f) = self.find_friend(id) {
return Some(f);
}

if let Err(e) = self.refresh_friend_list().await {
error!("获取好友({})失败: {}", id, e);
}

self.find_friend(id)
}

pub fn groups(&self) -> Vec<Group> {
self.group_caches().iter().map(|g| g.clone()).collect()
}
Expand Down Expand Up @@ -340,6 +313,33 @@ impl Client {
self.group_caches().remove(&group_id).map(|(_, g)| g)
}

pub(crate) async fn find_or_refresh_group(&self, id: i64) -> Option<Group> {
if let Some(g) = self.find_group(id) {
return Some(g);
}

self.refresh_group(id)
.await
.map_err(|e| {
error!("获取群成员({})时发生错误: {}", id, e);

e
})
.unwrap_or(None)
}

pub(crate) async fn find_or_refresh_friend_list(&self, id: i64) -> Option<Friend> {
if let Some(f) = self.find_friend(id) {
return Some(f);
}

if let Err(e) = self.refresh_friend_list().await {
error!("获取好友({})失败: {}", id, e);
}

self.find_friend(id)
}

#[inline]
pub(crate) fn request_client(&self) -> &RQClient {
&self.0.client
Expand Down
Loading

0 comments on commit 9fc8dd3

Please sign in to comment.