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

87 lines
3.3 KiB
Rust

use async_trait::async_trait;
use geoffrey_models::models::locations::{Location, LocationType};
use geoffrey_models::models::parameters::add_location_params::AddLocationParams;
use geoffrey_models::models::{Dimension, Position};
use reqwest::Method;
use serenity::model::interactions::application_command::{
ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use crate::bot::arg_parse::{
add_dimension_argument, add_x_position_argument, add_y_position_argument,
add_z_position_argument, option_to_dim, option_to_i64, option_to_loc_type, option_to_string,
};
use crate::bot::commands::{BotCommand, CommandError};
use crate::bot::formatters::display_loc;
use serenity::builder::CreateApplicationCommand;
pub struct AddLocationCommand;
#[async_trait]
impl BotCommand for AddLocationCommand {
type ApiParams = AddLocationParams;
type ApiResp = Location;
fn command_name() -> String {
"add_location".to_string()
}
fn request_type() -> Method {
Method::POST
}
fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(Self::command_name())
.description("Add a location to Geoffrey.")
.create_option(|option| {
option
.name("type")
.description("Location type")
.kind(ApplicationCommandOptionType::String)
.required(true)
.add_string_choice(LocationType::Base, LocationType::Base)
.add_string_choice(LocationType::Shop, LocationType::Shop)
.add_string_choice(LocationType::Attraction, LocationType::Attraction)
.add_string_choice(LocationType::Town, LocationType::Town)
.add_string_choice(LocationType::Farm, LocationType::Farm)
.add_string_choice(LocationType::Market, LocationType::Market)
})
.create_option(|option| {
option
.name("name")
.description("Name of the location")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
.create_option(add_x_position_argument)
.create_option(add_y_position_argument)
.create_option(add_z_position_argument)
.create_option(add_dimension_argument)
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, CommandError> {
let options = command_interaction.data.options;
let loc_type = option_to_loc_type(options.get(0), "loc_type")?;
let name = option_to_string(options.get(1), "name")?;
let x = option_to_i64(options.get(2), "x")?;
let y = option_to_i64(options.get(3), "y")?;
let z = option_to_i64(options.get(4), "z")?;
let dim = option_to_dim(options.get(5), "dimension").unwrap_or(Dimension::Overworld);
let position = Position::new(x as i32, y as i32, z as i32, dim);
Ok(Self::ApiParams::new(name, position, loc_type, None))
}
fn build_response(resp: Self::ApiResp) -> String {
format!(
"**{}** has been added to Geoffrey:\n{}",
resp.name,
display_loc(&resp)
)
}
}