Skip to content

Commit

Permalink
Feat/welcome banner more info (#17)
Browse files Browse the repository at this point in the history
* feat(welcome-banner): refactor welcome event

* feat(welcome-banner): send dm with information

* feat(welcome-banner): added information message

* feat(welcome-banner): added information
  • Loading branch information
velascoandres authored Sep 29, 2024
1 parent 34e15bc commit e0e2f3f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 51 deletions.
54 changes: 3 additions & 51 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::path::Path;

use crate::{commands, consts, events, utils};
use chrono::Utc;
use crate::{commands, consts, events, utils, welcome};
use serenity::all::{
CommandInteraction, CreateAllowedMentions, CreateInteractionResponseFollowup, Member, Message,
};
Expand Down Expand Up @@ -118,55 +115,10 @@ impl EventHandler for Handler {
}

async fn guild_member_addition(&self, ctx: Context, member: Member) {
let response = reqwest::get(member.face()).await.unwrap();
let avatar = response.bytes().await.unwrap();
let guild_id = GuildId::new(self.guild_id);

let position_number = guild_id
.to_guild_cached(&ctx)
.map(|g| g.member_count as usize)
.unwrap_or(1);

let output_path = format!("/tmp/{}_welcome.png", member.user.name);

let dt = Utc::now();
let timestamp: String = dt.timestamp().to_string();

let digits: Vec<_> = timestamp
.chars()
.map(|d| d.to_digit(10).unwrap_or(5))
.collect();

let random_number = *digits.last().unwrap_or(&5);

let banner = if random_number > 3 {
"./assets/banner.png"
} else {
"./assets/pride_banner.png"
};

if let Err(err) = gen_image::generate(
&avatar,
banner,
member.distinct(),
position_number,
&output_path,
include_bytes!("../assets/fonts/Roboto-Bold.ttf"),
include_bytes!("../assets/fonts/Roboto-Regular.ttf"),
) {
log_error!("{err:?}");
}

if let Err(err) = utils::send_file_message_to_channel(
&ctx.http,
consts::WELCOME_CHANNEL_ID,
&format!("Bienvenido a este humilde servidor: <@{}>", member.user.id),
Path::new(&output_path),
)
.await
{
log_error!("{err:?}");
}
welcome::banner::send_welcome_banner(&guild_id, &ctx, &member).await;
welcome::info::send_dm_welcome_information(&ctx, &member.user).await;
}

async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
Expand Down
1 change: 1 addition & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub const ENGLISH_DAYS: [chrono::Weekday; 2] = [chrono::Weekday::Wed, chrono::We
pub const ENGLISH_DAY_WHITELIST: [u64; 1] = [1072587560116817930];
pub const DUDE_EMOJI: (u64, &str) = (1257619715430420540, "tonohmm");
pub const ENGLISH_CHANNEL_ID: u64 = 1246229913099305013;
pub const GITHUB_ORGANIZATION: &str = "https://github.com/Chads-Programming";
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod router;
mod state;
mod utils;
mod wallet;
mod welcome;

use std::sync::Arc;

Expand Down
59 changes: 59 additions & 0 deletions src/welcome/banner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::path::Path;

use chrono::Utc;
use serenity::all::{Context, GuildId, Member};
use tracing::log::error as log_error;

use crate::{consts, utils};

pub async fn send_welcome_banner(guild_id: &GuildId, ctx: &Context, member: &Member) {
let response = reqwest::get(member.face()).await.unwrap();
let avatar = response.bytes().await.unwrap();

let position_number = guild_id
.to_guild_cached(&ctx)
.map(|g| g.member_count as usize)
.unwrap_or(1);

let output_path = format!("/tmp/{}_welcome.png", member.user.name);

let dt = Utc::now();
let timestamp: String = dt.timestamp().to_string();

let digits: Vec<_> = timestamp
.chars()
.map(|d| d.to_digit(10).unwrap_or(5))
.collect();

let random_number = *digits.last().unwrap_or(&5);

let banner = if random_number > 3 {
"./assets/banner.png"
} else {
"./assets/pride_banner.png"
};

if let Err(err) = gen_image::generate(
&avatar,
banner,
member.distinct(),
position_number,
&output_path,
include_bytes!("../../assets/fonts/Roboto-Bold.ttf"),
include_bytes!("../../assets/fonts/Roboto-Regular.ttf"),
) {
log_error!("{err:?}");
}

let send_result = utils::send_file_message_to_channel(
&ctx.http,
consts::WELCOME_CHANNEL_ID,
&format!("Bienvenido a este humilde servidor: <@{}>", member.user.id),
Path::new(&output_path),
)
.await;

if let Err(err) = send_result {
log_error!("{err:?}");
}
}
41 changes: 41 additions & 0 deletions src/welcome/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use serenity::all::{Context, User, UserId};
use tracing::error;

use crate::consts;

pub async fn send_dm_welcome_information(ctx: &Context, user: &User) {
match user.create_dm_channel(&ctx.http).await {
Ok(channel) => {
channel
.say(&ctx.http, generate_welcome_information(user.id))
.await
.unwrap();
}
Err(err) => {
error!("{err:?}");
}
}
}

fn generate_welcome_information(user_id: UserId) -> String {
let github_information = format!(
"🌟 **Proyectos de la comunidad** 🌟\n\
Tu participación es clave para el éxito de nuestra comunidad. Si quieres colaborar y formar parte de nuestros proyectos, puedes colaborar en nuestros [repositorios]({}).\n\
Aquí podrás contribuir con tus habilidades, ideas y participar activamente en el crecimiento de nuestra comunidad, así podrás desbloquear el badge de `colaborador`.\n", consts::GITHUB_ORGANIZATION
);

let wallet_information = String::from(
"🔗 **Wallet**:\n\
Aquí te dejamos información importante sobre cómo interactuar con la wallet interna del servidor a través de comandos.\n\n\
* `/register_wallet`: ¡Registra tu wallet personal!\n\
* `/donate_coins`: Si alguien del servidor te ha ayudado en algo, puedes donarle **chad-coins** para mostrarle tu gratitud. ¡Cada contribución es muy apreciada!\n\
* `/wallet_info`: Para ver el estado de tu wallet\n\
* `/wallet_leaderboard`: ¡Descubre quién lidera la lista de wallets con más dinero! ¿Serás tú el próximo en llegar a la cima?\n\n\
Recuerda registrarte primero en la wallet y cada semana se te acreditarán **chad-coins** (solo válido dentro del servidor) ¡Estamos emocionados de que formes parte!"
);

format!(
"🎉 **Bienvenido/a: ** <@{}> 🎉\nTe dejamos presente la siguiente información: \n\n{github_information}\n\n{wallet_information}",
user_id
)
}
2 changes: 2 additions & 0 deletions src/welcome/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod banner;
pub mod info;

0 comments on commit e0e2f3f

Please sign in to comment.