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

107 lines
3.6 KiB
Rust

use async_trait::async_trait;
use reqwest::Method;
use serenity::builder::CreateApplicationCommand;
use serenity::model::interactions::application_command::{
ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use geoffrey_models::models::locations::Location;
use geoffrey_models::models::parameters::set_portal_params::SetPortalParams;
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use geoffrey_models::models::Portal;
use crate::bot::arg_parse::{option_to_i64, option_to_string};
use crate::bot::commands::BotCommand;
use crate::bot::lang::PLAYER_DOES_NOT_HAVE_MATCHING_LOC;
use crate::context::GeoffreyContext;
use crate::error::BotError;
use serenity::utils::MessageBuilder;
pub struct SetPortalCommand;
#[async_trait]
impl BotCommand for SetPortalCommand {
type ApiParams = SetPortalParams;
type ApiResp = Location;
fn command_name() -> String {
"set_portal".to_string()
}
fn request_type() -> Method {
Method::POST
}
fn custom_err_resp(err: &BotError) -> Option<String> {
match err {
BotError::GeoffreyApi(GeoffreyAPIError::EntryNotFound) => {
Some(PLAYER_DOES_NOT_HAVE_MATCHING_LOC.to_string())
}
_ => None,
}
}
fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(Self::command_name())
.description("Set a portal for a location")
.create_option(|option| {
option
.name("loc_name")
.description("Name of the location")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
.create_option(|option| {
option
.name("x_portal")
.description("X coordinate of the portal in the nether")
.kind(ApplicationCommandOptionType::Integer)
.max_int_value(i32::MAX)
.min_int_value(i32::MIN)
.required(true)
})
.create_option(|option| {
option
.name("z_portal")
.description("Z coordinate of the portal in the nether")
.kind(ApplicationCommandOptionType::Integer)
.max_int_value(i32::MAX)
.min_int_value(i32::MIN)
.required(true)
})
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, BotError> {
let options = command_interaction.data.options;
let loc_name = option_to_string(options.get(0), "name")?;
let x_portal = option_to_i64(options.get(1), "x_portal")?;
let z_portal = option_to_i64(options.get(2), "z_portal")?;
let portal = Portal::new(x_portal as i32, z_portal as i32);
Ok(Self::ApiParams::new(loc_name, portal))
}
fn build_response(_: &GeoffreyContext, resp: Self::ApiResp, _: Self::ApiParams) -> String {
let portal = match resp.portal {
None => return "Portal could not be set, try again!".to_string(),
Some(p) => p,
};
MessageBuilder::new()
.push_bold_safe(&resp.name)
.push(format!(
" has had its portal set to {} {} (x={}, z={})",
portal.direction(),
portal.tunnel_addr(),
portal.x,
portal.z
))
.push_line("")
.build()
}
}