Skip to content

Commit

Permalink
added shortener function, if you use my shortener
Browse files Browse the repository at this point in the history
  • Loading branch information
espeon committed Sep 11, 2020
1 parent 6ff3553 commit 1ad6c62
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.inlayHints.enable": true
}
71 changes: 69 additions & 2 deletions src/commands/general.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dotenv::dotenv;
use rand::random;
use serde;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -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<Value, CommandError> {
let client = reqwest::Client::new();

Expand Down Expand Up @@ -134,6 +146,12 @@ async fn post_data_with_apikey(
Ok(resp)
}

fn rand_str(length: u32) -> String {
(0..length)
.map(|_| (0x0061 + (random::<f32>() * 26.0) as u8) as char)
.collect()
}

#[command]
#[aliases(tr)]
async fn translate(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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::<String>()?;
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 1ad6c62

Please sign in to comment.