diff --git a/Cargo.lock b/Cargo.lock index 25144ac..6bf61b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,7 +371,6 @@ dependencies = [ "chrono", "color-eyre", "gen_welcome", - "lazy_static", "once_cell", "parking_lot", "poise", diff --git a/src/bot/events.rs b/src/bot/events.rs index fbc6ab9..15d93f6 100644 --- a/src/bot/events.rs +++ b/src/bot/events.rs @@ -33,6 +33,14 @@ pub async fn handle( FullEvent::GuildMemberAddition { new_member } => { join::guild_member_addition(ctx, &GuildId::new(data.secrets.guild_id), new_member) .await; + }, + FullEvent::InteractionCreate { interaction } => { + // for buttons + if let Some (interaction) = interaction.as_message_component() { + if read_github_links::handle_delete_embed(ctx, interaction).await { + + } + } } _ => {} } diff --git a/src/bot/events/read_github_links.rs b/src/bot/events/read_github_links.rs index 1f80870..089c84a 100644 --- a/src/bot/events/read_github_links.rs +++ b/src/bot/events/read_github_links.rs @@ -1,48 +1,51 @@ -use poise::serenity_prelude::{Context, CreateMessage, Message, MESSAGE_CODE_LIMIT}; +use poise::serenity_prelude::{ButtonStyle, ComponentInteraction, Context, CreateButton, CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, Message, MESSAGE_CODE_LIMIT}; use regex::{Captures, Regex}; use reqwest::get; use std::collections::{HashMap, HashSet}; use std::option::Option; - -static COMMENT_TEMPLATES: HashMap<&'static str, &'static str> = HashMap::from([ - ("c", "// {}"), - ("cpp", "// {}"), - ("cs", "// {}"), - ("java", "// {}"), - ("js", "// {}"), - ("go", "// {}"), - ("kt", "// {}"), - ("swift", "// {}"), - ("rs", "// {}"), - ("scala", "// {}"), - ("py", "# {}"), - ("sh", "# {}"), - ("pl", "# {}"), - ("rb", "# {}"), - ("r", "# {}"), - ("ps1", "# {}"), - ("php", "// {}"), - ("sql", "-- {}"), - ("html", ""), - ("xml", ""), - ("css", "/* {} */"), - ("lisp", "; {}"), - ("scm", "; {}"), - ("hs", "-- {}"), - ("m", "% {}"), - ("asm", "; {}"), - ("pro", "% {}"), - ("vim", "\" {}"), - ("ini", "; {}"), - ("jl", "# {}"), - ("erl", "% {}"), - ("ex", "# {}"), - ("lua", "-- {}"), - ("tcl", "# {}"), - ("yml", "# {}"), - ("md", "[comment]: # ({})"), - ("lhs", "-- {}"), -]); +use std::sync::LazyLock; + +static COMMENT_TEMPLATES: LazyLock> = LazyLock::new(|| { + HashMap::from([ + ("c", "// {}"), + ("cpp", "// {}"), + ("cs", "// {}"), + ("java", "// {}"), + ("js", "// {}"), + ("go", "// {}"), + ("kt", "// {}"), + ("swift", "// {}"), + ("rs", "// {}"), + ("scala", "// {}"), + ("py", "# {}"), + ("sh", "# {}"), + ("pl", "# {}"), + ("rb", "# {}"), + ("r", "# {}"), + ("ps1", "# {}"), + ("php", "// {}"), + ("sql", "-- {}"), + ("html", ""), + ("xml", ""), + ("css", "/* {} */"), + ("lisp", "; {}"), + ("scm", "; {}"), + ("hs", "-- {}"), + ("m", "% {}"), + ("asm", "; {}"), + ("pro", "% {}"), + ("vim", "\" {}"), + ("ini", "; {}"), + ("jl", "# {}"), + ("erl", "% {}"), + ("ex", "# {}"), + ("lua", "-- {}"), + ("tcl", "# {}"), + ("yml", "# {}"), + ("md", "[comment]: # ({})"), + ("lhs", "-- {}"), + ]) +}); pub enum RangeOrIndex { Language(String), @@ -146,7 +149,7 @@ async fn read_message(link: String) -> Option { } pub async fn message(ctx: &Context, msg: &Message) -> bool { - if msg.author.bot || msg.content.starts_with("noembed") { + if msg.author.bot { return false; } @@ -165,7 +168,6 @@ pub async fn message(ctx: &Context, msg: &Message) -> bool { let without_hidden = hidden_link_regex.replace_all(&replaced, ""); 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('!') && s.starts_with("https://raw.githubusercontent.com/")); @@ -173,21 +175,63 @@ pub async fn message(ctx: &Context, msg: &Message) -> bool { let dup = HashSet::<&str>::from_iter(links); for link in dup { if let Some(content) = read_message(link.to_string()).await { + let message = CreateMessage::new() + .content(content) + .button( + CreateButton::new("delete_github_embed") + .label("Borrar") + .style(ButtonStyle::Danger) + ); + if let Some(reference) = &msg.message_reference { msg.channel_id .send_message( &ctx, - CreateMessage::new() - .content(content) - .reference_message(reference.clone()), + message + .reference_message(reference.clone()) ) .await .unwrap(); } else { - msg.reply(&ctx, content).await.unwrap(); + msg.channel_id + .send_message( + &ctx, + message + .reference_message(msg) + ) + .await + .unwrap(); } } } true } + +pub async fn handle_delete_embed(ctx: &Context, interaction: &ComponentInteraction) -> bool { + if interaction.data.custom_id != "delete_github_embed" { + return false; + } + + if interaction.message.author.id != interaction.user.id { + interaction + .create_response( + ctx, + CreateInteractionResponse::Message( + CreateInteractionResponseMessage::new() + .ephemeral(true) + .content("El bloque de codigo no era para ti.") + ) + ) + .await + .ok(); + + return true; + } + + interaction.message.delete(&ctx) + .await + .ok(); + + true +}