/link: create user if one does not exist

main
DaXcess 2022-11-01 10:49:16 +01:00
parent 6da879ee0c
commit 90354c7a60
1 changed files with 41 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use log::error; use log::error;
use reqwest::StatusCode;
use serenity::{ use serenity::{
builder::CreateApplicationCommand, builder::CreateApplicationCommand,
model::prelude::interaction::application_command::ApplicationCommandInteraction, model::prelude::interaction::application_command::ApplicationCommandInteraction,
@ -7,7 +8,7 @@ use serenity::{
use crate::{ use crate::{
bot::commands::{respond_message, CommandOutput}, bot::commands::{respond_message, CommandOutput},
database::Database, database::{Database, DatabaseError},
utils::embed::{EmbedBuilder, Status}, utils::embed::{EmbedBuilder, Status},
}; };
@ -57,6 +58,45 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
return; return;
} }
// Check if user exists, if not, create them
if let Err(why) = database.get_user(command.user.id.to_string()).await {
match why {
DatabaseError::InvalidStatusCode(StatusCode::NOT_FOUND) => {
if let Err(why) = database.create_user(command.user.id.to_string()).await {
error!("Error creating user: {:?}", why);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Something went wrong while trying to link your Spotify account.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
}
_ => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Something went wrong while trying to link your Spotify account.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
}
}
match database match database
.create_user_request(command.user.id.to_string()) .create_user_request(command.user.id.to_string())
.await .await