78 lines
2.6 KiB
Rust
78 lines
2.6 KiB
Rust
|
use async_trait::async_trait;
|
||
|
use geoffrey_models::models::locations::Location;
|
||
|
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_string,
|
||
|
};
|
||
|
use crate::bot::commands::{BotCommand, CommandError};
|
||
|
use crate::bot::formatters::display_loc;
|
||
|
use geoffrey_models::models::parameters::edit_params::EditParams;
|
||
|
use serenity::builder::CreateApplicationCommand;
|
||
|
|
||
|
//TODO: Combine edit commands into one class once I figure out why subcommand are not working
|
||
|
pub struct EditPosCommand;
|
||
|
|
||
|
#[async_trait]
|
||
|
impl BotCommand for EditPosCommand {
|
||
|
type ApiParams = EditParams;
|
||
|
type ApiResp = Location;
|
||
|
|
||
|
fn command_name() -> String {
|
||
|
"edit_pos".to_string()
|
||
|
}
|
||
|
|
||
|
fn endpoint_name() -> String {
|
||
|
"edit".to_string()
|
||
|
}
|
||
|
|
||
|
fn request_type() -> Method {
|
||
|
Method::POST
|
||
|
}
|
||
|
|
||
|
fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||
|
command
|
||
|
.name(Self::command_name())
|
||
|
.description("Edit a location's position")
|
||
|
.create_option(|option| {
|
||
|
option
|
||
|
.name("loc_name")
|
||
|
.description("Location to edit")
|
||
|
.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 name = option_to_string(options.get(0), "loc_name")?;
|
||
|
|
||
|
let x = option_to_i64(options.get(1), "x")?;
|
||
|
let y = option_to_i64(options.get(2), "y")?;
|
||
|
let z = option_to_i64(options.get(3), "z")?;
|
||
|
let dim = option_to_dim(options.get(4), "dimension").unwrap_or(Dimension::Overworld);
|
||
|
let position = Position::new(x as i32, y as i32, z as i32, dim);
|
||
|
|
||
|
Ok(Self::ApiParams::new(name, Some(position), None))
|
||
|
}
|
||
|
|
||
|
fn build_response(resp: Self::ApiResp) -> String {
|
||
|
format!(
|
||
|
"**{}** has been updated:\n{}",
|
||
|
resp.name,
|
||
|
display_loc(&resp)
|
||
|
)
|
||
|
}
|
||
|
}
|