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

75 lines
2.4 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::edit_params::EditParams;
use crate::bot::arg_parse::option_to_string;
use crate::bot::commands::BotCommand;
use crate::context::GeoffreyContext;
use crate::error::BotError;
use serenity::utils::MessageBuilder;
//TODO: Combine edit commands into one class once I figure out why subcommand are not working
pub struct EditNameCommand;
#[async_trait]
impl BotCommand for EditNameCommand {
type ApiParams = EditParams;
type ApiResp = Location;
fn command_name() -> String {
"edit_name".to_string()
}
fn endpoint() -> 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 name")
.create_option(|option| {
option
.name("loc_name")
.description("Location to edit")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
.create_option(|option| {
option
.name("new_name")
.description("New location name")
.kind(ApplicationCommandOptionType::String)
.required(true)
})
}
async fn process_arguments(
command_interaction: ApplicationCommandInteraction,
) -> Result<Self::ApiParams, BotError> {
let options = command_interaction.data.options;
let name = option_to_string(options.get(0), "loc_name")?;
let new_name = option_to_string(options.get(1), "new_name")?;
Ok(Self::ApiParams::new(name, None, Some(new_name)))
}
fn build_response(_: &GeoffreyContext, resp: Self::ApiResp, args: Self::ApiParams) -> String {
MessageBuilder::new()
.push_bold_safe(&args.loc_name)
.push(" has been renamed to ")
.push_bold_safe(&resp.name)
.push(".")
.build()
}
}