diff --git a/src/commands/cmd_utils.rs b/src/commands/cmd_utils.rs index 127ed70..138b8fc 100644 --- a/src/commands/cmd_utils.rs +++ b/src/commands/cmd_utils.rs @@ -8,6 +8,10 @@ pub async fn get_replied_user(ctx: Context<'_>) -> &serenity::User { ref_msg.map_or(ctx.author(), |x| &x.author) } +pub fn same_user(u1: &User, u2: &User) -> bool { + u1.id == u2.id +} + pub fn get_rand_embed_from_type(embed_type: &EmbedType) -> Result<&'static str, Error> { let embed_option = COMMAND_EMBEDS[embed_type].choose(&mut rand::thread_rng()); match embed_option { diff --git a/src/commands/embed_commands/bonk.rs b/src/commands/embed_commands/bonk.rs index 480c404..81e8e77 100644 --- a/src/commands/embed_commands/bonk.rs +++ b/src/commands/embed_commands/bonk.rs @@ -9,7 +9,7 @@ pub async fn bonk( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = get_rand_embed_from_type(&EmbedType::Bonk)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default().content("バカ!").embed( serenity::CreateEmbed::new() @@ -25,11 +25,14 @@ pub async fn bonk( return Ok(()); } - let response: String = format!( - "**{}** *bonks* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *bonks* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/bury.rs b/src/commands/embed_commands/bury.rs index 4f4b1a6..59369ed 100644 --- a/src/commands/embed_commands/bury.rs +++ b/src/commands/embed_commands/bury.rs @@ -18,11 +18,14 @@ pub async fn bury( let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Bury)?; let target_replied_user = user.as_ref().unwrap(); - let response: String = format!( - "**{}** *buries* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *buries* **{}**", u1, u2), + ) + .await; let bot_user = Arc::clone(&ctx.data().bot_user); diff --git a/src/commands/embed_commands/hug.rs b/src/commands/embed_commands/hug.rs index 52177b5..77e4470 100644 --- a/src/commands/embed_commands/hug.rs +++ b/src/commands/embed_commands/hug.rs @@ -9,7 +9,7 @@ pub async fn hug( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Hug)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content("Aww~ I'll hug you!") @@ -27,11 +27,14 @@ pub async fn hug( return Ok(()); } - let response: String = format!( - "**{}** *hugs* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *hugs* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/kick.rs b/src/commands/embed_commands/kick.rs index 2b767e6..1880e4f 100644 --- a/src/commands/embed_commands/kick.rs +++ b/src/commands/embed_commands/kick.rs @@ -8,7 +8,7 @@ pub async fn kick( ) -> Result<(), Error> { let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Kick)?; - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send(poise::CreateReply::default().content(format!( "{}, why would you kick yourself...? Weirdo...", target_replied_user.mention() @@ -17,11 +17,14 @@ pub async fn kick( return Ok(()); } - let response: String = format!( - "**{}** *kicks* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *kicks* **{}**", u1, u2), + ) + .await; let bot_user = Arc::clone(&ctx.data().bot_user); diff --git a/src/commands/embed_commands/kill.rs b/src/commands/embed_commands/kill.rs index eaa62f6..df26480 100644 --- a/src/commands/embed_commands/kill.rs +++ b/src/commands/embed_commands/kill.rs @@ -8,17 +8,20 @@ pub async fn kill( ) -> Result<(), Error> { let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Kill)?; - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send(poise::CreateReply::default().content("No.")) .await?; return Ok(()); } - let response: String = format!( - "**{}** *kills* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *kills* **{}**", u1, u2), + ) + .await; let bot_user = Arc::clone(&ctx.data().bot_user); diff --git a/src/commands/embed_commands/kiss.rs b/src/commands/embed_commands/kiss.rs index 97b4cd8..70f9dd1 100644 --- a/src/commands/embed_commands/kiss.rs +++ b/src/commands/embed_commands/kiss.rs @@ -8,7 +8,7 @@ pub async fn kiss( ) -> Result<(), Error> { let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content("Aww~ I won't kiss you! Ahahahah!") @@ -27,11 +27,14 @@ pub async fn kiss( } let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Kiss)?; - let response: String = format!( - "**{}** *kisses* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *kisses* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/mod.rs b/src/commands/embed_commands/mod.rs index 0f7c539..42dcf79 100644 --- a/src/commands/embed_commands/mod.rs +++ b/src/commands/embed_commands/mod.rs @@ -1,3 +1,31 @@ +use crate::prelude::*; + +pub async fn get_name(ctx: &Context<'_>, guild_id: Option, u: &serenity::User) -> String { + let base_case = || u.name.clone(); + match guild_id { + Some(id) => u.nick_in(ctx, id).await.unwrap_or(base_case()), + None => base_case(), + } +} + +pub async fn user_interaction( + ctx: &Context<'_>, + guild_id: Option, + user_1: &serenity::User, + user_2: &serenity::User, + f: fn(name_1: &str, name_2: &str) -> String, +) -> String { + let [ref name_1, ref name_2] = join_all([ + get_name(ctx, guild_id, user_1), + get_name(ctx, guild_id, user_2), + ]) + .await[..] else { + return String::from(""); + }; + + f(name_1, name_2) +} + // #region User interaction commands pub mod tieup; diff --git a/src/commands/embed_commands/nom.rs b/src/commands/embed_commands/nom.rs index 77676de..33b57ec 100644 --- a/src/commands/embed_commands/nom.rs +++ b/src/commands/embed_commands/nom.rs @@ -9,7 +9,7 @@ pub async fn nom( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Nom)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content(format!("{} noms themselves...?", target_replied_user.name)) @@ -27,11 +27,14 @@ pub async fn nom( return Ok(()); } - let response: String = format!( - "**{}** *noms* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *noms* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/pat.rs b/src/commands/embed_commands/pat.rs index 6babde2..cfb11ad 100644 --- a/src/commands/embed_commands/pat.rs +++ b/src/commands/embed_commands/pat.rs @@ -9,7 +9,7 @@ pub async fn pat( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Pat)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content("Aww~ I'll pat you!") @@ -27,11 +27,14 @@ pub async fn pat( return Ok(()); } - let response: String = format!( - "**{}** *pats* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *noms* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/punch.rs b/src/commands/embed_commands/punch.rs index 5bc8b2d..6debe0f 100644 --- a/src/commands/embed_commands/punch.rs +++ b/src/commands/embed_commands/punch.rs @@ -8,17 +8,20 @@ pub async fn punch( ) -> Result<(), Error> { let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Punch)?; - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send(poise::CreateReply::default().content("I won't punch you! *pouts*")) .await?; return Ok(()); } - let response: String = format!( - "**{}** *punches* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *punches* **{}**", u1, u2), + ) + .await; let bot_user = Arc::clone(&ctx.data().bot_user); diff --git a/src/commands/embed_commands/slap.rs b/src/commands/embed_commands/slap.rs index 4d01a3f..ef6e10d 100644 --- a/src/commands/embed_commands/slap.rs +++ b/src/commands/embed_commands/slap.rs @@ -9,7 +9,7 @@ pub async fn slap( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::Slap)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content("Why do you want to get slapped??") @@ -32,11 +32,14 @@ pub async fn slap( return Ok(()); } - let response: String = format!( - "**{}** *slaps* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *slaps* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/commands/embed_commands/tieup.rs b/src/commands/embed_commands/tieup.rs index 2208590..2e88b11 100644 --- a/src/commands/embed_commands/tieup.rs +++ b/src/commands/embed_commands/tieup.rs @@ -9,7 +9,7 @@ pub async fn tieup( let target_replied_user = user.as_ref().unwrap_or(get_replied_user(ctx).await); let embed_item: &str = cmd_utils::get_rand_embed_from_type(&EmbedType::TieUp)?; let bot_user = Arc::clone(&ctx.data().bot_user); - if target_replied_user == ctx.author() { + if same_user(target_replied_user, ctx.author()) { ctx.send( poise::CreateReply::default() .content("Y'know what? Sure, I'll tie you up!") @@ -32,11 +32,14 @@ pub async fn tieup( return Ok(()); } - let response: String = format!( - "**{}** *ties up* **{}**", - ctx.author().name, - target_replied_user.name - ); + let response: String = user_interaction( + &ctx, + ctx.guild_id(), + ctx.author(), + target_replied_user, + |u1, u2| format!("**{}** *ties up* **{}**", u1, u2), + ) + .await; let embed = serenity::CreateEmbed::new() .title(response) diff --git a/src/prelude.rs b/src/prelude.rs index 7188eb4..5aece6e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -19,7 +19,10 @@ pub use crate::{ assets::Assets, cdn_url, commands::{ - cmd_utils::{self, get_rand_embed_from_type, get_replied_user, make_full_response}, + cmd_utils::{ + self, get_rand_embed_from_type, get_replied_user, make_full_response, same_user, + }, + embed_commands::user_interaction, level_logic::update_level, }, data::{