-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.rs
More file actions
93 lines (76 loc) · 2.55 KB
/
commands.rs
File metadata and controls
93 lines (76 loc) · 2.55 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
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
use poise::command;
use poise::serenity_prelude::{AttachmentType, Channel, Timestamp};
use tracing::{error, info};
use crate::log::ChangeResolution;
use crate::plotting::create_log_graph;
use crate::time_period::TimePeriod;
use crate::{Context, Error};
/// Test connection speed.
#[command(slash_command)]
pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
let before_timestamp = Timestamp::now().timestamp_millis();
let reply = ctx.say("Measuring latency!").await?;
let after_timestamp = Timestamp::now().timestamp_millis();
let latency = ctx.ping().await.as_millis();
reply
.edit(ctx, |reply| {
reply.content(format!(
"Pong!\nDiscord Latency: {}ms\nBot Latency: {}ms",
latency,
after_timestamp - before_timestamp
))
})
.await?;
Ok(())
}
/// Fetch the logs for the current channel.
#[command(slash_command, required_permissions="MANAGE_CHANNELS")]
pub async fn logs(
ctx: Context<'_>,
#[description="How far back to search."]
time_period: TimePeriod,
#[description="The channel to fetch the logs for, defaults to the current channel."]
channel: Option<Channel>,
) -> Result<(), Error> {
let channel_id = channel.map_or_else(|| ctx.channel_id(), |channel| channel.id());
let now_timestamp = Timestamp::now();
let search_start_timestamp = time_period.relative_timestamp_from(now_timestamp.timestamp());
let logs = ctx
.data()
.fetch_logs_between(channel_id.0 as i64, search_start_timestamp, now_timestamp.timestamp())
.await?;
// Combign logs together to make displaying on graph easier
// Todo: This has not been fully tested yet
let logs = match time_period {
TimePeriod::Hour => logs,
TimePeriod::HalfDay => logs.change_resolution(360),
TimePeriod::Day => logs.change_resolution(720),
TimePeriod::Week => logs.change_resolution(2520),
};
if logs.is_empty() {
ctx.say("No logs found for this period").await?;
return Ok(());
}
let channel_name = channel_id
.name(ctx.cache())
.await
.unwrap_or_else(|| "Current Channel".to_string());
let graph = match create_log_graph(logs, &channel_name, search_start_timestamp, now_timestamp.timestamp(), time_period) {
Ok(graph) => graph,
Err(err) => {
error!("Failed to generate log graph: {}", err);
return Err(err)
}
};
ctx.send(|reply| {
reply.attachment(AttachmentType::Bytes {
data: graph.into(),
filename: "graph.png".to_string(),
})
})
.await?;
// Time it took for command to be run.
let finished_timestamp = Timestamp::now().timestamp_millis() - now_timestamp.timestamp_millis();
info!("Command took {}ms", finished_timestamp);
Ok(())
}