create almost done

This commit is contained in:
nora 2022-06-16 19:54:35 +02:00
parent ed6b2a7dfb
commit 984125841c
3 changed files with 45 additions and 24 deletions

View file

@ -12,6 +12,7 @@ use tracing::{debug, error, info};
use crate::{ use crate::{
lawsuit::{Lawsuit, LawsuitState}, lawsuit::{Lawsuit, LawsuitState},
model::SnowflakeId,
Mongo, WrapErr, Mongo, WrapErr,
}; };
@ -196,7 +197,7 @@ async fn lawsuit_command_handler(
let accused_layer = let accused_layer =
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 lawsuit = Lawsuit {
plaintiff: plaintiff.0.id.into(), plaintiff: plaintiff.0.id.into(),
accused: accused.0.id.into(), accused: accused.0.id.into(),
judge: judge.0.id.into(), judge: judge.0.id.into(),
@ -204,16 +205,14 @@ async fn lawsuit_command_handler(
accused_lawyer: accused_layer.map(|user| user.0.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: SnowflakeId(0),
}; };
let response = lawsuit let response = lawsuit
.initialize(&ctx.http, guild_id, mongo_client) .initialize(ctx.http.clone(), guild_id, mongo_client.clone())
.await .await
.wrap_err("initialize lawsuit")?; .wrap_err("initialize lawsuit")?;
info!(?lawsuit, "Created lawsuit");
Ok(response) Ok(response)
} }
"set_category" => { "set_category" => {

View file

@ -1,3 +1,5 @@
use std::sync::Arc;
use color_eyre::Result; use color_eyre::Result;
use mongodb::bson::doc; use mongodb::bson::doc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -5,7 +7,7 @@ use serenity::{
http::Http, http::Http,
model::{channel::PermissionOverwriteType, prelude::*, Permissions}, model::{channel::PermissionOverwriteType, prelude::*, Permissions},
}; };
use tracing::info; use tracing::{error, info};
use crate::{ use crate::{
handler::Response, handler::Response,
@ -29,15 +31,15 @@ pub struct Lawsuit {
pub judge: SnowflakeId, pub judge: SnowflakeId,
pub reason: String, pub reason: String,
pub state: LawsuitState, pub state: LawsuitState,
pub court_room: Option<SnowflakeId>, pub court_room: SnowflakeId,
} }
impl Lawsuit { impl Lawsuit {
pub async fn initialize( pub async fn initialize(
&mut self, mut self,
http: &Http, http: Arc<Http>,
guild_id: GuildId, guild_id: GuildId,
mongo_client: &Mongo, mongo_client: Mongo,
) -> Result<Response> { ) -> Result<Response> {
let state = mongo_client.find_or_insert_state(guild_id.into()).await?; let state = mongo_client.find_or_insert_state(guild_id.into()).await?;
@ -53,11 +55,11 @@ impl Lawsuit {
// create room // create room
let result = create_room( let result = create_room(
http, &http,
guild_id, guild_id,
state.court_rooms.len(), state.court_rooms.len(),
*category, *category,
mongo_client, &mongo_client,
) )
.await .await
.wrap_err("create new room")?; .wrap_err("create new room")?;
@ -73,10 +75,8 @@ impl Lawsuit {
)), )),
}; };
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)
.await .await
.wrap_err("send process open message")?; .wrap_err("send process open message")?;
@ -84,6 +84,28 @@ impl Lawsuit {
return Ok(response); return Ok(response);
} }
let channel_id = room.channel_id;
self.court_room = channel_id;
tokio::spawn(async move {
if let Err(err) = self.setup(guild_id, http, mongo_client, room).await {
error!(?err, "Error setting up lawsuit");
}
});
Ok(Response::Simple(format!(
"ha eine ufgmacht im channel <#{}>",
channel_id
)))
}
async fn setup(
&self,
guild_id: GuildId,
http: Arc<Http>,
mongo_client: Mongo,
room: CourtRoom,
) -> Result<()> {
mongo_client.add_lawsuit(guild_id.into(), self).await?; mongo_client.add_lawsuit(guild_id.into(), self).await?;
mongo_client mongo_client
.set_court_room( .set_court_room(
@ -108,20 +130,19 @@ impl Lawsuit {
Ok(()) Ok(())
} }
assign_role(self.accused, http, guild_id, room.role_id).await?; assign_role(self.accused, &http, guild_id, room.role_id).await?;
if let Some(accused_lawyer) = self.accused_lawyer { if let Some(accused_lawyer) = self.accused_lawyer {
assign_role(accused_lawyer, http, guild_id, room.role_id).await?; assign_role(accused_lawyer, &http, guild_id, room.role_id).await?;
} }
assign_role(self.plaintiff, 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 { if let Some(plaintiff_lawyer) = self.plaintiff_lawyer {
assign_role(plaintiff_lawyer, http, guild_id, room.role_id).await?; assign_role(plaintiff_lawyer, &http, guild_id, room.role_id).await?;
} }
assign_role(self.judge, http, guild_id, room.role_id).await?; assign_role(self.judge, &http, guild_id, room.role_id).await?;
Ok(Response::Simple(format!( info!(?self, "Created lawsuit");
"ha eine ufgmacht im channel <#{}>",
room.channel_id Ok(())
)))
} }
async fn send_process_open_message( async fn send_process_open_message(

View file

@ -101,6 +101,7 @@ pub struct CourtRoom {
pub role_id: SnowflakeId, pub role_id: SnowflakeId,
} }
#[derive(Clone)]
pub struct Mongo { pub struct Mongo {
db: Database, db: Database,
} }