Skip to content

Commit

Permalink
Refactor user interactions and same user
Browse files Browse the repository at this point in the history
This is both flexible and adaptive to change in case i want to add a
random quote after each command based on the type of interaction.
NOTE: This implementation also makes it so the guild nickname is used
(or defaults to the user name)
  • Loading branch information
1Git2Clone committed Dec 29, 2024
1 parent ed01780 commit 9db65f1
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 66 deletions.
4 changes: 4 additions & 0 deletions src/commands/cmd_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/bonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions src/commands/embed_commands/bury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/hug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/kick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);

Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/kiss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions src/commands/embed_commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
use crate::prelude::*;

pub async fn get_name(ctx: &Context<'_>, guild_id: Option<GuildId>, 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<GuildId>,
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;
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/slap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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??")
Expand All @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/commands/embed_commands/tieup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit 9db65f1

Please sign in to comment.