This commit is contained in:
nora 2022-06-16 19:47:10 +02:00
parent 1f6021b555
commit ed6b2a7dfb
4 changed files with 87 additions and 18 deletions

View file

@ -197,11 +197,11 @@ async fn lawsuit_command_handler(
UserOption::get_optional(options.get(5)).wrap_err("accused_layer")?; UserOption::get_optional(options.get(5)).wrap_err("accused_layer")?;
let mut lawsuit = Lawsuit { let mut lawsuit = Lawsuit {
plaintiff: plaintiff.id.into(), plaintiff: plaintiff.0.id.into(),
accused: accused.id.into(), accused: accused.0.id.into(),
judge: judge.id.into(), judge: judge.0.id.into(),
plaintiff_layer: plaintiff_layer.map(|user| user.id.into()), plaintiff_lawyer: plaintiff_layer.map(|user| user.0.id.into()),
accused_layer: accused_layer.map(|user| user.id.into()), accused_lawyer: accused_layer.map(|user| user.0.id.into()),
reason: reason.to_owned(), reason: reason.to_owned(),
state: LawsuitState::Initial, state: LawsuitState::Initial,
court_room: None, court_room: None,
@ -277,13 +277,13 @@ struct UserOption;
#[nougat::gat] #[nougat::gat]
impl GetOption for UserOption { impl GetOption for UserOption {
type Get<'a> = &'a User; type Get<'a> = (&'a User, &'a Option<PartialMember>);
fn extract( fn extract(
command: &ApplicationCommandInteractionDataOptionValue, command: &ApplicationCommandInteractionDataOptionValue,
) -> crate::Result<Self::Get<'_>> { ) -> crate::Result<Self::Get<'_>> {
if let ApplicationCommandInteractionDataOptionValue::User(user, _) = command { if let ApplicationCommandInteractionDataOptionValue::User(user, member) = command {
Ok(user) Ok((user, member))
} else { } else {
Err(eyre!("Expected user!")) Err(eyre!("Expected user!"))
} }

View file

@ -1,4 +1,5 @@
use color_eyre::Result; use color_eyre::Result;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::{ use serenity::{
http::Http, http::Http,
@ -23,8 +24,8 @@ pub enum LawsuitState {
pub struct Lawsuit { pub struct Lawsuit {
pub plaintiff: SnowflakeId, pub plaintiff: SnowflakeId,
pub accused: SnowflakeId, pub accused: SnowflakeId,
pub plaintiff_layer: Option<SnowflakeId>, pub plaintiff_lawyer: Option<SnowflakeId>,
pub accused_layer: Option<SnowflakeId>, pub accused_lawyer: Option<SnowflakeId>,
pub judge: SnowflakeId, pub judge: SnowflakeId,
pub reason: String, pub reason: String,
pub state: LawsuitState, pub state: LawsuitState,
@ -38,9 +39,7 @@ impl Lawsuit {
guild_id: GuildId, guild_id: GuildId,
mongo_client: &Mongo, mongo_client: &Mongo,
) -> Result<Response> { ) -> Result<Response> {
let state = mongo_client let state = mongo_client.find_or_insert_state(guild_id.into()).await?;
.find_or_insert_state(guild_id.into())
.await?;
let free_room = state let free_room = state
.court_rooms .court_rooms
@ -74,7 +73,7 @@ impl Lawsuit {
)), )),
}; };
self.court_room = Some(room.channel_id.clone()); self.court_room = Some(room.channel_id);
let result = self let result = self
.send_process_open_message(http, guild_id, &room) .send_process_open_message(http, guild_id, &room)
@ -85,6 +84,40 @@ impl Lawsuit {
return Ok(response); return Ok(response);
} }
mongo_client.add_lawsuit(guild_id.into(), self).await?;
mongo_client
.set_court_room(
guild_id.into(),
room.channel_id,
doc! { "court_rooms.$.ongoing_lawsuit": true },
)
.await?;
async fn assign_role(
user: SnowflakeId,
http: &Http,
guild_id: GuildId,
role_id: SnowflakeId,
) -> Result<()> {
let mut member = guild_id.member(http, user).await.wrap_err("fetch member")?;
member
.add_role(http, role_id)
.await
.wrap_err("add role to member")?;
Ok(())
}
assign_role(self.accused, http, guild_id, room.role_id).await?;
if let Some(accused_lawyer) = self.accused_lawyer {
assign_role(accused_lawyer, http, guild_id, room.role_id).await?;
}
assign_role(self.plaintiff, http, guild_id, room.role_id).await?;
if let Some(plaintiff_lawyer) = self.plaintiff_lawyer {
assign_role(plaintiff_lawyer, http, guild_id, room.role_id).await?;
}
assign_role(self.judge, http, guild_id, room.role_id).await?;
Ok(Response::Simple(format!( Ok(Response::Simple(format!(
"ha eine ufgmacht im channel <#{}>", "ha eine ufgmacht im channel <#{}>",
room.channel_id room.channel_id
@ -118,7 +151,7 @@ impl Lawsuit {
.field("Kläger", format!("<@{}>", self.plaintiff), false) .field("Kläger", format!("<@{}>", self.plaintiff), false)
.field( .field(
"Anwalt des Klägers", "Anwalt des Klägers",
match &self.plaintiff_layer { match &self.plaintiff_lawyer {
Some(lawyer) => format!("<@{}>", lawyer), Some(lawyer) => format!("<@{}>", lawyer),
None => "TBD".to_string(), None => "TBD".to_string(),
}, },
@ -127,7 +160,7 @@ impl Lawsuit {
.field("Angeklagter", format!("<@{}>", self.accused), false) .field("Angeklagter", format!("<@{}>", self.accused), false)
.field( .field(
"Anwalt des Angeklagten", "Anwalt des Angeklagten",
match &self.accused_layer { match &self.accused_lawyer {
Some(lawyer) => format!("<@{}>", lawyer), Some(lawyer) => format!("<@{}>", lawyer),
None => "TBD".to_string(), None => "TBD".to_string(),
}, },

View file

@ -1,3 +1,5 @@
extern crate core;
mod handler; mod handler;
mod lawsuit; mod lawsuit;
mod model; mod model;

View file

@ -192,6 +192,40 @@ impl Mongo {
Ok(()) Ok(())
} }
pub async fn add_lawsuit(&self, guild_id: SnowflakeId, lawsuit: &Lawsuit) -> Result<()> {
let _ = self.find_or_insert_state(guild_id).await?;
let coll = self.state_coll();
coll.update_one(
doc! { "guild_id": &guild_id },
doc! { "$push": { "lawsuits": bson::to_bson(lawsuit).wrap_err("invalid bson for lawsuit")? } },
None,
)
.await
.wrap_err("push lawsuit")?;
Ok(())
}
pub async fn set_court_room(
&self,
guild_id: SnowflakeId,
channel_id: SnowflakeId,
value: impl Into<Bson>,
) -> Result<()> {
let _ = self.find_or_insert_state(guild_id).await?;
let coll = self.state_coll();
coll.update_one(
doc! { "guild_id": &guild_id, "court_rooms.channel_id": channel_id },
doc! { "$set": value.into() },
None,
)
.await
.wrap_err("set courtroom")?;
Ok(())
}
fn state_coll(&self) -> Collection<State> { fn state_coll(&self) -> Collection<State> {
self.db.collection("state") self.db.collection("state")
} }