175 lines
6.6 KiB
Rust
175 lines
6.6 KiB
Rust
use crate::commands::bot_command::{BotCommand, CommandError};
|
|
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, ApplicationCommandInteractionDataOption,
|
|
ApplicationCommandOptionType,
|
|
};
|
|
use serenity::model::prelude::application_command::ApplicationCommandInteractionDataOptionValue;
|
|
use std::str::FromStr;
|
|
|
|
pub struct AddLocationCommand;
|
|
|
|
fn option_to_i64(option: &ApplicationCommandInteractionDataOption) -> Option<i64> {
|
|
let option = option.resolved.as_ref();
|
|
|
|
if let Some(ApplicationCommandInteractionDataOptionValue::Integer(s)) = option {
|
|
Some(*s)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
#[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 mut name = String::new();
|
|
let mut loc_type = LocationType::Base;
|
|
let x;
|
|
let _y;
|
|
let z;
|
|
let dimension;
|
|
|
|
if let Some(option) = options.get(0) {
|
|
let option = option.resolved.as_ref();
|
|
|
|
if let Some(ApplicationCommandInteractionDataOptionValue::String(s)) = option {
|
|
loc_type = LocationType::from_str(s).unwrap();
|
|
} else {
|
|
return Err(CommandError::ArgumentParse("loc_type".to_string()));
|
|
}
|
|
}
|
|
|
|
if let Some(option) = options.get(1) {
|
|
let option = option.resolved.as_ref();
|
|
|
|
if let Some(ApplicationCommandInteractionDataOptionValue::String(s)) = option {
|
|
name = s.clone();
|
|
} else {
|
|
return Err(CommandError::ArgumentParse("name".to_string()));
|
|
}
|
|
}
|
|
|
|
x = option_to_i64(&options[2]).unwrap() as i32;
|
|
_y = option_to_i64(&options[3]).unwrap() as i32;
|
|
z = option_to_i64(&options[4]).unwrap() as i32;
|
|
|
|
if let Some(option) = options.get(5) {
|
|
let option = option.resolved.as_ref();
|
|
|
|
if let Some(ApplicationCommandInteractionDataOptionValue::String(s)) = option {
|
|
dimension = Dimension::from_str(s.as_str()).unwrap();
|
|
} else {
|
|
return Err(CommandError::ArgumentParse("dimension".to_string()));
|
|
}
|
|
} else {
|
|
dimension = Dimension::default();
|
|
}
|
|
|
|
Ok(Self::ApiParams::new(
|
|
name,
|
|
Position::new(x, z, dimension),
|
|
loc_type,
|
|
None,
|
|
))
|
|
}
|
|
|
|
fn build_response(resp: Self::ApiResp) -> String {
|
|
format!("{} has been added to Geoffrey.", resp.name)
|
|
}
|
|
}
|