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

125 lines
5.2 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::client::Context;
use serenity::model::interactions::application_command::{
ApplicationCommand, ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use crate::bot::arg_parse::{option_to_dim, option_to_i64, option_to_loc_type, option_to_string};
use crate::bot::commands::{BotCommand, CommandError};
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
}
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("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(|option| {
option
.name("x")
.description("X coordinate of the shop")
.kind(ApplicationCommandOptionType::Integer)
.required(true)
})
.create_option(|option| {
option
.name("y")
.description("Y coordinate of the shop")
.kind(ApplicationCommandOptionType::Integer)
.required(true)
})
.create_option(|option| {
option
.name("z")
.description("Z coordinate of the shop")
.kind(ApplicationCommandOptionType::Integer)
.required(true)
})
.create_option(|option| {
option
.name("dimension")
.description("Dimension of the shop, default is Overworld")
.kind(ApplicationCommandOptionType::String)
.add_string_choice(Dimension::Overworld, Dimension::Overworld)
.add_string_choice(Dimension::Nether, Dimension::Nether)
.add_string_choice(Dimension::TheEnd, Dimension::TheEnd)
.required(false)
})
.create_option(|option| {
option
.name("portal_x")
.description("X Coordinate of the portal in the nether")
.kind(ApplicationCommandOptionType::Integer)
.required(false)
})
.create_option(|option| {
option
.name("portal_z")
.description("Z Coordinate of the portal in the nether")
.kind(ApplicationCommandOptionType::Integer)
.required(false)
})
})
.await?;
Ok(command)
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, CommandError> {
let options = command_interaction.data.options;
let name = option_to_string(options.get(0), "name")?;
let loc_type = option_to_loc_type(options.get(1), "loc_type")?;
let x = option_to_i64(options.get(2), "x")?;
let _y = option_to_i64(options.get(3), "x")?;
let z = option_to_i64(options.get(4), "x")?;
let dim = option_to_dim(options.get(5), "dimension").unwrap_or(Dimension::Overworld);
let position = Position::new(x 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.", resp.name)
}
}