chore: refine user and bot creation methods

This commit is contained in:
Paul Makles
2023-08-05 12:06:43 +01:00
parent c9011ac692
commit 9f3c1036d0
6 changed files with 155 additions and 111 deletions

View File

@@ -1,11 +1,8 @@
use nanoid::nanoid;
use revolt_database::{Bot, BotInformation, Database, User};
use revolt_database::{Bot, Database, User};
use revolt_models::v0;
use revolt_result::{create_error, Result};
use rocket::serde::json::Json;
use rocket::State;
use ulid::Ulid;
use validator::Validate;
/// # Create Bot
@@ -18,10 +15,6 @@ pub async fn create_bot(
user: User,
info: Json<v0::DataCreateBot>,
) -> Result<Json<v0::Bot>> {
if user.bot.is_some() {
return Err(create_error!(IsBot));
}
let info = info.into_inner();
info.validate().map_err(|error| {
create_error!(FailedValidation {
@@ -29,32 +22,6 @@ pub async fn create_bot(
})
})?;
// TODO: config
let max_bot_count = 5;
if db.get_number_of_bots_by_user(&user.id).await? >= max_bot_count {
return Err(create_error!(ReachedMaximumBots));
}
let id = Ulid::new().to_string();
let username = User::validate_username(info.name)?;
let bot_user = User {
id: id.clone(),
discriminator: User::find_discriminator(db, &username, None).await?,
username,
bot: Some(BotInformation {
owner: user.id.clone(),
}),
..Default::default()
};
let bot = Bot {
id,
owner: user.id,
token: nanoid!(64),
..Default::default()
};
db.insert_user(&bot_user).await?;
db.insert_bot(&bot).await?;
let bot = Bot::create(db, info.name, &user, None).await?;
Ok(Json(bot.into()))
}