-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seen.rs
104 lines (90 loc) · 2.98 KB
/
seen.rs
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
use std::time::Duration;
use azalea::{
app::{App, Plugin, Update},
ecs::prelude::*,
};
use chrono::{DateTime, Utc};
use handlers::prelude::*;
use serde::Deserialize;
use crate::commands::{prelude::*, Commands};
/// 2B2T Seen Command <https://2b2t.vc>
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SeenCommandPlugin;
impl Command for SeenCommandPlugin {
fn aliases(&self) -> Vec<&'static str> {
vec!["seen"]
}
}
impl Plugin for SeenCommandPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
handle_seen_command_event
.ambiguous_with_all()
.before(handle_discord_whisper_event)
.before(handle_minecraft_whisper_event)
.after(handle_chat_received_event),
);
}
}
pub fn handle_seen_command_event(
mut command_events: EventReader<CommandEvent>,
mut whisper_events: EventWriter<WhisperEvent>,
) {
for event in command_events.read() {
let Commands::Seen(_plugin) = event.command else {
continue;
};
let mut whisper_event = WhisperEvent {
entity: event.entity,
source: event.source,
sender: event.sender,
content: String::new(),
};
let Some(player_name) = event.args.iter().next() else {
whisper_event.content = String::from("[400] Missing player name");
whisper_events.send(whisper_event);
continue;
};
let response = match ureq::get("https://api.2b2t.vc/seen")
.query("playerName", player_name)
.timeout(Duration::from_secs(25))
.call()
{
Ok(response) => response,
Err(error) => {
whisper_event.content = format!("[500] Error: {error}");
whisper_events.send(whisper_event);
error!("{error}");
continue;
}
};
if response.status() == 204 {
whisper_event.content = format!("[204] Player not found: {player_name}");
whisper_events.send(whisper_event);
continue;
}
let Ok(json) = response.into_json::<Json>() else {
whisper_event.content = String::from("[500] Failed to parse JSON");
whisper_events.send(whisper_event);
continue;
};
let (Some(first), Some(last)) = (json.first_seen, json.last_seen) else {
whisper_event.content = String::from("[200] Player has never joined");
whisper_events.send(whisper_event);
continue;
};
whisper_event.content = format!(
"First: {} | Last: {}",
first.format("%Y-%m-%d"),
last.format("%Y-%m-%d %H:%M")
);
whisper_events.send(whisper_event);
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Json {
first_seen: Option<DateTime<Utc>>,
last_seen: Option<DateTime<Utc>>,
}