submodules handlers

This commit is contained in:
nora 2022-06-19 20:27:31 +02:00
parent d9ca14c642
commit 0605eda466
2 changed files with 305 additions and 324 deletions

View file

@ -38,34 +38,7 @@ impl Display for Response {
} }
} }
} }
/*
#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) {
info!(name = %ready.user.name, "Bot is connected!");
if let Some(guild_id) = self.dev_guild_id {
let guild_commands =
GuildId::set_application_commands(&guild_id, &ctx.http, slash_commands).await;
match guild_commands {
Ok(_) => info!("Installed guild slash commands"),
Err(error) => error!(?error, "Failed to create global commands"),
}
}
if self.set_global_commands {
let guild_commands =
ApplicationCommand::set_global_application_commands(&ctx.http, slash_commands)
.await;
match guild_commands {
Ok(commands) => info!(?commands, "Created global commands"),
Err(error) => error!(?error, "Failed to create global commands"),
}
}
}
}
*/
impl Handler { impl Handler {
async fn handle_guild_member_join( async fn handle_guild_member_join(
&self, &self,
@ -99,330 +72,338 @@ impl Handler {
} }
} }
#[poise::command( pub mod lawsuit {
slash_command, use super::*;
subcommands(
"lawsuit_create",
"lawsuit_set_category",
"lawsuit_close",
"lawsuit_clear"
)
)]
pub async fn lawsuit(_: crate::Context<'_>) -> Result<()> {
unreachable!()
}
/// Einen neuen Gerichtsprozess erstellen #[poise::command(
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")] slash_command,
async fn lawsuit_create( subcommands(
ctx: crate::Context<'_>, "create",
#[description = "Der Kläger"] plaintiff: User, "set_category",
#[description = "Der Angeklagte"] accused: User, "close",
#[description = "Der Richter"] judge: User, "clear"
#[description = "Der Grund für die Klage"] reason: String, )
#[description = "Der Anwalt des Klägers"] plaintiff_lawyer: Option<User>, )]
#[description = "Der Anwalt des Angeklagten"] accused_lawyer: Option<User>, pub async fn lawsuit(_: crate::Context<'_>) -> Result<()> {
) -> Result<()> { unreachable!()
lawsuit_create_impl(
ctx,
plaintiff,
accused,
judge,
reason,
plaintiff_lawyer,
accused_lawyer,
)
.await
.wrap_err("lawsuit_create")
}
/// Die Rolle für Gefangene setzen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_set_category(
ctx: crate::Context<'_>,
#[description = "Die Kategorie"] category: Channel,
) -> Result<()> {
lawsuit_set_category_impl(ctx, category)
.await
.wrap_err("lawsuit_set_category")
}
/// Den Gerichtsprozess abschliessen und ein Urteil fällen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_close(
ctx: crate::Context<'_>,
#[description = "Das Urteil"] verdict: String,
) -> Result<()> {
lawsuit_close_impl(ctx, verdict)
.await
.wrap_err("lawsuit_close")
}
/// Alle Rechtsprozessdaten löschen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_clear(ctx: crate::Context<'_>) -> Result<()> {
lawsuit_clear_impl(ctx).await.wrap_err("lawsuit_clear")
}
#[poise::command(
slash_command,
subcommands("prison_set_role", "prison_arrest", "prison_release")
)]
pub async fn prison(_: crate::Context<'_>) -> Result<()> {
unreachable!()
}
/// Die Rolle für Gefangene setzen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn prison_set_role(
ctx: crate::Context<'_>,
#[description = "Die Rolle"] role: Role,
) -> Result<()> {
prison_set_role_impl(ctx, role)
.await
.wrap_err("prison_set_role")
}
/// Jemanden einsperren
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn prison_arrest(
ctx: crate::Context<'_>,
#[description = "Die Person zum einsperren"] user: User,
) -> Result<()> {
prison_arrest_impl(ctx, user)
.await
.wrap_err("prison_arrest")
}
/// Einen Gefangenen freilassen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn prison_release(
ctx: crate::Context<'_>,
#[description = "Die Person zum freilassen"] user: User,
) -> Result<()> {
prison_release_impl(ctx, user)
.await
.wrap_err("prison_release")
}
async fn lawsuit_create_impl(
ctx: crate::Context<'_>,
plaintiff: User,
accused: User,
judge: User,
reason: String,
plaintiff_lawyer: Option<User>,
accused_lawyer: Option<User>,
) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let lawsuit = Lawsuit {
id: Uuid::new(),
plaintiff: plaintiff.id.into(),
accused: accused.id.into(),
judge: judge.id.into(),
plaintiff_lawyer: plaintiff_lawyer.map(|user| user.id.into()),
accused_lawyer: accused_lawyer.map(|user| user.id.into()),
reason: reason.to_owned(),
verdict: None,
court_room: SnowflakeId(0),
};
let lawsuit_ctx = LawsuitCtx {
lawsuit,
mongo_client: ctx.data().mongo.clone(),
http: ctx.discord().http.clone(),
guild_id,
};
let response = lawsuit_ctx
.initialize()
.await
.wrap_err("initialize lawsuit")?;
ctx.say(response.to_string()).await?;
Ok(())
}
async fn lawsuit_set_category_impl(ctx: crate::Context<'_>, category: Channel) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
//let channel = channel
// .id
// .to_channel(&ctx.http)
// .await
// .wrap_err("fetch category for set_category")?;
match category.category() {
Some(category) => {
let id = category.id;
ctx.data()
.mongo
.set_court_category(guild_id.into(), id.into())
.await?;
ctx.say("isch gsetzt").await?;
}
None => {
ctx.say("Das ist keine Kategorie!").await?;
}
} }
Ok(()) /// Einen neuen Gerichtsprozess erstellen
} #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn create(
async fn lawsuit_close_impl(ctx: crate::Context<'_>, verdict: String) -> Result<()> { ctx: crate::Context<'_>,
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; #[description = "Der Kläger"] plaintiff: User,
#[description = "Der Angeklagte"] accused: User,
let member = ctx.author_member().await.wrap_err("member not found")?; #[description = "Der Richter"] judge: User,
let permission_override = member #[description = "Der Grund für die Klage"] reason: String,
.permissions #[description = "Der Anwalt des Klägers"] plaintiff_lawyer: Option<User>,
.wrap_err("permissions not found")? #[description = "Der Anwalt des Angeklagten"] accused_lawyer: Option<User>,
.contains(Permissions::MANAGE_GUILD); ) -> Result<()> {
lawsuit_create_impl(
let room_id = ctx.channel_id(); ctx,
let mongo_client = &ctx.data().mongo; plaintiff,
accused,
let state = mongo_client judge,
.find_or_insert_state(guild_id.into()) reason,
.await plaintiff_lawyer,
.wrap_err("find guild for verdict")?; accused_lawyer,
let lawsuit = state
.lawsuits
.iter()
.find(|l| l.court_room == room_id.into() && l.verdict.is_none());
let lawsuit = match lawsuit {
Some(lawsuit) => lawsuit.clone(),
None => {
ctx.say("i dem channel lauft kein aktive prozess!").await?;
return Ok(());
}
};
let room = state
.court_rooms
.iter()
.find(|r| r.channel_id == room_id.into());
let room = match room {
Some(room) => room.clone(),
None => {
ctx.say("i dem channel lauft kein aktive prozess!").await?;
return Ok(());
}
};
let mut lawsuit_ctx = LawsuitCtx {
lawsuit,
mongo_client: mongo_client.clone(),
http: ctx.discord().http.clone(),
guild_id,
};
let response = lawsuit_ctx
.rule_verdict(
permission_override,
member.user.id,
verdict.to_string(),
room,
) )
.await?; .await
.wrap_err("lawsuit_create")
}
/// Die Rolle für Gefangene setzen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn set_category(
ctx: crate::Context<'_>,
#[description = "Die Kategorie"] category: Channel,
) -> Result<()> {
lawsuit_set_category_impl(ctx, category)
.await
.wrap_err("lawsuit_set_category")
}
/// Den Gerichtsprozess abschliessen und ein Urteil fällen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn close(
ctx: crate::Context<'_>,
#[description = "Das Urteil"] verdict: String,
) -> Result<()> {
lawsuit_close_impl(ctx, verdict)
.await
.wrap_err("lawsuit_close")
}
/// Alle Rechtsprozessdaten löschen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn clear(ctx: crate::Context<'_>) -> Result<()> {
lawsuit_clear_impl(ctx).await.wrap_err("lawsuit_clear")
}
async fn lawsuit_create_impl(
ctx: crate::Context<'_>,
plaintiff: User,
accused: User,
judge: User,
reason: String,
plaintiff_lawyer: Option<User>,
accused_lawyer: Option<User>,
) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let lawsuit = Lawsuit {
id: Uuid::new(),
plaintiff: plaintiff.id.into(),
accused: accused.id.into(),
judge: judge.id.into(),
plaintiff_lawyer: plaintiff_lawyer.map(|user| user.id.into()),
accused_lawyer: accused_lawyer.map(|user| user.id.into()),
reason: reason.to_owned(),
verdict: None,
court_room: SnowflakeId(0),
};
let lawsuit_ctx = LawsuitCtx {
lawsuit,
mongo_client: ctx.data().mongo.clone(),
http: ctx.discord().http.clone(),
guild_id,
};
let response = lawsuit_ctx
.initialize()
.await
.wrap_err("initialize lawsuit")?;
if let Err(response) = response {
ctx.say(response.to_string()).await?; ctx.say(response.to_string()).await?;
return Ok(());
Ok(())
} }
ctx.say("ich han en dir abschlosse").await?; async fn lawsuit_set_category_impl(ctx: crate::Context<'_>, category: Channel) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
Ok(()) //let channel = channel
} // .id
// .to_channel(&ctx.http)
// .await
// .wrap_err("fetch category for set_category")?;
match category.category() {
Some(category) => {
let id = category.id;
ctx.data()
.mongo
.set_court_category(guild_id.into(), id.into())
.await?;
ctx.say("isch gsetzt").await?;
}
None => {
ctx.say("Das ist keine Kategorie!").await?;
}
}
async fn lawsuit_clear_impl(ctx: crate::Context<'_>) -> Result<()> { Ok(())
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; }
ctx.data().mongo.delete_guild(guild_id.into()).await?; async fn lawsuit_close_impl(ctx: crate::Context<'_>, verdict: String) -> Result<()> {
ctx.say("alles weg").await?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
Ok(())
}
async fn prison_set_role_impl(ctx: crate::Context<'_>, role: Role) -> Result<()> { let member = ctx.author_member().await.wrap_err("member not found")?;
ctx.data() let permission_override = member
.mongo .permissions
.set_prison_role( .wrap_err("permissions not found")?
ctx.guild_id().wrap_err("guild_id not found")?.into(), .contains(Permissions::MANAGE_GUILD);
role.id.into(),
)
.await?;
ctx.say("isch gsetzt").await.wrap_err("reply")?; let room_id = ctx.channel_id();
let mongo_client = &ctx.data().mongo;
Ok(()) let state = mongo_client
} .find_or_insert_state(guild_id.into())
.await
.wrap_err("find guild for verdict")?;
async fn prison_arrest_impl(ctx: crate::Context<'_>, user: User) -> Result<()> { let lawsuit = state
let mongo_client = &ctx.data().mongo; .lawsuits
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; .iter()
let http = &ctx.discord().http; .find(|l| l.court_room == room_id.into() && l.verdict.is_none());
let state = mongo_client.find_or_insert_state(guild_id.into()).await?; let lawsuit = match lawsuit {
let role = state.prison_role; Some(lawsuit) => lawsuit.clone(),
None => {
ctx.say("i dem channel lauft kein aktive prozess!").await?;
return Ok(());
}
};
let role = match role { let room = state
Some(role) => role, .court_rooms
None => { .iter()
ctx.say("du mosch zerst e rolle setze mit /prison set_role") .find(|r| r.channel_id == room_id.into());
.await?; let room = match room {
Some(room) => room.clone(),
None => {
ctx.say("i dem channel lauft kein aktive prozess!").await?;
return Ok(());
}
};
let mut lawsuit_ctx = LawsuitCtx {
lawsuit,
mongo_client: mongo_client.clone(),
http: ctx.discord().http.clone(),
guild_id,
};
let response = lawsuit_ctx
.rule_verdict(
permission_override,
member.user.id,
verdict.to_string(),
room,
)
.await?;
if let Err(response) = response {
ctx.say(response.to_string()).await?;
return Ok(()); return Ok(());
} }
};
mongo_client ctx.say("ich han en dir abschlosse").await?;
.add_to_prison(guild_id.into(), user.id.into())
.await?;
guild_id Ok(())
.member(http, user.id) }
.await
.wrap_err("fetching guild member")? async fn lawsuit_clear_impl(ctx: crate::Context<'_>) -> Result<()> {
.add_role(http, role) let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
.await
.wrap_err("add guild member role")?; ctx.data().mongo.delete_guild(guild_id.into()).await?;
Ok(()) ctx.say("alles weg").await?;
Ok(())
}
} }
async fn prison_release_impl(ctx: crate::Context<'_>, user: User) -> Result<()> { pub mod prison {
let mongo_client = &ctx.data().mongo; use super::*;
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let http = &ctx.discord().http;
let state = mongo_client.find_or_insert_state(guild_id.into()).await?; #[poise::command(
let role = state.prison_role; slash_command,
subcommands("set_role", "arrest", "release")
)]
pub async fn prison(_: crate::Context<'_>) -> Result<()> {
unreachable!()
}
let role = match role { /// Die Rolle für Gefangene setzen
Some(role) => role, #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
None => { async fn set_role(
ctx.say("du mosch zerst e rolle setze mit /prison set_role") ctx: crate::Context<'_>,
.await?; #[description = "Die Rolle"] role: Role,
return Ok(()); ) -> Result<()> {
} prison_set_role_impl(ctx, role)
}; .await
.wrap_err("prison_set_role")
}
mongo_client /// Jemanden einsperren
.remove_from_prison(guild_id.into(), user.id.into()) #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
.await?; async fn arrest(
ctx: crate::Context<'_>,
#[description = "Die Person zum einsperren"] user: User,
) -> Result<()> {
prison_arrest_impl(ctx, user)
.await
.wrap_err("prison_arrest")
}
guild_id /// Einen Gefangenen freilassen
.member(http, user.id) #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
.await async fn release(
.wrap_err("fetching guild member")? ctx: crate::Context<'_>,
.remove_role(http, role) #[description = "Die Person zum freilassen"] user: User,
.await ) -> Result<()> {
.wrap_err("remove guild member role")?; prison_release_impl(ctx, user)
.await
.wrap_err("prison_release")
}
ctx.say("d'freiheit wartet").await?; async fn prison_set_role_impl(ctx: crate::Context<'_>, role: Role) -> Result<()> {
ctx.data()
.mongo
.set_prison_role(
ctx.guild_id().wrap_err("guild_id not found")?.into(),
role.id.into(),
)
.await?;
Ok(()) ctx.say("isch gsetzt").await.wrap_err("reply")?;
Ok(())
}
async fn prison_arrest_impl(ctx: crate::Context<'_>, user: User) -> Result<()> {
let mongo_client = &ctx.data().mongo;
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let http = &ctx.discord().http;
let state = mongo_client.find_or_insert_state(guild_id.into()).await?;
let role = state.prison_role;
let role = match role {
Some(role) => role,
None => {
ctx.say("du mosch zerst e rolle setze mit /prison set_role")
.await?;
return Ok(());
}
};
mongo_client
.add_to_prison(guild_id.into(), user.id.into())
.await?;
guild_id
.member(http, user.id)
.await
.wrap_err("fetching guild member")?
.add_role(http, role)
.await
.wrap_err("add guild member role")?;
Ok(())
}
async fn prison_release_impl(ctx: crate::Context<'_>, user: User) -> Result<()> {
let mongo_client = &ctx.data().mongo;
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let http = &ctx.discord().http;
let state = mongo_client.find_or_insert_state(guild_id.into()).await?;
let role = state.prison_role;
let role = match role {
Some(role) => role,
None => {
ctx.say("du mosch zerst e rolle setze mit /prison set_role")
.await?;
return Ok(());
}
};
mongo_client
.remove_from_prison(guild_id.into(), user.id.into())
.await?;
guild_id
.member(http, user.id)
.await
.wrap_err("fetching guild member")?
.remove_role(http, role)
.await
.wrap_err("remove guild member role")?;
ctx.say("d'freiheit wartet").await?;
Ok(())
}
} }
pub async fn listener( pub async fn listener(

View file

@ -44,7 +44,7 @@ async fn main() -> Result<()> {
let token = env::var("DISCORD_TOKEN").wrap_err("DISCORD_TOKEN not found in environment")?; let token = env::var("DISCORD_TOKEN").wrap_err("DISCORD_TOKEN not found in environment")?;
let dev_guild_id = if env::var("DEV").is_ok() { let dev_guild_id = if env::var("DEV").is_ok() {
Some(serenity::GuildId( Some(GuildId(
env::var("GUILD_ID") env::var("GUILD_ID")
.wrap_err("GUILD_ID not found in environment, must be set when DEV is set")? .wrap_err("GUILD_ID not found in environment, must be set when DEV is set")?
.parse() .parse()
@ -106,7 +106,7 @@ async fn main() -> Result<()> {
}) })
}) })
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![handler::lawsuit(), handler::prison(), hello()], commands: vec![handler::lawsuit::lawsuit(), handler::prison::prison(), hello()],
on_error: |err| Box::pin(async { handler::error_handler(err).await }), on_error: |err| Box::pin(async { handler::error_handler(err).await }),
listener: |ctx, event, ctx2, data| { listener: |ctx, event, ctx2, data| {
Box::pin(async move { handler::listener(ctx, event, ctx2, data).await }) Box::pin(async move { handler::listener(ctx, event, ctx2, data).await })