mirror of
https://github.com/Noratrieb/discord-court-bot.git
synced 2026-01-14 18:05:02 +01:00
nougat!
This commit is contained in:
parent
e44c292f1f
commit
ac13fa1676
3 changed files with 118 additions and 48 deletions
44
Cargo.lock
generated
44
Cargo.lock
generated
|
|
@ -202,6 +202,7 @@ dependencies = [
|
|||
"color-eyre",
|
||||
"dotenv",
|
||||
"mongodb",
|
||||
"nougat",
|
||||
"serde",
|
||||
"serenity",
|
||||
"tokio",
|
||||
|
|
@ -746,6 +747,22 @@ dependencies = [
|
|||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "macro_rules_attribute"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "422a6e082b3d33461030d68a243af2482eefded078eaaf8ac401777d21cae32b"
|
||||
dependencies = [
|
||||
"macro_rules_attribute-proc_macro",
|
||||
"paste",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "macro_rules_attribute-proc_macro"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb4091d2e7f515b3a7ae8dda39417cd2b81d5c8814f68361c3e15b24e2d40b4a"
|
||||
|
||||
[[package]]
|
||||
name = "match_cfg"
|
||||
version = "0.1.0"
|
||||
|
|
@ -865,6 +882,27 @@ dependencies = [
|
|||
"webpki-roots",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nougat"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8dafa3a476b709b3930ca280fe980fa583a9904735b76045ca328f2c7382ca10"
|
||||
dependencies = [
|
||||
"macro_rules_attribute",
|
||||
"nougat-proc_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nougat-proc_macros"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aad0b7693b89a294713bf27d8664b86a9fee198e44bd683533215e256dc3176"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
|
|
@ -966,6 +1004,12 @@ dependencies = [
|
|||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
|
||||
|
||||
[[package]]
|
||||
name = "pbkdf2"
|
||||
version = "0.10.1"
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ tokio = { version = "1.19.2", features = ["full"] }
|
|||
tracing = "0.1.35"
|
||||
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
|
||||
serde = { version = "1.0.137", features = ["derive"] }
|
||||
nougat = "0.1.0"
|
||||
|
||||
[dependencies.serenity]
|
||||
version = "0.11.2"
|
||||
|
|
|
|||
121
src/handler.rs
121
src/handler.rs
|
|
@ -62,6 +62,19 @@ fn slash_commands(commands: &mut CreateApplicationCommands) -> &mut CreateApplic
|
|||
.required(false)
|
||||
})
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("set_category")
|
||||
.description("Die Gerichtskategorie setzen")
|
||||
.kind(ApplicationCommandOptionType::SubCommand)
|
||||
.create_sub_option(|option| {
|
||||
option
|
||||
.name("category")
|
||||
.description("Die Kategorie")
|
||||
.kind(ApplicationCommandOptionType::Channel)
|
||||
.required(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -150,11 +163,13 @@ async fn lawsuit_command_handler(
|
|||
match subcomamnd.name.as_str() {
|
||||
"create" => {
|
||||
let options = &subcomamnd.options;
|
||||
let plaintiff = get_user(options.get(0)).wrap_err("plaintiff")?;
|
||||
let accused = get_user(options.get(1)).wrap_err("accused")?;
|
||||
let reason = get_string(options.get(2)).wrap_err("reason")?;
|
||||
let plaintiff_layer = get_user_optional(options.get(3)).wrap_err("plaintiff_layer")?;
|
||||
let accused_layer = get_user_optional(options.get(4)).wrap_err("accused_layer")?;
|
||||
let plaintiff = UserOption::get(options.get(0)).wrap_err("plaintiff")?;
|
||||
let accused = UserOption::get(options.get(1)).wrap_err("accused")?;
|
||||
let reason = StringOption::get(options.get(2)).wrap_err("reason")?;
|
||||
let plaintiff_layer =
|
||||
UserOption::get_optional(options.get(3)).wrap_err("plaintiff_layer")?;
|
||||
let accused_layer =
|
||||
UserOption::get_optional(options.get(4)).wrap_err("accused_layer")?;
|
||||
|
||||
let mut lawsuit = Lawsuit {
|
||||
plaintiff: plaintiff.0.id,
|
||||
|
|
@ -187,64 +202,74 @@ async fn lawsuit_command_handler(
|
|||
.wrap_err("success reponse")?;
|
||||
Ok(())
|
||||
}
|
||||
"set_category" => Ok(()),
|
||||
_ => Err(eyre!("Unknown subcommand")),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_user(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<(&User, &Option<PartialMember>)> {
|
||||
let option = get_user_optional(option);
|
||||
match option {
|
||||
Ok(Some(t)) => Ok(t),
|
||||
Ok(None) => Err(eyre!("Expected value!")),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
#[nougat::gat]
|
||||
trait GetOption {
|
||||
type Get<'a>;
|
||||
|
||||
fn get_user_optional(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<Option<(&User, &Option<PartialMember>)>> {
|
||||
if let Some(option) = option {
|
||||
if let Some(command) = option.resolved.as_ref() {
|
||||
if let ApplicationCommandInteractionDataOptionValue::User(user, member) = command {
|
||||
Ok(Some((user, member)))
|
||||
fn extract(
|
||||
command: &ApplicationCommandInteractionDataOptionValue,
|
||||
) -> color_eyre::Result<Self::Get<'_>>;
|
||||
|
||||
fn get(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<Self::Get<'_>> {
|
||||
let option = Self::get_optional(option);
|
||||
match option {
|
||||
Ok(Some(get)) => Ok(get),
|
||||
Ok(None) => Err(eyre!("Expected value!")),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
fn get_optional(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<Option<Self::Get<'_>>> {
|
||||
if let Some(option) = option {
|
||||
if let Some(command) = option.resolved.as_ref() {
|
||||
Self::extract(command).map(Some)
|
||||
} else {
|
||||
Err(eyre!("Expected user!"))
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_string(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<&str> {
|
||||
let option = get_string_optional(option);
|
||||
match option {
|
||||
Ok(Some(t)) => Ok(t),
|
||||
Ok(None) => Err(eyre!("Expected value!")),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
struct UserOption;
|
||||
|
||||
fn get_string_optional(
|
||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||
) -> color_eyre::Result<Option<&str>> {
|
||||
if let Some(option) = option {
|
||||
if let Some(command) = option.resolved.as_ref() {
|
||||
if let ApplicationCommandInteractionDataOptionValue::String(str) = command {
|
||||
Ok(Some(str))
|
||||
} else {
|
||||
Err(eyre!("Expected string!"))
|
||||
}
|
||||
#[nougat::gat]
|
||||
impl GetOption for UserOption {
|
||||
type Get<'a> = (&'a User, &'a Option<PartialMember>);
|
||||
|
||||
fn extract(
|
||||
command: &ApplicationCommandInteractionDataOptionValue,
|
||||
) -> crate::Result<Self::Get<'_>> {
|
||||
if let ApplicationCommandInteractionDataOptionValue::User(user, member) = command {
|
||||
Ok((user, member))
|
||||
} else {
|
||||
Ok(None)
|
||||
Err(eyre!("Expected user!"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct StringOption;
|
||||
|
||||
#[nougat::gat]
|
||||
impl GetOption for StringOption {
|
||||
type Get<'a> = &'a str;
|
||||
|
||||
fn extract(
|
||||
command: &ApplicationCommandInteractionDataOptionValue,
|
||||
) -> crate::Result<Self::Get<'_>> {
|
||||
if let ApplicationCommandInteractionDataOptionValue::String(str) = command {
|
||||
Ok(str)
|
||||
} else {
|
||||
Err(eyre!("Expected string!"))
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue