Added `set_portal` command to bot

+ Added display_portal formatter
main
Joey Hines 2021-12-12 12:41:09 -07:00
parent c45bb592db
commit 2ff4d14e3f
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
7 changed files with 108 additions and 5 deletions

View File

@ -1,5 +1,4 @@
use log::{LevelFilter, SetLoggerError}; use log::{LevelFilter, SetLoggerError};
use serde::{Deserialize, Serialize};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use geoffrey_models::logging::LogLevel; use geoffrey_models::logging::LogLevel;

View File

@ -21,6 +21,7 @@ pub mod add_item;
pub mod add_location; pub mod add_location;
pub mod find; pub mod find;
pub mod selling; pub mod selling;
pub mod set_portal;
#[derive(Debug)] #[derive(Debug)]
pub enum CommandError { pub enum CommandError {

View File

@ -0,0 +1,81 @@
use async_trait::async_trait;
use geoffrey_models::models::locations::Location;
use geoffrey_models::models::Portal;
use reqwest::Method;
use serenity::client::Context;
use serenity::model::interactions::application_command::{
ApplicationCommand, ApplicationCommandInteraction, ApplicationCommandOptionType,
};
use crate::bot::arg_parse::{option_to_i64, option_to_string};
use crate::bot::commands::{BotCommand, CommandError};
use geoffrey_models::models::parameters::set_portal_params::SetPortalParams;
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
}
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("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)
})
})
.await?;
Ok(command)
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, CommandError> {
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(resp: Self::ApiResp) -> String {
format!("{} has been been updated.", resp.name)
}
}

View File

@ -1,5 +1,6 @@
use geoffrey_models::models::locations::Location; use geoffrey_models::models::locations::Location;
use geoffrey_models::models::player::Player; use geoffrey_models::models::player::Player;
use geoffrey_models::models::Portal;
pub fn display_owners(owners: Vec<Player>, limit: usize) -> String { pub fn display_owners(owners: Vec<Player>, limit: usize) -> String {
let mut plural = ""; let mut plural = "";
@ -28,11 +29,21 @@ pub fn display_owners(owners: Vec<Player>, limit: usize) -> String {
) )
} }
pub fn display_portal(portal: Portal) -> String {
format!("Portal: {} {} (x={}, z={})", portal.direction(), portal.tunnel_addr(), portal.x, portal.z)
}
pub fn display_loc(loc: Location) -> String { pub fn display_loc(loc: Location) -> String {
let portal_str = match loc.portal {
None => "".to_string(),
Some(p) => format!("{}, ", display_portal(p))
};
format!( format!(
"**{}**, {}, Owner(s): **{}**", "**{}**, {}, {}{}",
loc.name, loc.name,
loc.position, loc.position,
display_owners(loc.owners, 3) portal_str,
display_owners(loc.owners, 3),
) )
} }

View File

@ -5,6 +5,7 @@ use commands::add_item::AddItemCommand;
use commands::add_location::AddLocationCommand; use commands::add_location::AddLocationCommand;
use commands::find::FindCommand; use commands::find::FindCommand;
use commands::selling::SellingCommand; use commands::selling::SellingCommand;
use commands::set_portal::SetPortalCommand;
use commands::{BotCommand, CommandError}; use commands::{BotCommand, CommandError};
pub mod arg_parse; pub mod arg_parse;
@ -18,6 +19,7 @@ pub async fn create_commands(ctx: &Context) -> Result<Vec<ApplicationCommand>, C
commands.push(SellingCommand::create_app_command(ctx).await?); commands.push(SellingCommand::create_app_command(ctx).await?);
commands.push(AddLocationCommand::create_app_command(ctx).await?); commands.push(AddLocationCommand::create_app_command(ctx).await?);
commands.push(AddItemCommand::create_app_command(ctx).await?); commands.push(AddItemCommand::create_app_command(ctx).await?);
commands.push( SetPortalCommand::create_app_command(ctx).await?);
Ok(commands) Ok(commands)
} }

View File

@ -25,6 +25,7 @@ use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use geoffrey_models::logging::LogLevel; use geoffrey_models::logging::LogLevel;
use crate::logging::init_logging; use crate::logging::init_logging;
use crate::bot::commands::set_portal::SetPortalCommand;
#[derive(Debug, StructOpt, Clone)] #[derive(Debug, StructOpt, Clone)]
#[structopt(name = "GeoffreyBot", about = "Geoffrey Discord Bot")] #[structopt(name = "GeoffreyBot", about = "Geoffrey Discord Bot")]
@ -81,6 +82,7 @@ impl EventHandler for Handler {
AddLocationCommand::command(geoffrey_ctx, user_id, command.clone()).await AddLocationCommand::command(geoffrey_ctx, user_id, command.clone()).await
} }
"add_item" => AddItemCommand::command(geoffrey_ctx, user_id, command.clone()).await, "add_item" => AddItemCommand::command(geoffrey_ctx, user_id, command.clone()).await,
"set_portal" => SetPortalCommand::command(geoffrey_ctx, user_id, command.clone()).await,
_ => "not implemented :(".to_string(), _ => "not implemented :(".to_string(),
}; };

View File

@ -96,8 +96,8 @@ impl Display for Position {
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Portal { pub struct Portal {
x: i32, pub x: i32,
z: i32, pub z: i32,
} }
impl Portal { impl Portal {
@ -118,6 +118,13 @@ impl Portal {
Direction::South Direction::South
} }
} }
pub fn tunnel_addr(&self) -> i32 {
match self.direction() {
Direction::North | Direction::South => self.z.abs(),
Direction::East | Direction::West => self.x.abs()
}
}
} }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]