-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.rs
More file actions
64 lines (50 loc) · 1.72 KB
/
verify.rs
File metadata and controls
64 lines (50 loc) · 1.72 KB
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
use anyhow::{Result, anyhow};
use poise::serenity_prelude::EditRole;
use crate::{Context, Message, database, util};
/// Verify a candidate and assign role upon success.
#[tracing::instrument]
#[poise::command(slash_command, prefix_command, ephemeral)]
pub async fn verify(ctx: Context<'_>, id: String) -> Result<()> {
let pool = &ctx.data().pool;
let config = &ctx.data().config;
if !util::is_valid_id(&id) {
ctx.reply(Message::InvalidId).await?;
return Ok(());
}
let Some(candidate) = database::candidate::get(&id, pool).await? else {
ctx.reply(Message::NotRegistered).await?;
return Ok(());
};
if let Some(verification_time) = candidate.verification_time {
ctx.reply(Message::Verified(Some(verification_time)))
.await?;
return Ok(());
}
tracing::info!(author = ?ctx.author_member().await);
let Some(member) = ctx.author_member().await else {
return Err(anyhow!("Message contain no author"));
};
let possible_names = [
member.nick.as_ref(),
member.user.global_name.as_ref(),
Some(&member.user.name),
];
let name = possible_names.iter().flatten().next().unwrap();
if !name.contains(&id) {
ctx.reply(Message::InvalidName).await?;
return Ok(());
}
database::candidate::verify(&id, &ctx.data().pool).await?;
let Some(guild) = ctx.guild_id() else {
return Err(anyhow!("Must be in a guild"));
};
let role = guild
.create_role(
ctx.http(),
EditRole::new().name(config.candidate_role.clone()),
)
.await?;
member.add_role(ctx.http(), role.id).await?;
ctx.reply(Message::Verified(None)).await?;
Ok(())
}