Skip to content

Commit

Permalink
Merge pull request #43 from romancitodev/main
Browse files Browse the repository at this point in the history
feat: ✨ add `!` prefix to avoid rendering github links.
  • Loading branch information
SergioRibera authored Nov 18, 2024
2 parents b017adf + 8d285e0 commit 1281066
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/bot/events/read_github_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use lazy_static::lazy_static;
use poise::serenity_prelude::{Context, CreateMessage, Message, MESSAGE_CODE_LIMIT};
use regex::{Captures, Regex};
use reqwest::get;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::option::Option;

lazy_static! {
Expand Down Expand Up @@ -155,32 +155,28 @@ pub async fn message(ctx: &Context, msg: &Message) -> bool {
return false;
}

let repo_regex = Regex::new(r"(https://github\.com/(?:[^/]+/){2})blob/(.*)").unwrap();
let repo_regex = Regex::new("(https://github\\.com/(?:[^/]+/){2})blob/(.*)").unwrap();
let hidden_link_regex = Regex::new(r"[<>]").unwrap();
let split_message_regex = Regex::new(r"[\n ]").unwrap();

let replaced = if repo_regex.is_match(&msg.content) {
repo_regex.replace_all(&msg.content, |captures: &Captures| {
captures[1].to_string() + &captures[2]
})
} else {
return false;
}.replace("https://github.com/", "https://raw.githubusercontent.com/");
}
.replace("https://github.com/", "https://raw.githubusercontent.com/");

let without_hidden = hidden_link_regex.replace_all(&replaced, "");

let without_spaces = split_message_regex.split(&without_hidden);
let without_spaces = without_hidden.split('\n');
// let without_spaces = split_message_regex.split(&without_hidden);


let links = without_spaces
.filter(|s| s.starts_with("https://raw.githubusercontent.com/"));

let mut dup: Vec<&str> = Vec::new();
for link in links {
if dup.contains(&link) {
continue;
}
.filter(|s| !s.starts_with('!') && s.starts_with("https://raw.githubusercontent.com/"));

let dup = HashSet::<&str>::from_iter(links);
for link in dup {
if let Some(content) = read_message(link.to_string()).await {
if let Some(reference) = &msg.message_reference {
msg.channel_id
Expand All @@ -196,8 +192,6 @@ pub async fn message(ctx: &Context, msg: &Message) -> bool {
msg.reply(&ctx, content).await.unwrap();
}
}

dup.push(link);
}

true
Expand Down

0 comments on commit 1281066

Please sign in to comment.