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",
|
"color-eyre",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"mongodb",
|
"mongodb",
|
||||||
|
"nougat",
|
||||||
"serde",
|
"serde",
|
||||||
"serenity",
|
"serenity",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
@ -746,6 +747,22 @@ dependencies = [
|
||||||
"linked-hash-map",
|
"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]]
|
[[package]]
|
||||||
name = "match_cfg"
|
name = "match_cfg"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
@ -865,6 +882,27 @@ dependencies = [
|
||||||
"webpki-roots",
|
"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]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.45"
|
version = "0.1.45"
|
||||||
|
|
@ -966,6 +1004,12 @@ dependencies = [
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "paste"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pbkdf2"
|
name = "pbkdf2"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ tokio = { version = "1.19.2", features = ["full"] }
|
||||||
tracing = "0.1.35"
|
tracing = "0.1.35"
|
||||||
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
|
||||||
serde = { version = "1.0.137", features = ["derive"] }
|
serde = { version = "1.0.137", features = ["derive"] }
|
||||||
|
nougat = "0.1.0"
|
||||||
|
|
||||||
[dependencies.serenity]
|
[dependencies.serenity]
|
||||||
version = "0.11.2"
|
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)
|
.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() {
|
match subcomamnd.name.as_str() {
|
||||||
"create" => {
|
"create" => {
|
||||||
let options = &subcomamnd.options;
|
let options = &subcomamnd.options;
|
||||||
let plaintiff = get_user(options.get(0)).wrap_err("plaintiff")?;
|
let plaintiff = UserOption::get(options.get(0)).wrap_err("plaintiff")?;
|
||||||
let accused = get_user(options.get(1)).wrap_err("accused")?;
|
let accused = UserOption::get(options.get(1)).wrap_err("accused")?;
|
||||||
let reason = get_string(options.get(2)).wrap_err("reason")?;
|
let reason = StringOption::get(options.get(2)).wrap_err("reason")?;
|
||||||
let plaintiff_layer = get_user_optional(options.get(3)).wrap_err("plaintiff_layer")?;
|
let plaintiff_layer =
|
||||||
let accused_layer = get_user_optional(options.get(4)).wrap_err("accused_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 {
|
let mut lawsuit = Lawsuit {
|
||||||
plaintiff: plaintiff.0.id,
|
plaintiff: plaintiff.0.id,
|
||||||
|
|
@ -187,64 +202,74 @@ async fn lawsuit_command_handler(
|
||||||
.wrap_err("success reponse")?;
|
.wrap_err("success reponse")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
"set_category" => Ok(()),
|
||||||
_ => Err(eyre!("Unknown subcommand")),
|
_ => Err(eyre!("Unknown subcommand")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_user(
|
#[nougat::gat]
|
||||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
trait GetOption {
|
||||||
) -> color_eyre::Result<(&User, &Option<PartialMember>)> {
|
type Get<'a>;
|
||||||
let option = get_user_optional(option);
|
|
||||||
match option {
|
|
||||||
Ok(Some(t)) => Ok(t),
|
|
||||||
Ok(None) => Err(eyre!("Expected value!")),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_user_optional(
|
fn extract(
|
||||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
command: &ApplicationCommandInteractionDataOptionValue,
|
||||||
) -> color_eyre::Result<Option<(&User, &Option<PartialMember>)>> {
|
) -> color_eyre::Result<Self::Get<'_>>;
|
||||||
if let Some(option) = option {
|
|
||||||
if let Some(command) = option.resolved.as_ref() {
|
fn get(
|
||||||
if let ApplicationCommandInteractionDataOptionValue::User(user, member) = command {
|
option: Option<&ApplicationCommandInteractionDataOption>,
|
||||||
Ok(Some((user, member)))
|
) -> 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 {
|
} else {
|
||||||
Err(eyre!("Expected user!"))
|
Ok(None)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_string(
|
struct UserOption;
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_string_optional(
|
#[nougat::gat]
|
||||||
option: Option<&ApplicationCommandInteractionDataOption>,
|
impl GetOption for UserOption {
|
||||||
) -> color_eyre::Result<Option<&str>> {
|
type Get<'a> = (&'a User, &'a Option<PartialMember>);
|
||||||
if let Some(option) = option {
|
|
||||||
if let Some(command) = option.resolved.as_ref() {
|
fn extract(
|
||||||
if let ApplicationCommandInteractionDataOptionValue::String(str) = command {
|
command: &ApplicationCommandInteractionDataOptionValue,
|
||||||
Ok(Some(str))
|
) -> crate::Result<Self::Get<'_>> {
|
||||||
} else {
|
if let ApplicationCommandInteractionDataOptionValue::User(user, member) = command {
|
||||||
Err(eyre!("Expected string!"))
|
Ok((user, member))
|
||||||
}
|
|
||||||
} else {
|
} 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