Skip to content

Commit

Permalink
Merge pull request #45 from stifskere/main
Browse files Browse the repository at this point in the history
feat: add delete button to github links responses and fix comments
  • Loading branch information
SergioRibera authored Nov 18, 2024
2 parents eea3570 + fad67ef commit da6946f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 48 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

8 changes: 8 additions & 0 deletions src/bot/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
}
}
_ => {}
}
Expand Down
138 changes: 91 additions & 47 deletions src/bot/events/read_github_links.rs
Original file line number Diff line number Diff line change
@@ -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<HashMap<&'static str, &'static str>> = 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),
Expand Down Expand Up @@ -146,7 +149,7 @@ async fn read_message(link: String) -> Option<String> {
}

pub async fn message(ctx: &Context, msg: &Message) -> bool {
if msg.author.bot || msg.content.starts_with("noembed") {
if msg.author.bot {
return false;
}

Expand All @@ -165,29 +168,70 @@ 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/"));

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
}

0 comments on commit da6946f

Please sign in to comment.