Skip to content

Commit

Permalink
feat(warn): send warn reason
Browse files Browse the repository at this point in the history
  • Loading branch information
velascoandres committed Nov 1, 2024
1 parent 5023a4c commit fe7e1ec
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 33 deletions.
9 changes: 2 additions & 7 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{commands, consts, events, utils, welcome};
use crate::{commands, consts, events, welcome};
use serenity::all::{
CommandInteraction, CreateAllowedMentions, CreateEmbed, CreateInteractionResponseFollowup,
Member, Message,
Expand Down Expand Up @@ -258,12 +258,7 @@ impl EventHandler for Handler {
}
"list_projects" => commands::list_projects::run(&ctx).await.into(),
"warn" => {
let user_option = utils::get_user_from_query(&command.data.options());

let content = match user_option {
Some(user) => commands::warn::run(&ctx, &user).await,
None => "Debe establecer un usuario".to_string(),
};
let content = commands::warn::run(&ctx, &command).await;

ContentPayload::from_str(content).ephemeral(true)
}
Expand Down
50 changes: 43 additions & 7 deletions src/commands/warn.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
use serenity::all::{CommandOptionType, Context, CreateCommand, CreateCommandOption, User};
use serenity::all::{
CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption,
ResolvedOption, ResolvedValue,
};

use crate::gifs;
pub async fn run(ctx: &Context, interaction: &CommandInteraction) -> String {
let options = &interaction.data.options().clone();

pub async fn run(ctx: &Context, user: &User) -> String {
let target_user = user.clone();
let option_user = if let Some(ResolvedOption {
value: ResolvedValue::User(user, _),
..
}) = options.first()
{
Some(user)
} else {
None
};

let reason_option = if let Some(ResolvedOption {
value: ResolvedValue::String(reason),
..
}) = options.get(1)
{
Some(*reason)
} else {
None
};

if option_user.is_none() {
return "No se especifico un usuario".to_string();
}

let target_user = option_user.unwrap();

match target_user.create_dm_channel(&ctx.http).await {
Ok(channel) => {
let name = target_user.name;
let image_url = gifs::WARN_CAT;
let message = format!("\n**Quieto ahí pibardo!**\nEstimado: *{name}* se le informa educamente que **ha sido advertido** \n[hungry_cat]({image_url})");
let name = target_user.name.clone();
let base_message = format!("\n**Quieto ahí pibardo!**\nEstimado: *{name}* se le informa educamente que **ha sido advertido**");

let message = if let Some(reason) = reason_option {
format!("{base_message}\nRazón:`{reason}`")
} else {
base_message
};

channel.say(&ctx.http, message).await.unwrap();

Expand All @@ -26,4 +58,8 @@ pub fn register() -> CreateCommand {
CreateCommandOption::new(CommandOptionType::User, "user", "The user to lookup")
.required(true),
)
.add_option(
CreateCommandOption::new(CommandOptionType::String, "reason", "The reason of warn")
.required(true),
)
}
1 change: 0 additions & 1 deletion src/gifs.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod consts;
mod db;
mod errors;
mod events;
mod gifs;
mod github;
mod helpers;
mod projects;
Expand Down
18 changes: 1 addition & 17 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
use std::path::Path;

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

#[allow(suspicious_double_ref_op)]
pub fn get_user_from_query(options: &[ResolvedOption]) -> Option<User> {
if let Some(ResolvedOption {
value: ResolvedValue::User(user, _),
..
}) = options.first()
{
return Some(user.clone().clone());
}

None
}

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

0 comments on commit fe7e1ec

Please sign in to comment.