Skip to content

Commit

Permalink
fix(events) Github links (#36)
Browse files Browse the repository at this point in the history
* feat: event

* fix: imports

* Update read_github_links.rs

Allow to show entire files without `#L` in url

* feat: message reference and trim message

---------

Co-authored-by: Apika Luca <[email protected]>
  • Loading branch information
stifskere and Brayan-724 authored Jul 24, 2024
1 parent 65dd1e7 commit cdca1ba
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ once_cell = "1.18.0"

gen_welcome = { version = "0.1.0", path = "crates/gen_welcome" }
urlencoding = "2.1.3"
lazy_static = "1.4.0"

[dependencies.parking_lot]
version = "0.12"
Expand Down
90 changes: 82 additions & 8 deletions src/events/read_github_links.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
use std::collections::HashMap;
use regex::{Captures, Regex};
use reqwest::get;
use serenity::all::{Context, EventHandler, Message};
use serenity::all::{Context, CreateMessage, EventHandler, Message};
use serenity::async_trait;
use std::option::Option;
use lazy_static::lazy_static;
use serenity::constants::MESSAGE_CODE_LIMIT;

pub struct ReadGithubLinkHandler;

lazy_static! {
static ref COMMENT_TEMPLATES: HashMap<&'static str, &'static str> = {
let mut m = HashMap::new();
m.insert("c", "// {}");
m.insert("cpp", "// {}");
m.insert("cs", "// {}");
m.insert("java", "// {}");
m.insert("js", "// {}");
m.insert("go", "// {}");
m.insert("kt", "// {}");
m.insert("swift", "// {}");
m.insert("rs", "// {}");
m.insert("scala", "// {}");
m.insert("py", "# {}");
m.insert("sh", "# {}");
m.insert("pl", "# {}");
m.insert("rb", "# {}");
m.insert("r", "# {}");
m.insert("ps1", "# {}");
m.insert("php", "// {}");
m.insert("sql", "-- {}");
m.insert("html", "<!-- {} -->");
m.insert("xml", "<!-- {} -->");
m.insert("css", "/* {} */");
m.insert("lisp", "; {}");
m.insert("scm", "; {}");
m.insert("hs", "-- {}");
m.insert("m", "% {}");
m.insert("asm", "; {}");
m.insert("pro", "% {}");
m.insert("vim", "\" {}");
m.insert("ini", "; {}");
m.insert("jl", "# {}");
m.insert("erl", "% {}");
m.insert("ex", "# {}");
m.insert("lua", "-- {}");
m.insert("tcl", "# {}");
m.insert("yml", "# {}");
m.insert("md", "[comment]: # ({})");
m.insert("lhs", "-- {}");
m
};
}

pub enum RangeOrIndex {
Language(String),
Index(String, i32),
Expand Down Expand Up @@ -53,6 +100,20 @@ pub fn parse_url(url: &str) -> Option<RangeOrIndex> {
}
}

fn trim_message(lang: String, content: String) -> String {
if content.len() > MESSAGE_CODE_LIMIT {
content[0..MESSAGE_CODE_LIMIT - 200].to_string()
+ &*format!(
"\n{}",
COMMENT_TEMPLATES
.get(&*lang)
.unwrap_or(&"// {}")
.replace("{}", "El mensaje fue cortado por limite de caracteres.")
)
} else {
content
}
}

async fn read_message(link: String) -> Option<String> {
if let Ok(result) = get(&link).await {
Expand All @@ -65,27 +126,31 @@ async fn read_message(link: String) -> Option<String> {
return match parsed {
RangeOrIndex::Language(language)
=> Some(format!(
"Mostrando <{link}>\n```{language}\n{text}\n```"
"Mostrando <{link}>\n```{}\n{}\n```",
language.to_owned(),
trim_message(language, text)
)),
RangeOrIndex::Index(language, index)
=> {
if index < subtext.len() as i32 {
Some(format!(
"Mostrando linea {} de <{link}>\n```{language}\n{}\n```",
"Mostrando linea {} de <{link}>\n```{}\n{}\n```",
index + 1,
subtext[index as usize].to_string())
language.to_owned(),
trim_message(language, subtext[index as usize].to_string()))
)
} else {
None
}
}
RangeOrIndex::Range(language, start, end)
=> {
if start < subtext.len() as i32 && end <= subtext.len() as i32 {
if start < subtext.len() as i32 && end <= subtext.len() as i32 {
Some(format!(
"Mostrando desde la linea {} hasta la linea {end} de <{link}>\n```{language}\n{}\n```",
"Mostrando desde la linea {} hasta la linea {end} de <{link}>\n```{}\n{}\n```",
start + 1,
subtext[start as usize..end as usize].join("\n")
language.to_owned(),
trim_message(language, subtext[start as usize..end as usize].join("\n"))
))
} else {
None
Expand Down Expand Up @@ -135,7 +200,16 @@ impl EventHandler for ReadGithubLinkHandler {
}

if let Some(content) = read_message(link.to_string()).await {
msg.reply(&ctx, content).await.unwrap();
if let Some(reference) = &msg.message_reference {
msg.channel_id.send_message(
&ctx,
CreateMessage::new()
.content(content)
.reference_message(reference.clone())
).await.unwrap();
} else {
msg.reply(&ctx, content).await.unwrap();
}
}

dup.push(link);
Expand Down

0 comments on commit cdca1ba

Please sign in to comment.