Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: simplify code block embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jan 6, 2023
1 parent fd4bf5a commit 8376eb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/events/message_create.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::utils::autorespond::auto_respond;
use crate::utils::code_embed::utils::handle_code_url;
use crate::utils::code_embed::utils::code_preview;
use crate::utils::media_channel::handle_media_channel;

pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Message) {
Expand All @@ -12,5 +12,5 @@ pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Mes

auto_respond(ctx, new_message).await;

handle_code_url(ctx, new_message).await;
code_preview(ctx, new_message).await;
}
70 changes: 22 additions & 48 deletions src/utils/code_embed/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use chrono::Utc;
use reqwest::Url;
use tracing::{debug, error};
use tracing::{debug, error, trace};

use super::*;
use crate::utils::bot::get_data_lock;
use crate::utils::code_embed::url_parser::{CodePreview, CodeUrlParser, GitHubCodeUrl};

pub async fn handle_code_url(ctx: &serenity::Context, new_message: &serenity::Message) {
pub async fn code_preview(ctx: &serenity::Context, new_message: &serenity::Message) {
let data_lock = get_data_lock(ctx).await;
let configuration = &data_lock.read().await.configuration;

Expand Down Expand Up @@ -55,8 +54,12 @@ pub async fn handle_code_url(ctx: &serenity::Context, new_message: &serenity::Me
};

match code_url.parse().await {
Err(e) => error!("Failed to parse url: {} ({:?})", url, e),
Ok(code_preview) => code_previews.push(code_preview),
Err(e) => trace!("Failed to parse url: {} ({:?})", url, e),
Ok(code_preview) => {
if code_preview.preview.is_some() {
code_previews.push(code_preview)
}
},
}
}

Expand All @@ -66,53 +69,23 @@ pub async fn handle_code_url(ctx: &serenity::Context, new_message: &serenity::Me

if let Err(err) = new_message
.channel_id
.send_message(&ctx.http, |m| {
let mut message = m;
.send_message(&ctx.http, |message| {
let mut message = message.reference_message(new_message);
let icon_url = &new_message.guild(&ctx.cache).unwrap().icon_url();

for code_preview in code_previews {
message = message.add_embed(|e| {
let mut e = e
.title("Source code")
.url(code_preview.code.original_code_url)
.color(configuration.general.embed_color)
.field(
"Raw link",
format!("[Click here]({})", code_preview.code.raw_code_url),
true,
)
.field("Branch/Sha", code_preview.code.branch_or_sha, true);

if let Some(preview) = code_preview.preview {
e = e.field("Preview", preview, false)
}

let guild = &new_message.guild(&ctx.cache).unwrap();
if let Some(url) = &guild.icon_url() {
e = e.footer(|f| {
f.icon_url(url).text(format!(
"{} • {}",
guild.name,
Utc::today().format("%Y/%m/%d")
))
})
message = message.add_embed(|embed| {
if let Some(url) = icon_url {
embed.footer(|f| f.text("ReVanced").icon_url(url))
} else {
embed
}

e.field(
format!("Original message by {}", new_message.author.tag()),
new_message.content.clone(),
false,
)
.color(configuration.general.embed_color)
.description(code_preview.preview.unwrap())
});
}

message.content(
new_message
.mentions
.iter()
.map(|m| format!("<@{}>", m.id))
.collect::<Vec<_>>()
.join(" "),
)
message
})
.await
{
Expand All @@ -121,9 +94,10 @@ pub async fn handle_code_url(ctx: &serenity::Context, new_message: &serenity::Me
new_message.author.tag(),
err
);
return;
}

if let Err(err) = new_message.delete(&ctx.http).await {
error!("Failed to delete the message. Error: {:?}", err);
if let Err(err) = new_message.clone().suppress_embeds(&ctx.http).await {
error!("Failed to remove embeds. Error: {:?}", err);
}
}

0 comments on commit 8376eb9

Please sign in to comment.