spoticord/src/bot/commands/core/unlink.rs

84 lines
2.2 KiB
Rust
Raw Normal View History

2022-10-18 20:59:32 +00:00
use log::error;
use serenity::{
builder::CreateApplicationCommand,
2022-10-31 21:46:34 +00:00
model::prelude::interaction::application_command::ApplicationCommandInteraction,
2022-10-18 20:59:32 +00:00
prelude::Context,
};
use crate::{
2022-10-31 21:46:34 +00:00
bot::commands::{respond_message, CommandOutput},
2022-10-18 20:59:32 +00:00
database::{Database, DatabaseError},
session::manager::SessionManager,
2022-10-31 21:46:34 +00:00
utils::embed::{EmbedBuilder, Status},
2022-10-18 20:59:32 +00:00
};
pub const NAME: &str = "unlink";
2023-02-21 13:45:59 +00:00
pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
2022-10-18 20:59:32 +00:00
Box::pin(async move {
let data = ctx.data.read().await;
let database = data.get::<Database>().expect("to contain a value");
let session_manager = data.get::<SessionManager>().expect("to contain a value");
2022-10-18 20:59:32 +00:00
// Disconnect session if user has any
if let Some(session) = session_manager.find(command.user.id).await {
2022-10-31 21:46:34 +00:00
session.disconnect().await;
2022-10-18 20:59:32 +00: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 21:46:34 +00: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 20:59:32 +00:00
return;
}
}
error!("Error deleting user account: {:?}", why);
2022-10-31 21:46:34 +00:00
respond_message(
2022-10-18 20:59:32 +00:00
&ctx,
&command,
2022-10-31 21:46:34 +00: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 20:59:32 +00:00
true,
)
2022-10-31 21:46:34 +00:00
.await;
2022-10-18 20:59:32 +00:00
return;
}
2022-10-31 21:46:34 +00:00
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Successfully unlinked your Spotify account from Spoticord")
.status(Status::Success)
.build(),
true,
)
.await;
2022-10-18 20:59:32 +00:00
})
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(NAME)
.description("Unlink your Spotify account from Spoticord")
}