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

82 lines
2.5 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::parameters::register_params::RegisterParameters;
use geoffrey_models::models::player::{Player, UserID};
use geoffrey_models::models::response::api_error::GeoffreyAPIError;
use crate::bot::arg_parse::option_to_string;
use crate::bot::commands::BotCommand;
use crate::bot::lang::ACCOUNT_LINK_INVALID;
use crate::context::GeoffreyContext;
use crate::error::BotError;
use serenity::utils::MessageBuilder;
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: &BotError) -> Option<String> {
match err {
BotError::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<Self::ApiParams, BotError> {
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(_: &GeoffreyContext, resp: Self::ApiResp, _: Self::ApiParams) -> String {
MessageBuilder::new()
.push_bold_safe(resp.name)
.push_line(", you have been registered for the Geoffrey bot!")
.build()
}
}