From 1ad6c62daf4df331d4c7502c0aeae95c1dd53655 Mon Sep 17 00:00:00 2001 From: Emma Lo Date: Fri, 11 Sep 2020 08:28:01 -0500 Subject: [PATCH] added shortener function, if you use my shortener --- .vscode/settings.json | 3 ++ src/commands/general.rs | 71 +++++++++++++++++++++++++++++++++++++++-- src/main.rs | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2f41870 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.inlayHints.enable": true +} \ No newline at end of file diff --git a/src/commands/general.rs b/src/commands/general.rs index f609858..eb0a7eb 100644 --- a/src/commands/general.rs +++ b/src/commands/general.rs @@ -1,4 +1,5 @@ use dotenv::dotenv; +use rand::random; use serde; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -99,12 +100,23 @@ pub struct Translation { character_count: i64, } +#[derive(Debug, Serialize, Deserialize)] +pub struct ShortenerResult { + ts: i64, + data: Data, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Data { + key: String, + value: String, +} + #[derive(Debug, Serialize, Deserialize)] pub struct TranslationElement { translation: String, } - async fn get_data(url: String) -> Result { let client = reqwest::Client::new(); @@ -134,6 +146,12 @@ async fn post_data_with_apikey( Ok(resp) } +fn rand_str(length: u32) -> String { + (0..length) + .map(|_| (0x0061 + (random::() * 26.0) as u8) as char) + .collect() +} + #[command] #[aliases(tr)] async fn translate(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { @@ -178,7 +196,7 @@ async fn translate(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul .json() .await?; - dbg!(&data); + dbg!(&data); let _message = if let Some(message) = data.pointer("/error").and_then(|x| x.as_str()) { return Err(CommandError::from(message)); @@ -210,6 +228,55 @@ async fn translate(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul Ok(()) } +#[command] +#[owners_only] +#[aliases(link, li, redir, r)] +async fn shorten(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let mut key = args.single::()?; + let value: String = args.rest().to_string(); + + if key == "random".to_string() { + key = rand_str(5).to_string(); + } + + let url = "https://r.izu.moe/create?key={KEY}&url={VALUE}&auth={AUTH}"; + + let short_url = url + .replace("{KEY}", &key) + .replace("{VALUE}", &value) + .replace( + "{AUTH}", + &env::var("REDIR_KEY").expect("Expected REDIR_KEY to be set in environment"), + ) + .to_string(); + let data = get_data(short_url.to_string()).await?; + let short_url_des: ShortenerResult = serde_json::from_value(data.clone()).unwrap(); + + let _ = msg + .channel_id + .send_message(&ctx.http, |m| { + m.embed(|e| { + e.color(0x3498db) + .title("Link shortener - Result") + .description(&format!( + "[r.izu.moe/{}](https://r.izu.moe/{}) + ```json +data: {{ + key: \"{}\" + value: \"{}\" +}}```", + short_url_des.data.key, + short_url_des.data.key, + short_url_des.data.key, + short_url_des.data.value + )) + .footer(|f| f.text(format!("Document ID: {}", short_url_des.ts))) + }) + }) + .await; + Ok(()) +} + #[command] #[aliases(w)] async fn weather(ctx: &Context, msg: &Message, args: Args) -> CommandResult { diff --git a/src/main.rs b/src/main.rs index b8eb700..b36c24d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ impl EventHandler for Handler { } #[group] -#[commands(activity, nickname, quit)] +#[commands(activity, nickname, quit, shorten)] #[description = "admin/bot management stuff."] struct Admin;