Add latency to ping command

This commit is contained in:
Agatha Rose 2020-04-29 05:17:49 +03:00
parent 1021cdef38
commit 5b5720a6b7
1 changed files with 21 additions and 3 deletions

View File

@ -6,7 +6,7 @@ extern crate lazy_static;
use colored::*;
use rand::Rng;
use serenity::{
client::Client,
client::{Client, bridge::gateway::{ShardId, ShardManager}},
framework::standard::{
macros::{check, command, group},
Args, CheckResult, CommandError, CommandOptions, CommandResult, DispatchError, Reason,
@ -17,10 +17,16 @@ use serenity::{
};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::{env, process};
use std::{env, process, sync::Arc};
struct Handler;
struct ShardManagerContainer;
impl TypeMapKey for ShardManagerContainer {
type Value = Arc<Mutex<ShardManager>>;
}
impl EventHandler for Handler {
fn ready(&self, ctx: Context, ready: Ready) {
if let Some(shard) = ready.shard {
@ -127,7 +133,19 @@ fn init(ctx: &mut Context, message: &Message) -> CommandResult {
#[command]
fn ping(ctx: &mut Context, message: &Message) -> CommandResult {
let _ = message.reply(&ctx, "Pong!");
// I have no idea if this works but its 5æm and I need to sleep help
let data = ctx.data.read();
let shards = data.get::<ShardManagerContainer>().unwrap().lock();
let runners = shards.runners.lock();
let runner = match runners.get(&ShardId(ctx.shard_id)) {
Some(v) => v,
None => {
return Err(CommandError("No shard found!".to_string()));
}
};
let _ = message.reply(&ctx, format!("Pong! Latency: {:?}", runner.latency));
Ok(())
}