Geoffrey-rs/geoffrey_bot/src/bot/commands/find.rs

78 lines
2.2 KiB
Rust

use async_trait::async_trait;
use reqwest::Method;
use serenity::builder::CreateApplicationCommand;
use serenity::model::interactions::application_command::{
ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use geoffrey_models::models::locations::Location;
use geoffrey_models::models::parameters::find_params::FindParams;
use crate::bot::arg_parse::option_to_string;
use crate::bot::commands::BotCommand;
use crate::bot::formatters::GeoffreyFormatter;
use crate::context::GeoffreyContext;
use crate::error::BotError;
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
}
fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
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)
})
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, BotError> {
let options = command_interaction.data.options;
let query = option_to_string(options.get(0), "query")?;
Ok(FindParams::new(query))
}
fn build_response(
ctx: &GeoffreyContext,
resp: Self::ApiResp,
params: Self::ApiParams,
) -> String {
if resp.is_empty() {
"No locations match that query. Try better next time, ding dong".to_string()
} else {
let mut formatter = GeoffreyFormatter::new(ctx.settings.clone());
formatter
.push("The following locations match '")
.push(&params.query)
.push("':")
.push_new_line();
for loc in &resp {
formatter.push_loc(loc).push_new_line();
}
formatter.build()
}
}
}