use async_trait::async_trait; use reqwest::Method; use serenity::model::interactions::application_command::{ ApplicationCommandInteraction, ApplicationCommandOptionType, }; use crate::bot::arg_parse::option_to_string; use crate::bot::commands::{BotCommand, CommandError}; use crate::bot::lang::ACCOUNT_LINK_INVALID; use geoffrey_models::models::parameters::register_params::RegisterParameters; use geoffrey_models::models::player::{Player, UserID}; use geoffrey_models::models::response::api_error::GeoffreyAPIError; use serenity::builder::CreateApplicationCommand; pub struct RegisterCommand; #[async_trait] impl BotCommand for RegisterCommand { type ApiParams = RegisterParameters; type ApiResp = Player; fn command_name() -> String { "register".to_string() } fn request_type() -> Method { Method::POST } fn custom_err_resp(err: &CommandError) -> Option { match err { CommandError::GeoffreyApi(GeoffreyAPIError::AccountLinkInvalid) => { Some(ACCOUNT_LINK_INVALID.to_string()) } _ => None, } } fn create_app_command(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { command .name(Self::command_name()) .description("Link your discord account to a geoffrey account") .create_option(|option| { option .name("link_code") .description("Link code give to you by Geoffrey in-game") .kind(ApplicationCommandOptionType::String) .required(true) }) } async fn process_arguments( command_interaction: ApplicationCommandInteraction, ) -> Result { let options = command_interaction.data.options; let link_code = option_to_string(options.get(0), "link_code")?; let discord_user_id = UserID::DiscordUUID { discord_uuid: command_interaction.user.id.0, }; let register = RegisterParameters::new( command_interaction.user.name, discord_user_id, Some(link_code), ); Ok(register) } fn build_response(resp: Self::ApiResp, _: Self::ApiParams) -> String { format!( "**{}**, you have been registered for the Geoffrey bot!", resp.name ) } }