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

67 lines
2.0 KiB
Rust

use async_trait::async_trait;
use reqwest::Method;
use serenity::model::interactions::application_command::{
ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use geoffrey_models::models::locations::Location;
use crate::bot::arg_parse::option_to_string;
use crate::bot::commands::{BotCommand, CommandError};
use crate::bot::formatters::display_loc_full;
use crate::bot::lang::NO_LOCATION_FOUND;
use geoffrey_models::models::parameters::info_params::InfoParams;
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use serenity::builder::CreateApplicationCommand;
pub struct InfoCommand;
#[async_trait]
impl BotCommand for InfoCommand {
type ApiParams = InfoParams;
type ApiResp = Location;
fn command_name() -> String {
"info".to_string()
}
fn request_type() -> Method {
Method::GET
}
fn custom_err_resp(err: &CommandError) -> Option<String> {
match err {
CommandError::GeoffreyApi(GeoffreyAPIError::EntryNotFound) => {
Some(NO_LOCATION_FOUND.to_string())
}
_ => None,
}
}
fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(Self::command_name())
.description("Get info on a location in Geoffrey.")
.create_option(|option| {
option
.name("loc_name")
.description("The location to get info on")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, CommandError> {
let options = command_interaction.data.options;
let location_name = option_to_string(options.get(0), "loc_name")?;
Ok(Self::ApiParams::new(location_name))
}
fn build_response(resp: Self::ApiResp) -> String {
display_loc_full(&resp)
}
}