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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use crate::{from_i64, stringify_option, to_i64, AdapterError};
#[cfg(feature = "net")]
use serde::{Deserialize, Serialize};
use serenity::{
futures::TryStreamExt,
model::id::{GuildId, MessageId, UserId},
};
use sqlx::{query, query_scalar, Executor, Postgres};
use tokio_stream::{Stream, StreamExt};
#[cfg_attr(feature = "net", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Enforcer {
Community,
Manager(UserId),
}
impl From<Option<u64>> for Enforcer {
fn from(option: Option<u64>) -> Self {
match option {
Some(id) => Enforcer::Manager(id.into()),
None => Enforcer::Community,
}
}
}
fn option_to_enforcer(option: Option<i64>) -> Enforcer {
match option {
Some(int) => Enforcer::Manager(from_i64(int)),
None => Enforcer::Community,
}
}
pub(crate) fn enforcer_to_option(enforcer: Enforcer) -> Option<UserId> {
match enforcer {
Enforcer::Manager(user) => Some(user),
Enforcer::Community => None,
}
}
type Result<R> = std::result::Result<R, AdapterError>;
#[cfg_attr(feature = "net", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct SlapReport {
pub sentence: MessageId,
pub offender: UserId,
pub enforcer: Enforcer,
pub reason: Option<String>,
}
impl SlapReport {
pub async fn get<'a, PgExec: Executor<'a, Database = Postgres>>(
conn: PgExec,
sentence: MessageId,
) -> Result<Option<SlapReport>> {
Ok(query!(
"SELECT offender, enforcer, reason FROM slaps WHERE sentence=$1",
to_i64(sentence)
)
.fetch_optional(conn)
.await?
.map(|record| SlapReport {
sentence,
offender: UserId(from_i64(record.offender)),
enforcer: option_to_enforcer(record.enforcer),
reason: record.reason,
}))
}
}
async fn insert_raw_slap<'a, PgExec: Executor<'a, Database = Postgres>, S: std::fmt::Display>(
conn: PgExec,
sentence: i64,
guild: i64,
offender: i64,
enforcer: Enforcer,
reason: Option<S>,
) -> Result<()> {
sqlx::query(&format!("INSERT INTO slaps(sentence, guild, offender, enforcer, reason) VALUES ({}, {}, {}, {}, {})",sentence, guild, offender, stringify_option(enforcer_to_option(enforcer)), stringify_option(reason))).execute(conn).await?;
Ok(())
}
#[derive(Debug, PartialEq, Eq)]
pub struct MemberSlapRecord(pub GuildId, pub UserId);
impl MemberSlapRecord {
pub async fn new_slap<'a, PgExec: Executor<'a, Database = Postgres> + Copy>(
&self,
conn: PgExec,
sentence: MessageId,
enforcer: Enforcer,
reason: Option<String>,
) -> Result<SlapReport> {
insert_raw_slap(
conn,
to_i64(sentence),
to_i64(self.0),
to_i64(self.1),
enforcer.clone(),
reason.clone(),
)
.await?;
Ok(SlapReport {
sentence,
offender: self.1,
enforcer,
reason,
})
}
pub fn slaps<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
&'a self,
conn: PgExec,
) -> impl Stream<Item = Result<SlapReport>> + 'a {
let offender = to_i64(self.1);
query!(
"SELECT sentence, enforcer, reason FROM slaps WHERE guild=$1 AND offender=$2",
to_i64(self.0),
offender
)
.fetch(conn)
.map_err(|e| AdapterError::from(e))
.map(move |res| {
res.map(|record| SlapReport {
sentence: MessageId(from_i64(record.sentence)),
offender: self.1,
enforcer: match record.enforcer {
Some(user) => Enforcer::Manager(UserId(from_i64(user))),
None => Enforcer::Community,
},
reason: record.reason,
})
})
}
pub async fn len<'a, PgExec: Executor<'a, Database = Postgres>>(
&self,
conn: PgExec,
) -> Result<usize> {
Ok(query_scalar!(
r#"SELECT COUNT(sentence) as "count!" FROM slaps WHERE guild=$1 AND offender=$2"#,
to_i64(self.0),
to_i64(self.1),
)
.fetch_one(conn)
.await? as usize)
}
}
impl From<(GuildId, UserId)> for MemberSlapRecord {
fn from(src: (GuildId, UserId)) -> Self {
MemberSlapRecord(src.0, src.1)
}
}
impl From<(GuildSlapRecord, UserId)> for MemberSlapRecord {
fn from(src: (GuildSlapRecord, UserId)) -> Self {
MemberSlapRecord(src.0 .0, src.1)
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct GuildSlapRecord(pub GuildId);
impl GuildSlapRecord {
pub async fn new_slap<
'a,
PgExec: Executor<'a, Database = Postgres> + Copy,
S: std::fmt::Display,
>(
&self,
conn: PgExec,
sentence: MessageId,
offender: UserId,
enforcer: Enforcer,
reason: Option<S>,
) -> Result<SlapReport> {
let reason = reason.map(|s| s.to_string());
insert_raw_slap(
conn,
to_i64(sentence),
to_i64(self.0),
to_i64(offender),
enforcer.clone(),
reason.clone(),
)
.await?;
Ok(SlapReport {
sentence,
offender,
enforcer,
reason,
})
}
pub async fn len<'a, PgExec: Executor<'a, Database = Postgres>>(
&self,
conn: PgExec,
) -> Result<usize> {
Ok(query_scalar!(
r#"SELECT COUNT(sentence) as "count!" FROM slaps WHERE guild=$1"#,
to_i64(self.0),
)
.fetch_one(conn)
.await? as usize)
}
pub fn slaps<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
&'a self,
conn: PgExec,
) -> impl Stream<Item = Result<SlapReport>> + 'a {
query!(
"SELECT sentence, offender, enforcer, reason FROM slaps WHERE guild=$1",
to_i64(self.0),
)
.fetch(conn)
.map_err(|e| AdapterError::from(e))
.map(move |res| {
res.map(|record| SlapReport {
sentence: MessageId(from_i64(record.sentence)),
offender: UserId(from_i64(record.offender)),
enforcer: match record.enforcer {
Some(user) => Enforcer::Manager(UserId(from_i64(user))),
None => Enforcer::Community,
},
reason: record.reason,
})
})
}
pub fn offenders<'a, PgExec: Executor<'a, Database = Postgres> + 'a>(
&'a self,
conn: PgExec,
) -> impl Stream<Item = Result<MemberSlapRecord>> + 'a {
query!(
"SELECT DISTINCT offender FROM slaps WHERE guild=$1",
to_i64(self.0)
)
.fetch(conn)
.map_err(|e| AdapterError::from(e))
.map(move |res| {
res.map(|record| MemberSlapRecord(self.0, UserId(from_i64(record.offender))))
})
}
pub async fn offender_count<'a, PgExec: Executor<'a, Database = Postgres>>(
&self,
conn: PgExec,
) -> Result<usize> {
Ok(query_scalar!(
r#"SELECT COUNT(DISTINCT offender) as "count!" FROM slaps WHERE guild=$1"#,
to_i64(self.0),
)
.fetch_one(conn)
.await? as usize)
}
}
impl From<GuildId> for GuildSlapRecord {
fn from(src: GuildId) -> Self {
GuildSlapRecord(src)
}
}