75 lines
2.4 KiB
Rust
75 lines
2.4 KiB
Rust
use crate::commands::bot_command::{BotCommand, CommandError};
|
|
use async_trait::async_trait;
|
|
use geoffrey_models::models::locations::Location;
|
|
use geoffrey_models::models::parameters::find_params::FindParams;
|
|
use reqwest::Method;
|
|
use serenity::client::Context;
|
|
use serenity::model::interactions::application_command::{
|
|
ApplicationCommand, ApplicationCommandInteraction, ApplicationCommandOptionType,
|
|
};
|
|
use serenity::model::prelude::application_command::ApplicationCommandInteractionDataOptionValue;
|
|
use std::fmt::Write;
|
|
|
|
pub struct FindCommand;
|
|
|
|
#[async_trait]
|
|
impl BotCommand for FindCommand {
|
|
type ApiParams = FindParams;
|
|
type ApiResp = Vec<Location>;
|
|
|
|
fn command_name() -> String {
|
|
"find".to_string()
|
|
}
|
|
|
|
fn request_type() -> Method {
|
|
Method::GET
|
|
}
|
|
|
|
async fn create_app_command(ctx: &Context) -> Result<ApplicationCommand, CommandError> {
|
|
let command = ApplicationCommand::create_global_application_command(&ctx.http, |command| {
|
|
command
|
|
.name(Self::command_name())
|
|
.description("Find a location in Geoffrey.")
|
|
.create_option(|option| {
|
|
option
|
|
.name("query")
|
|
.description("The location name or player to lookup")
|
|
.kind(ApplicationCommandOptionType::String)
|
|
.required(true)
|
|
})
|
|
})
|
|
.await?;
|
|
|
|
Ok(command)
|
|
}
|
|
|
|
async fn process_arguments(
|
|
command_interaction: ApplicationCommandInteraction,
|
|
) -> Result<Self::ApiParams, CommandError> {
|
|
let options = command_interaction.data.options;
|
|
|
|
if let Some(option) = options.get(0) {
|
|
let query = option.resolved.as_ref();
|
|
|
|
if let Some(ApplicationCommandInteractionDataOptionValue::String(s)) = query {
|
|
return Ok(FindParams::new(s.clone()));
|
|
}
|
|
}
|
|
|
|
Err(CommandError::ArgumentParse("query".to_string()))
|
|
}
|
|
|
|
fn build_response(resp: Self::ApiResp) -> String {
|
|
if resp.is_empty() {
|
|
"No locations match that query, try better next time ding dong".to_string()
|
|
} else {
|
|
let mut resp_str = String::new();
|
|
writeln!(resp_str, "The following locations match:").unwrap();
|
|
for loc in resp {
|
|
writeln!(resp_str, "**{}**, {}", loc.name, loc.position).unwrap();
|
|
}
|
|
resp_str
|
|
}
|
|
}
|
|
}
|