2022-10-18 22:59:32 +02:00
|
|
|
use log::error;
|
|
|
|
use serenity::{
|
|
|
|
builder::CreateApplicationCommand,
|
2022-10-31 22:46:34 +01:00
|
|
|
model::prelude::interaction::application_command::ApplicationCommandInteraction,
|
2022-10-18 22:59:32 +02:00
|
|
|
prelude::Context,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2022-10-31 22:46:34 +01:00
|
|
|
bot::commands::{respond_message, CommandOutput},
|
2022-10-18 22:59:32 +02:00
|
|
|
database::{Database, DatabaseError},
|
|
|
|
session::manager::SessionManager,
|
2022-10-31 22:46:34 +01:00
|
|
|
utils::embed::{EmbedBuilder, Status},
|
2022-10-18 22:59:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const NAME: &str = "unlink";
|
|
|
|
|
|
|
|
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
|
|
|
|
Box::pin(async move {
|
|
|
|
let data = ctx.data.read().await;
|
|
|
|
let database = data.get::<Database>().unwrap();
|
|
|
|
let session_manager = data.get::<SessionManager>().unwrap();
|
|
|
|
|
|
|
|
// Disconnect session if user has any
|
|
|
|
if let Some(session) = session_manager.find(command.user.id).await {
|
2022-10-31 22:46:34 +01:00
|
|
|
session.disconnect().await;
|
2022-10-18 22:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user exists in the first place
|
|
|
|
if let Err(why) = database
|
|
|
|
.delete_user_account(command.user.id.to_string())
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
if let DatabaseError::InvalidStatusCode(status) = why {
|
|
|
|
if status == 404 {
|
2022-10-31 22:46:34 +01:00
|
|
|
respond_message(
|
|
|
|
&ctx,
|
|
|
|
&command,
|
|
|
|
EmbedBuilder::new()
|
|
|
|
.description("You cannot unlink your Spotify account if you haven't linked one.")
|
|
|
|
.status(Status::Error)
|
|
|
|
.build(),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await;
|
2022-10-18 22:59:32 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error!("Error deleting user account: {:?}", why);
|
|
|
|
|
2022-10-31 22:46:34 +01:00
|
|
|
respond_message(
|
2022-10-18 22:59:32 +02:00
|
|
|
&ctx,
|
|
|
|
&command,
|
2022-10-31 22:46:34 +01:00
|
|
|
EmbedBuilder::new()
|
|
|
|
.description("An unexpected error has occured while trying to unlink your account. Please try again later.")
|
|
|
|
.status(Status::Error)
|
|
|
|
.build(),
|
2022-10-18 22:59:32 +02:00
|
|
|
true,
|
|
|
|
)
|
2022-10-31 22:46:34 +01:00
|
|
|
.await;
|
2022-10-18 22:59:32 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-31 22:46:34 +01:00
|
|
|
respond_message(
|
|
|
|
&ctx,
|
|
|
|
&command,
|
|
|
|
EmbedBuilder::new()
|
|
|
|
.description("Successfully unlinked your Spotify account from Spoticord")
|
|
|
|
.status(Status::Success)
|
|
|
|
.build(),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await;
|
2022-10-18 22:59:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
|
|
|
command
|
|
|
|
.name(NAME)
|
|
|
|
.description("Unlink your Spotify account from Spoticord")
|
|
|
|
}
|