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,22 +72,25 @@ impl Handler {
} }
} }
#[poise::command( pub mod lawsuit {
use super::*;
#[poise::command(
slash_command, slash_command,
subcommands( subcommands(
"lawsuit_create", "create",
"lawsuit_set_category", "set_category",
"lawsuit_close", "close",
"lawsuit_clear" "clear"
) )
)] )]
pub async fn lawsuit(_: crate::Context<'_>) -> Result<()> { pub async fn lawsuit(_: crate::Context<'_>) -> Result<()> {
unreachable!() unreachable!()
} }
/// Einen neuen Gerichtsprozess erstellen /// Einen neuen Gerichtsprozess erstellen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")] #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_create( async fn create(
ctx: crate::Context<'_>, ctx: crate::Context<'_>,
#[description = "Der Kläger"] plaintiff: User, #[description = "Der Kläger"] plaintiff: User,
#[description = "Der Angeklagte"] accused: User, #[description = "Der Angeklagte"] accused: User,
@ -122,7 +98,7 @@ async fn lawsuit_create(
#[description = "Der Grund für die Klage"] reason: String, #[description = "Der Grund für die Klage"] reason: String,
#[description = "Der Anwalt des Klägers"] plaintiff_lawyer: Option<User>, #[description = "Der Anwalt des Klägers"] plaintiff_lawyer: Option<User>,
#[description = "Der Anwalt des Angeklagten"] accused_lawyer: Option<User>, #[description = "Der Anwalt des Angeklagten"] accused_lawyer: Option<User>,
) -> Result<()> { ) -> Result<()> {
lawsuit_create_impl( lawsuit_create_impl(
ctx, ctx,
plaintiff, plaintiff,
@ -134,78 +110,37 @@ async fn lawsuit_create(
) )
.await .await
.wrap_err("lawsuit_create") .wrap_err("lawsuit_create")
} }
/// Die Rolle für Gefangene setzen /// Die Rolle für Gefangene setzen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")] #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_set_category( async fn set_category(
ctx: crate::Context<'_>, ctx: crate::Context<'_>,
#[description = "Die Kategorie"] category: Channel, #[description = "Die Kategorie"] category: Channel,
) -> Result<()> { ) -> Result<()> {
lawsuit_set_category_impl(ctx, category) lawsuit_set_category_impl(ctx, category)
.await .await
.wrap_err("lawsuit_set_category") .wrap_err("lawsuit_set_category")
} }
/// Den Gerichtsprozess abschliessen und ein Urteil fällen /// Den Gerichtsprozess abschliessen und ein Urteil fällen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")] #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_close( async fn close(
ctx: crate::Context<'_>, ctx: crate::Context<'_>,
#[description = "Das Urteil"] verdict: String, #[description = "Das Urteil"] verdict: String,
) -> Result<()> { ) -> Result<()> {
lawsuit_close_impl(ctx, verdict) lawsuit_close_impl(ctx, verdict)
.await .await
.wrap_err("lawsuit_close") .wrap_err("lawsuit_close")
} }
/// Alle Rechtsprozessdaten löschen /// Alle Rechtsprozessdaten löschen
#[poise::command(slash_command, required_permissions = "MANAGE_GUILD")] #[poise::command(slash_command, required_permissions = "MANAGE_GUILD")]
async fn lawsuit_clear(ctx: crate::Context<'_>) -> Result<()> { async fn clear(ctx: crate::Context<'_>) -> Result<()> {
lawsuit_clear_impl(ctx).await.wrap_err("lawsuit_clear") lawsuit_clear_impl(ctx).await.wrap_err("lawsuit_clear")
} }
#[poise::command( async fn lawsuit_create_impl(
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<'_>, ctx: crate::Context<'_>,
plaintiff: User, plaintiff: User,
accused: User, accused: User,
@ -213,7 +148,7 @@ async fn lawsuit_create_impl(
reason: String, reason: String,
plaintiff_lawyer: Option<User>, plaintiff_lawyer: Option<User>,
accused_lawyer: Option<User>, accused_lawyer: Option<User>,
) -> Result<()> { ) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let lawsuit = Lawsuit { let lawsuit = Lawsuit {
@ -243,9 +178,9 @@ async fn lawsuit_create_impl(
ctx.say(response.to_string()).await?; ctx.say(response.to_string()).await?;
Ok(()) Ok(())
} }
async fn lawsuit_set_category_impl(ctx: crate::Context<'_>, category: Channel) -> Result<()> { 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 guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
//let channel = channel //let channel = channel
@ -268,9 +203,9 @@ async fn lawsuit_set_category_impl(ctx: crate::Context<'_>, category: Channel) -
} }
Ok(()) Ok(())
} }
async fn lawsuit_close_impl(ctx: crate::Context<'_>, verdict: String) -> Result<()> { async fn lawsuit_close_impl(ctx: crate::Context<'_>, verdict: String) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let member = ctx.author_member().await.wrap_err("member not found")?; let member = ctx.author_member().await.wrap_err("member not found")?;
@ -336,17 +271,62 @@ async fn lawsuit_close_impl(ctx: crate::Context<'_>, verdict: String) -> Result<
ctx.say("ich han en dir abschlosse").await?; ctx.say("ich han en dir abschlosse").await?;
Ok(()) Ok(())
} }
async fn lawsuit_clear_impl(ctx: crate::Context<'_>) -> Result<()> { async fn lawsuit_clear_impl(ctx: crate::Context<'_>) -> Result<()> {
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
ctx.data().mongo.delete_guild(guild_id.into()).await?; ctx.data().mongo.delete_guild(guild_id.into()).await?;
ctx.say("alles weg").await?; ctx.say("alles weg").await?;
Ok(()) Ok(())
}
} }
async fn prison_set_role_impl(ctx: crate::Context<'_>, role: Role) -> Result<()> { pub mod prison {
use super::*;
#[poise::command(
slash_command,
subcommands("set_role", "arrest", "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 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 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 release(
ctx: crate::Context<'_>,
#[description = "Die Person zum freilassen"] user: User,
) -> Result<()> {
prison_release_impl(ctx, user)
.await
.wrap_err("prison_release")
}
async fn prison_set_role_impl(ctx: crate::Context<'_>, role: Role) -> Result<()> {
ctx.data() ctx.data()
.mongo .mongo
.set_prison_role( .set_prison_role(
@ -358,9 +338,9 @@ async fn prison_set_role_impl(ctx: crate::Context<'_>, role: Role) -> Result<()>
ctx.say("isch gsetzt").await.wrap_err("reply")?; ctx.say("isch gsetzt").await.wrap_err("reply")?;
Ok(()) Ok(())
} }
async fn prison_arrest_impl(ctx: crate::Context<'_>, user: User) -> Result<()> { async fn prison_arrest_impl(ctx: crate::Context<'_>, user: User) -> Result<()> {
let mongo_client = &ctx.data().mongo; let mongo_client = &ctx.data().mongo;
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let http = &ctx.discord().http; let http = &ctx.discord().http;
@ -389,9 +369,9 @@ async fn prison_arrest_impl(ctx: crate::Context<'_>, user: User) -> Result<()> {
.await .await
.wrap_err("add guild member role")?; .wrap_err("add guild member role")?;
Ok(()) Ok(())
} }
async fn prison_release_impl(ctx: crate::Context<'_>, user: User) -> Result<()> { async fn prison_release_impl(ctx: crate::Context<'_>, user: User) -> Result<()> {
let mongo_client = &ctx.data().mongo; let mongo_client = &ctx.data().mongo;
let guild_id = ctx.guild_id().wrap_err("guild_id not found")?; let guild_id = ctx.guild_id().wrap_err("guild_id not found")?;
let http = &ctx.discord().http; let http = &ctx.discord().http;
@ -423,6 +403,7 @@ async fn prison_release_impl(ctx: crate::Context<'_>, user: User) -> Result<()>
ctx.say("d'freiheit wartet").await?; ctx.say("d'freiheit wartet").await?;
Ok(()) 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 })