1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::sync::Arc;
use async_trait::async_trait;
use crate::error::WalleResult;
use crate::prelude::Version;
use crate::structs::{Bot, Selft, Status};
use crate::util::GetSelf;
use crate::EventHandler;
use crate::OneBot;
#[async_trait]
pub trait ActionHandler<E, A, R>: GetStatus + GetVersion + Sync {
type Config;
async fn start<AH, EH>(
&self,
ob: &Arc<OneBot<AH, EH>>,
config: Self::Config,
) -> WalleResult<Vec<tokio::task::JoinHandle<()>>>
where
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static;
async fn call<AH, EH>(&self, action: A, ob: &Arc<OneBot<AH, EH>>) -> WalleResult<R>
where
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static;
async fn before_call_event<AH, EH>(&self, event: E, _ob: &Arc<OneBot<AH, EH>>) -> WalleResult<E>
where
E: Send + 'static,
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static,
{
Ok(event)
}
async fn after_call_event<AH, EH>(&self, _ob: &Arc<OneBot<AH, EH>>) -> WalleResult<()>
where
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static,
{
Ok(())
}
async fn shutdown(&self) {}
}
#[async_trait]
pub trait GetSelfs {
async fn get_selfs(&self) -> Vec<Selft>;
async fn get_impl(&self, selft: &Selft) -> String;
}
impl<T: GetSelfs> GetSelfs for Arc<T> {
fn get_impl<'life0, 'life1, 'async_trait>(
&'life0 self,
selft: &'life1 Selft,
) -> core::pin::Pin<
Box<dyn core::future::Future<Output = String> + core::marker::Send + 'async_trait>,
>
where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
{
self.as_ref().get_impl(selft)
}
fn get_selfs<'life0, 'async_trait>(
&'life0 self,
) -> core::pin::Pin<
Box<dyn core::future::Future<Output = Vec<Selft>> + core::marker::Send + 'async_trait>,
>
where
'life0: 'async_trait,
Self: 'async_trait,
{
self.as_ref().get_selfs()
}
}
#[async_trait]
pub trait GetStatus: GetSelfs {
async fn is_good(&self) -> bool;
async fn get_status(&self) -> Status
where
Self: Sized,
{
Status {
good: self.is_good().await,
bots: self
.get_selfs()
.await
.into_iter()
.map(|selft| Bot {
selft,
online: true,
})
.collect(),
}
}
}
pub trait GetVersion {
fn get_version(&self) -> Version;
}
pub struct JoinedHandler<H0, H1>(pub H0, pub H1);
pub trait AHExt<E, A, R> {
fn join<AH1>(self, action_handler: AH1) -> JoinedHandler<Self, AH1>
where
Self: Sized,
{
JoinedHandler(self, action_handler)
}
}
impl<T: ActionHandler<E, A, R>, E, A, R> AHExt<E, A, R> for T {}
#[async_trait]
impl<H0, H1> GetSelfs for JoinedHandler<H0, H1>
where
H0: GetSelfs + Send + Sync,
H1: GetSelfs + Send + Sync,
{
async fn get_selfs(&self) -> Vec<crate::structs::Selft> {
let mut r = self.0.get_selfs().await;
r.extend(self.1.get_selfs().await.into_iter());
r
}
async fn get_impl(&self, selft: &Selft) -> String {
if self.0.get_selfs().await.contains(selft) {
self.0.get_impl(selft).await
} else {
self.1.get_impl(selft).await
}
}
}
#[async_trait]
impl<H0, H1> GetStatus for JoinedHandler<H0, H1>
where
H0: GetStatus + Send + Sync,
H1: GetStatus + Send + Sync,
{
async fn is_good(&self) -> bool {
self.0.is_good().await && self.1.is_good().await
}
}
impl<H0, H1> GetVersion for JoinedHandler<H0, H1>
where
H0: GetVersion,
{
fn get_version(&self) -> Version {
self.0.get_version()
}
}
#[async_trait]
impl<AH0, AH1, E, A, R> ActionHandler<E, A, R> for JoinedHandler<AH0, AH1>
where
AH0: ActionHandler<E, A, R> + Send + Sync + 'static,
AH0::Config: Send + Sync + 'static,
AH1: ActionHandler<E, A, R> + Send + Sync + 'static,
AH1::Config: Send + Sync + 'static,
A: GetSelf + Send + Sync + 'static,
R: From<crate::resp::RespError>,
{
type Config = (AH0::Config, AH1::Config);
async fn start<AH, EH>(
&self,
ob: &Arc<OneBot<AH, EH>>,
config: Self::Config,
) -> WalleResult<Vec<tokio::task::JoinHandle<()>>>
where
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static,
{
let mut joins = self.0.start(ob, config.0).await?;
joins.extend(self.1.start(ob, config.1).await?.into_iter());
Ok(joins)
}
async fn call<AH, EH>(&self, action: A, ob: &Arc<OneBot<AH, EH>>) -> WalleResult<R>
where
AH: ActionHandler<E, A, R> + Send + Sync + 'static,
EH: EventHandler<E, A, R> + Send + Sync + 'static,
{
if self.0.get_selfs().await.contains(&action.get_self()) {
self.0.call(action, ob).await
} else if self.1.get_selfs().await.contains(&action.get_self()) {
self.1.call(action, ob).await
} else {
Ok(crate::resp::resp_error::who_am_i("").into())
}
}
async fn shutdown(&self) {
self.0.shutdown().await;
self.1.shutdown().await;
}
}