uptime and fix

This commit is contained in:
Oupson 2021-05-08 00:56:34 +02:00
parent e6f60efbe7
commit 362dfeeb9d
5 changed files with 60 additions and 3 deletions

View File

@ -22,7 +22,8 @@ use serenity::{
longcode, longcode,
older, older,
ping, ping,
random_mute random_mute,
uptime
)] )]
pub struct General; pub struct General;
@ -318,6 +319,40 @@ async fn random_mute(ctx: &Context, msg: &Message) -> CommandResult {
Ok(()) Ok(())
} }
#[command]
#[description("Get the uptime")]
async fn uptime(ctx: &Context, msg: &Message) -> CommandResult {
let now = std::time::Instant::now();
let data = ctx.data.read().await;
let start = data
.get::<crate::data::Uptime>()
.expect("Failed to get uptime");
let elapsed = now.duration_since(*start);
let seconds = elapsed.as_secs();
let hours = seconds / (60 * 60);
let min = (seconds - (hours * 60 * 60)) / 60;
let seconds = seconds - (hours * 3600) - (min * 60);
msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.field(
"Uptime",
if hours > 0 {
format!("{}h {:02}min {:02}s", hours, min, seconds)
} else {
format!("{}min {:02}s", min, seconds)
},
false,
).colour((247, 76, 0))
})
}).await?;
Ok(())
}
// TODO JSON FILE // TODO JSON FILE
enum Image { enum Image {
HackerMan(), HackerMan(),

View File

@ -229,6 +229,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
#[description("Play a music (require an url)")] #[description("Play a music (require an url)")]
#[aliases("p")]
async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let guild_id = match ctx.cache.guild_channel(msg.channel_id).await { let guild_id = match ctx.cache.guild_channel(msg.channel_id).await {
Some(channel) => channel.guild_id, Some(channel) => channel.guild_id,
@ -283,6 +284,17 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
Ok(()) => { Ok(()) => {
let source = if url.starts_with("http") { let source = if url.starts_with("http") {
if url.contains("youtube.com") && url.contains("list=") { if url.contains("youtube.com") && url.contains("list=") {
msg.channel_id
.send_message(&ctx.http, |m| {
embed_response(
m,
"Error",
"This is disabled for the moment",
Some(&member),
)
})
.await?;
return Ok(());
msg.channel_id msg.channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
embed_response( embed_response(

View File

@ -40,7 +40,7 @@ pub(crate) fn embed_song<'a, 'b>(
let hours = seconds / (60 * 60); let hours = seconds / (60 * 60);
let min = (seconds - (hours * 60 * 60)) / 60; let min = (seconds - (hours * 60 * 60)) / 60;
let seconds = seconds - (min * 60); let seconds = seconds - (hours * 3600) - (min * 60);
e.field( e.field(
"Duration", "Duration",

View File

@ -18,3 +18,9 @@ pub(crate) struct BulletsContainer;
impl TypeMapKey for BulletsContainer { impl TypeMapKey for BulletsContainer {
type Value = HashMap<u64, (u8, u8)>; type Value = HashMap<u64, (u8, u8)>;
} }
pub(crate) struct Uptime;
impl TypeMapKey for Uptime {
type Value = std::time::Instant;
}

View File

@ -3,7 +3,7 @@ use crate::{
admin::ADMIN_GROUP, general::GENERAL_GROUP, owner::OWNER_GROUP, roulette::ROULETTE_GROUP, admin::ADMIN_GROUP, general::GENERAL_GROUP, owner::OWNER_GROUP, roulette::ROULETTE_GROUP,
settings::SETTINGS_GROUP, settings::SETTINGS_GROUP,
}, },
data::{BulletsContainer, GuildOptions, GuildOptionsKey, ShardManagerContainer}, data::{BulletsContainer, GuildOptions, GuildOptionsKey, ShardManagerContainer, Uptime},
}; };
use async_trait::async_trait; use async_trait::async_trait;
use commands::interaction; use commands::interaction;
@ -27,6 +27,7 @@ use std::{
path::Path, path::Path,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
time::Instant
}; };
use tokio::{fs::File, io::AsyncWriteExt}; use tokio::{fs::File, io::AsyncWriteExt};
@ -52,6 +53,8 @@ pub(crate) static mut INVITE_URL: Option<String> = None;
// TODO CLAP FOR CLI // TODO CLAP FOR CLI
#[tokio::main] #[tokio::main]
async fn main() -> IoResult<()> { async fn main() -> IoResult<()> {
let now = Instant::now();
log4rs::init_file("log4rs.yaml", Default::default()).unwrap(); log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
let conf: config::Conf = toml::from_str(&fs::read_to_string("Conf.toml")?)?; let conf: config::Conf = toml::from_str(&fs::read_to_string("Conf.toml")?)?;
debug!("conf : {:?}", conf); debug!("conf : {:?}", conf);
@ -154,6 +157,7 @@ async fn main() -> IoResult<()> {
{ {
data.insert::<VoiceManager>(std::sync::Arc::clone(&client.voice_manager.unwrap())); data.insert::<VoiceManager>(std::sync::Arc::clone(&client.voice_manager.unwrap()));
}*/ }*/
data.insert::<Uptime>(now);
data.insert::<GuildOptionsKey>({ data.insert::<GuildOptionsKey>({
let options = GuildOptions::load_from_dir("./data/guilds_options").unwrap_or_default(); let options = GuildOptions::load_from_dir("./data/guilds_options").unwrap_or_default();