Skip to content

Commit

Permalink
Feat/courses format (#14)
Browse files Browse the repository at this point in the history
* feat(courses): change course to embed

* feat(courses): change vars
  • Loading branch information
velascoandres authored Aug 18, 2024
1 parent 764535e commit 34e15bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
34 changes: 17 additions & 17 deletions src/router/scraping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{extract::State, response::IntoResponse};
use axum::{Json, Router};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use serenity::all::ChannelId;
use serenity::all::{ChannelId, Colour, CreateEmbed};
use tracing::{error, info};

pub type FreeCourses = Vec<FreeCourse>;
Expand Down Expand Up @@ -39,35 +39,35 @@ async fn publish_free_courses(
let course_chunk_size = 5;
let total_courses = body.len();

let formated_messages = body
let embeds = body
.into_iter()
.map(|free_course| {
format!(
"**{}**\nCódigo: `{}`\nTiempo para reclamar: `{}`\n🔗 [enlace]({})",
free_course.title, free_course.code, free_course.countdown, free_course.link,
)
CreateEmbed::new()
.title(format!("**{}**", free_course.title))
.field("**Código**", free_course.code, true)
.field("**Tiempo para reclamar**", free_course.countdown, true)
.url(free_course.link)
.color(Colour::from_rgb(15, 137, 64))
})
.collect::<Vec<String>>();
.collect::<Vec<CreateEmbed>>();

let courses = formated_messages.chunks(course_chunk_size);
let courses_embeds_chunks = embeds.chunks(course_chunk_size);

let mut errors = 0;
let mut current_chunk = 1;

for courses_message in courses {
let msg = courses_message.join("\n\n");

let formated_message = if current_chunk == 1 {
format!("@here\n\n**Cursos gratis de la semana**\n\n{msg}")
for embeds in courses_embeds_chunks.into_iter() {
let text_content = if current_chunk == 1 {
Some("@here\n# Cursos gratis de la semana 🦊🚬\n".to_string())
} else {
format!("_\n\n{msg}")
None
};

let send_response = utils::send_message_to_channel(
let send_response = utils::send_embeds_to_channel(
&ctx.0,
consts::COURSES_CHANNEL_ID,
formated_message,
None,
embeds.to_vec(),
text_content,
)
.await;

Expand Down
31 changes: 29 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::Path;

use serenity::all::{
ChannelId, CreateAttachment, CreateMessage, Http, ResolvedOption, ResolvedValue, StickerId,
User,
ChannelId, CreateAttachment, CreateEmbed, CreateMessage, Http, ResolvedOption, ResolvedValue,
StickerId, User,
};
use tracing::{error, info};

Expand Down Expand Up @@ -44,6 +44,33 @@ pub async fn send_message_to_channel(
Ok(())
}

pub async fn send_embeds_to_channel(
http: &Http,
channel_id: u64,
embeds: Vec<CreateEmbed>,
content: Option<String>,
) -> Result<(), serenity::Error> {
let channel = ChannelId::new(channel_id);

let mut message = CreateMessage::new();

if let Some(txt) = content {
message = message.content(txt);
}

message = message.add_embeds(embeds);

if let Err(err) = channel.send_message(http, message).await {
error!("Error on send message: {err}");

return Err(err);
}

info!("Message was sending to channel");

Ok(())
}

pub async fn send_file_message_to_channel(
http: &Http,
channel_id: u64,
Expand Down

0 comments on commit 34e15bc

Please sign in to comment.