Skip to content

Commit

Permalink
set wom command
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfingers23 committed Nov 2, 2023
1 parent 09bb834 commit ac5dcb5
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 4 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions trackscape-discord-bot/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod set_clan_chat_channel;
pub mod set_diary_min_command;
pub mod set_quest_min_command;
pub mod set_threshold_command;
pub mod set_wom_id_command;
pub mod toggle_broadcasts;
59 changes: 59 additions & 0 deletions trackscape-discord-bot/src/commands/set_wom_id_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::database::BotMongoDb;
use serenity::builder;
use serenity::client::Context;
use serenity::model::prelude::application_command::{CommandDataOption, CommandDataOptionValue};
use serenity::model::prelude::command::CommandOptionType;
use serenity::model::prelude::Permissions;
use trackscape_discord_shared::database::guilds_db::RegisteredGuildModel;

pub fn register(
command: &mut builder::CreateApplicationCommand,
) -> &mut builder::CreateApplicationCommand {
command
.name("wom")
.description("Connects Trackscape to your Wise Old Man group.")
.default_member_permissions(Permissions::MANAGE_GUILD)
.create_option(|option| {
option
.name("group_id")
.description(
"Your Wise Old Man group id. Found on the side bar of your group page.",
)
.kind(CommandOptionType::Integer)
.required(true)
})
}

pub async fn run(
command: &Vec<CommandDataOption>,
_ctx: &Context,
db: &BotMongoDb,
guild_id: u64,
) -> Option<String> {
let saved_guild_query = db.guilds.get_by_guild_id(guild_id).await;
match saved_guild_query {
Ok(saved_guild) => {
let mut saved_guild = saved_guild.unwrap_or(RegisteredGuildModel::new(guild_id));
let wom_id_command_option = command
.get(0)
.expect("Expected a command option")
.resolved
.as_ref()
.expect("Expected a command option");

return if let CommandDataOptionValue::Integer(wom_id) = wom_id_command_option {
//TODO when we have a wom client check if its a valid id
saved_guild.wom_id = Some(*wom_id);
let _ = db.guilds.update_guild(saved_guild).await;
None
} else {
Some("Invalid Wise Old Man Id.".to_string())
};
}
Err(_) => {
return Some(
"No saved guild was found. Please try adding and removing the bot".to_string(),
);
}
}
}
13 changes: 13 additions & 0 deletions trackscape-discord-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ impl EventHandler for Bot {
.create_application_command(|command| {
commands::toggle_broadcasts::register(command)
})
.create_application_command(|command| {
commands::set_wom_id_command::register(command)
})
})
.await;

Expand Down Expand Up @@ -277,6 +280,15 @@ impl EventHandler for Bot {
)
.await
}
"wom" => {
commands::set_wom_id_command::run(
&command.data.options,
&ctx,
&self.mongo_db,
command.guild_id.unwrap().0,
)
.await
}
_ => {
info!("not implemented :(");
None
Expand Down Expand Up @@ -326,6 +338,7 @@ pub async fn create_commands_for_guild(guild_id: &GuildId, ctx: Context) {
commands::reset_broadcasts_thresholds::register(command)
})
.create_application_command(|command| commands::toggle_broadcasts::register(command))
.create_application_command(|command| commands::set_wom_id_command::register(command))
})
.await;
match commands {
Expand Down
5 changes: 4 additions & 1 deletion trackscape-discord-shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ tokio = "1.26.0"
log = "0.4.20"
async-trait = "0.1.73"
mockall = "0.11.4"
anyhow = "1.0.75"
anyhow = "1.0.75"

[dev-dependencies]
pretty_assertions = "1.4.0"
69 changes: 66 additions & 3 deletions trackscape-discord-shared/src/database/clan_mates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::database::DropLogsDb;
use crate::osrs_broadcast_extractor::osrs_broadcast_extractor::DropItemBroadcast;
use async_trait::async_trait;
use futures::TryStreamExt;
use mockall::predicate::*;
use mockall::*;
use mongodb::bson::{doc, DateTime};
Expand All @@ -11,15 +9,21 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ClanMateModel {
pub guild_id: u64,
pub player_name: String,
pub wom_player_id: Option<u64>,
pub previous_names: Vec<String>,
pub created_at: DateTime,
}

impl ClanMateModel {
pub const COLLECTION_NAME: &'static str = "clan_mates";

pub fn new(guild_id: u64) -> Self {
pub fn new(guild_id: u64, player_name: String, wom_player_id: Option<u64>) -> Self {
Self {
guild_id,
wom_player_id,
previous_names: Vec::new(),
player_name,
created_at: DateTime::now(),
}
}
Expand All @@ -29,11 +33,70 @@ impl ClanMateModel {
#[async_trait]
pub trait ClanMates {
fn new_instance(mongodb: Database) -> Self;

async fn create_new_clan_mate(
&self,
guild_id: u64,
player_name: String,
wom_player_id: Option<u64>,
) -> Result<(), anyhow::Error>;

async fn find_by_current_name(
&self,
player_name: String,
) -> Result<Option<ClanMateModel>, anyhow::Error>;

async fn find_by_previous_name(
&self,
player_name: String,
) -> Result<Option<ClanMateModel>, anyhow::Error>;
}

#[async_trait]
impl ClanMates for DropLogsDb {
fn new_instance(mongodb: Database) -> Self {
Self { db: mongodb }
}

async fn create_new_clan_mate(
&self,
guild_id: u64,
player_name: String,
wom_player_id: Option<u64>,
) -> Result<(), anyhow::Error> {
let collection = self
.db
.collection::<ClanMateModel>(ClanMateModel::COLLECTION_NAME);
let clan_mate = ClanMateModel::new(guild_id, player_name, wom_player_id);
let _ = collection.insert_one(clan_mate, None).await?;
Ok(())
}

async fn find_by_current_name(
&self,
player_name: String,
) -> Result<Option<ClanMateModel>, anyhow::Error> {
let collection = self
.db
.collection::<ClanMateModel>(ClanMateModel::COLLECTION_NAME);
let filter = doc! {
"player_name": bson::to_bson(&player_name).unwrap(),
};
let result = collection.find_one(filter, None).await?;
Ok(result)
}

async fn find_by_previous_name(
&self,
player_name: String,
) -> Result<Option<ClanMateModel>, anyhow::Error> {
let collection = self
.db
.collection::<ClanMateModel>(ClanMateModel::COLLECTION_NAME);
let filter = doc! {
"previous_names": bson::to_bson(&player_name).unwrap(),
};
let result = collection.find_one(filter, None).await?;
Ok(result)
}
}
2 changes: 2 additions & 0 deletions trackscape-discord-shared/src/database/guilds_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct RegisteredGuildModel {
pub min_quest_difficulty: Option<QuestDifficulty>,
pub min_diary_tier: Option<DiaryTier>,
pub pk_value_threshold: Option<i64>,
pub wom_id: Option<i64>,
pub created_at: Option<DateTime>,
}

Expand All @@ -47,6 +48,7 @@ impl RegisteredGuildModel {
min_quest_difficulty: None,
min_diary_tier: None,
pk_value_threshold: None,
wom_id: None,
created_at: DateTime::now().into(),
}
}
Expand Down

0 comments on commit ac5dcb5

Please sign in to comment.