Skip to content

Commit

Permalink
feat: distance command (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
crnvl committed Oct 21, 2023
1 parent 75099b1 commit a3e29c1
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 29 deletions.
2 changes: 1 addition & 1 deletion xp-bot/src/commands/admin/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl XpCommand for AddCommand {
.embed(|embed| {
embed.description(format!(
"Successfully added **{}** xp to <@{}>.",
format_number(amount as u64),
format_number(amount as i64),
user
));
embed.color(colors::green());
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/admin/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl XpCommand for RemoveCommand {
.embed(|embed| {
embed.description(format!(
"Successfully removed **{}** xp from <@{}>.",
format_number(amount as u64),
format_number(amount as i64),
user
));
embed.color(colors::green());
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/admin/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl XpCommand for SetCommand {
embed.description(format!(
"Successfully set xp of <@{}> to **{}**.",
user,
format_number(amount as u64)
format_number(amount as i64)
));
embed.color(colors::green());
embed
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/games/daily.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl XpCommand for DailyCommand {
message.embed(|embed| {
embed.description(format!(
"You claimed **{}** xp. Your streak is now **{}**.\n\n{}",
format_number(xp_to_add),
format_number(xp_to_add as i64),
streak,
old_streak_msg
));
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/games/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl XpCommand for FishCommand {
message.embed(|embed| {
embed.description(format!(
":fishing_pole_and_fish: | You got **{}** xp for finding **{}**.",
format_number(game_result.xp as u64),
format_number(game_result.xp),
game_result.item
));
embed.color(colors::green())
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/games/loot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl XpCommand for LootCommand {
embed.description(format!(
":package: | You found **{}** crate and got **{}** xp!",
game_result.item,
format_number(game_result.xp as u64),
format_number(game_result.xp),
));
embed.color(colors::green())
})
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/commands/games/roll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl XpCommand for RollCommand {
embed.description(format!(
":game_die: | You rolled a **{}** and got **{}** xp!",
random_num,
format_number(random_num * guild.values.rollXP as u64),
format_number((random_num * guild.values.rollXP as u64) as i64),
));
embed.color(colors::green())
})
Expand Down
103 changes: 103 additions & 0 deletions xp-bot/src/commands/misc/distance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use serenity::{
async_trait,
builder::{CreateApplicationCommand, CreateEmbed},
model::prelude::{
application_command::ApplicationCommandInteraction, command::CommandOptionType,
},
prelude::Context,
};
use xp_db_connector::guild_member::GuildMember;

use crate::commands::XpCommand;

pub struct DistanceCommand;

#[async_trait]
impl XpCommand for DistanceCommand {
fn name(&self) -> &'static str {
"distance"
}

fn register<'a>(
&self,
command: &'a mut CreateApplicationCommand,
) -> &'a mut CreateApplicationCommand {
command
.name("distance")
.description("See how far you are from another user.")
.create_option(|option| {
option
.name("user")
.description("The user you want to check.")
.kind(CommandOptionType::User)
.required(true)
})
}

async fn exec(
&self,
ctx: &Context,
command: &ApplicationCommandInteraction,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let guild_member =
GuildMember::from_id(command.guild_id.unwrap().0, command.user.id.0).await;

if guild_member.is_err() {
log::error!("Could not get guild member: {:?}", command.user.id.0);
return Ok(());
}

let guild_member = guild_member?;

let user_id = command.data.options[0]
.value
.as_ref()
.unwrap()
.as_str()
.unwrap()
.parse::<u64>()
.unwrap();

let other_guild_member = GuildMember::from_id(command.guild_id.unwrap().0, user_id).await;

if other_guild_member.is_err() {
log::error!("Could not get guild member: {:?}", user_id);
return Ok(());
}

let other_guild_member = other_guild_member?;
let required_xp = other_guild_member.xp as i64 - guild_member.xp as i64;

let username = ctx.http.get_user(user_id).await.unwrap().name.clone();

let description_str = if required_xp < 0 {
format!(
"You are **{} xp** ahead of **{}**.",
crate::utils::utils::format_number(required_xp.abs()),
username
)
} else {
format!(
"You are **{} xp** behind **{}**.",
crate::utils::utils::format_number(required_xp),
username
)
};

let mut embed = CreateEmbed::default();
embed.title("Distance");
embed.color(crate::utils::colors::blue());
embed.description(description_str.as_str());

let _ = command
.create_interaction_response(&ctx.http, |response| {
response.interaction_response_data(|message| {
message.add_embed(embed);
message
})
})
.await?;

Ok(())
}
}
6 changes: 3 additions & 3 deletions xp-bot/src/commands/misc/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl XpCommand for LevelCommand {
if required_xp > guild_member.xp as usize {
need_until = format!(
"You still need **{} xp** to reach level **{}**.",
crate::utils::utils::format_number(required_xp as u64 - guild_member.xp),
crate::utils::utils::format_number((required_xp as u64 - guild_member.xp) as i64),
level
);
}
Expand All @@ -75,9 +75,9 @@ impl XpCommand for LevelCommand {
message.embed(|embed: &mut CreateEmbed| {
embed.title(format!("Level {}", level)).description(format!(
"You need **{} xp** to reach level **{}**.\n You currently have **{} xp** (**{}%**).\n\n{}",
crate::utils::utils::format_number(required_xp as u64),
crate::utils::utils::format_number(required_xp as i64),
level,
crate::utils::utils::format_number(guild_member.xp as u64),
crate::utils::utils::format_number(guild_member.xp as i64),
(guild_member.xp as f32 / required_xp as f32 * 100.0).round(),
need_until
)).color(colors::blue())
Expand Down
1 change: 1 addition & 0 deletions xp-bot/src/commands/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod level;
pub mod rank;
pub mod settings;
pub mod voicetime;
pub mod distance;
26 changes: 13 additions & 13 deletions xp-bot/src/commands/misc/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,31 @@ impl XpCommand for SettingsCommand {
fields.push((
format!(
"Message xp: {}",
crate::utils::utils::format_number(guild.values.messagexp as u64)
crate::utils::utils::format_number(guild.values.messagexp as i64)
),
"The amount of xp a user gets per message.".to_string(),
true,
));
fields.push((
format!(
"Message cooldown: {}",
crate::utils::utils::format_number(guild.values.messagecooldown as u64)
crate::utils::utils::format_number(guild.values.messagecooldown as i64)
),
"The time in seconds until the next message will be counted.".to_string(),
true,
));
fields.push((
format!(
"Voice xp: {}",
crate::utils::utils::format_number(guild.values.voicexp as u64)
crate::utils::utils::format_number(guild.values.voicexp as i64)
),
"The amount of xp a user receives per minute in the voicechat.".to_string(),
true,
));
fields.push((
format!(
"Voice Join Cooldown: {}",
crate::utils::utils::format_number(guild.values.voicejoincooldown as u64)
crate::utils::utils::format_number(guild.values.voicejoincooldown as i64)
),
"The time in seconds until XP starts measuring the user's voicechat time."
.to_string(),
Expand All @@ -201,39 +201,39 @@ impl XpCommand for SettingsCommand {
fields.push((
format!(
"Reaction xp: {}",
crate::utils::utils::format_number(guild.values.reactionxp as u64)
crate::utils::utils::format_number(guild.values.reactionxp as i64)
),
"The amount of xp a user gets per reaction.".to_string(),
true,
));
fields.push((
format!(
"Loot xp: {}",
crate::utils::utils::format_number(guild.values.lootXP as u64)
crate::utils::utils::format_number(guild.values.lootXP as i64)
),
"The amount of xp a user gets for playing `/loot`.".to_string(),
true,
));
fields.push((
format!(
"Fish xp: {}",
crate::utils::utils::format_number(guild.values.fishXP as u64)
crate::utils::utils::format_number(guild.values.fishXP as i64)
),
"The amount of xp a user gets for playing `/fish`.".to_string(),
true,
));
fields.push((
format!(
"Roll xp: {}",
crate::utils::utils::format_number(guild.values.rollXP as u64)
crate::utils::utils::format_number(guild.values.rollXP as i64)
),
"The amount of xp a user gets for playing `/roll`.".to_string(),
true,
));
fields.push((
format!(
"Game cooldown: {}",
crate::utils::utils::format_number(guild.values.gamecooldown as u64)
crate::utils::utils::format_number(guild.values.gamecooldown as i64)
),
format!(
"`{}` The time in seconds until a user start another game.",
Expand All @@ -244,15 +244,15 @@ impl XpCommand for SettingsCommand {
fields.push((
format!(
"Trivia xp: {}",
crate::utils::utils::format_number(guild.values.triviaxp as u64)
crate::utils::utils::format_number(guild.values.triviaxp as i64)
),
"The maximum amount of xp a user gets for playing trivia.".to_string(),
true,
));
fields.push((
format!(
"Trivia cooldown: {}",
crate::utils::utils::format_number(guild.values.triviacooldown as u64)
crate::utils::utils::format_number(guild.values.triviacooldown as i64)
),
format!(
"`{}` The time in seconds until a user can start a new Trivia game.",
Expand All @@ -263,7 +263,7 @@ impl XpCommand for SettingsCommand {
fields.push((
format!(
"Maximum daily xp: {}",
crate::utils::utils::format_number(guild.values.maximumdailyxp as u64)
crate::utils::utils::format_number(guild.values.maximumdailyxp as i64)
),
"The maximum amount of xp obtainable by executing the `/daily` command."
.to_string(),
Expand All @@ -272,7 +272,7 @@ impl XpCommand for SettingsCommand {
fields.push((
format!(
"Maximum level: {}",
crate::utils::utils::format_number(guild.values.maximumlevel as u64)
crate::utils::utils::format_number(guild.values.maximumlevel as i64)
),
"The maximum level that can be reached.".to_string(),
true,
Expand Down
6 changes: 3 additions & 3 deletions xp-bot/src/commands/misc/voicetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ impl XpCommand for VoicetimeCommand {
"Level",
format!(
"**{} → {}**",
crate::utils::utils::format_number(current_level as u64),
crate::utils::utils::format_number(new_level as u64)
crate::utils::utils::format_number(current_level as i64),
crate::utils::utils::format_number(new_level as i64)
),
true,
);
}

embed.field(
"XP",
crate::utils::utils::format_number(voice_xp as u64),
crate::utils::utils::format_number(voice_xp as i64),
true,
);
embed.field("", "", true);
Expand Down
1 change: 1 addition & 0 deletions xp-bot/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub const COMMANDS: &[&dyn XpCommand] = &[
&misc::settings::SettingsCommand,
&misc::voicetime::VoicetimeCommand,
&misc::incognito::IncognitoCommand,
&misc::distance::DistanceCommand,
&admin::add::AddCommand,
&admin::set::SetCommand,
&admin::remove::RemoveCommand,
Expand Down
4 changes: 2 additions & 2 deletions xp-bot/src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,12 @@ impl Handler {
if level_difference > 0 {
embed.field(
"Level",
format!("**{} → {}**", crate::utils::utils::format_number(current_level as u64), crate::utils::utils::format_number(new_level as u64)),
format!("**{} → {}**", crate::utils::utils::format_number(current_level as i64), crate::utils::utils::format_number(new_level as i64)),
true,
);
}

embed.field("XP", crate::utils::utils::format_number(xp as u64), true);
embed.field("XP", crate::utils::utils::format_number(xp as i64), true);
embed.field("", "", true);
embed.color(colors::blue());
embed
Expand Down
2 changes: 1 addition & 1 deletion xp-bot/src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn calculate_total_boost_percentage_by_ids(
boost_percentage
}

pub fn format_number(number: u64) -> String {
pub fn format_number(number: i64) -> String {
let number_string = number.to_string();
let mut formatted_number = String::new();

Expand Down

0 comments on commit a3e29c1

Please sign in to comment.