Uhm so yeah here's some c o d e

This commit is contained in:
DaXcess
2022-10-31 22:46:34 +01:00
parent 6a77189343
commit d530b182b3
33 changed files with 1934 additions and 298 deletions
+34
View File
@@ -0,0 +1,34 @@
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::application_command::ApplicationCommandInteraction,
prelude::Context,
};
use crate::{
bot::commands::{respond_message, CommandOutput},
utils::embed::{EmbedBuilder, Status},
};
pub const NAME: &str = "help";
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
Box::pin(async move {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Spoticord Help")
.icon_url("https://spoticord.com/img/logo-standard.webp")
.description(format!("Click **[here](https://spoticord.com/commands)** for a list of commands.\n{}",
"If you need help setting Spoticord up you can check out the **[Documentation](https://spoticord.com/documentation)** page on the Spoticord website.\n\n"))
.status(Status::Info)
.build(),
false,
)
.await;
})
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name(NAME).description("Shows the help message")
}
+58 -62
View File
@@ -1,53 +1,34 @@
use log::error;
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::{
application_command::ApplicationCommandInteraction, InteractionResponseType,
},
model::prelude::interaction::application_command::ApplicationCommandInteraction,
prelude::Context,
Result as SerenityResult,
};
use crate::{bot::commands::CommandOutput, database::Database};
use crate::{
bot::commands::{respond_message, CommandOutput},
database::Database,
utils::embed::{EmbedBuilder, Status},
};
pub const NAME: &str = "link";
async fn respond_message(
ctx: &Context,
command: &ApplicationCommandInteraction,
msg: impl Into<String>,
ephemeral: bool,
) -> SerenityResult<()> {
command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| message.content(msg.into()).ephemeral(ephemeral))
})
.await
}
fn check_msg(result: SerenityResult<()>) {
if let Err(why) = result {
error!("Error sending message: {:?}", why);
}
}
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
Box::pin(async move {
let data = ctx.data.read().await;
let database = data.get::<Database>().unwrap();
if let Ok(_) = database.get_user_account(command.user.id.to_string()).await {
check_msg(
respond_message(
&ctx,
&command,
"You have already linked your Spotify account.",
true,
)
.await,
);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("You have already linked your Spotify account.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
@@ -56,15 +37,22 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
let base = std::env::var("SPOTICORD_ACCOUNTS_URL").unwrap();
let link = format!("{}/spotify/{}", base, request.token);
check_msg(
respond_message(
&ctx,
&command,
format!("Go to the following URL to link your account:\n{}", link),
true,
)
.await,
);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Link your Spotify account")
.title_url(&link)
.icon_url("https://spoticord.com/img/spotify-logo.png")
.description(format!(
"Go to [this link]({}) to connect your Spotify account.",
link
))
.status(Status::Info)
.build(),
true,
)
.await;
return;
}
@@ -77,30 +65,38 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
let base = std::env::var("SPOTICORD_ACCOUNTS_URL").unwrap();
let link = format!("{}/spotify/{}", base, request.token);
check_msg(
respond_message(
&ctx,
&command,
format!("Go to the following URL to link your account:\n{}", link),
true,
)
.await,
);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Link your Spotify account")
.title_url(&link)
.icon_url("https://spoticord.com/img/spotify-logo.png")
.description(format!(
"Go to [this link]({}) to connect your Spotify account.",
link
))
.status(Status::Info)
.build(),
true,
)
.await;
return;
}
Err(why) => {
error!("Error creating user request: {:?}", why);
check_msg(
respond_message(
&ctx,
&command,
"An error occurred while serving your request. Please try again later.",
true,
)
.await,
);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("An error occurred while serving your request. Please try again later.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
+3
View File
@@ -1,2 +1,5 @@
pub mod help;
pub mod link;
pub mod rename;
pub mod unlink;
pub mod version;
+165
View File
@@ -0,0 +1,165 @@
use log::error;
use reqwest::StatusCode;
use serenity::{
builder::CreateApplicationCommand,
model::prelude::{
command::CommandOptionType, interaction::application_command::ApplicationCommandInteraction,
},
prelude::Context,
};
use crate::{
bot::commands::{respond_message, CommandOutput},
database::{Database, DatabaseError},
utils::{
self,
embed::{EmbedBuilder, Status},
},
};
pub const NAME: &str = "rename";
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
Box::pin(async move {
let data = ctx.data.read().await;
let database = data.get::<Database>().unwrap();
// 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 rename your Spoticord device.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
}
_ => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Something went wrong while trying to rename your Spoticord device.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
}
}
let device_name = match command.data.options.get(0) {
Some(option) => match option.value {
Some(ref value) => value.as_str().unwrap().to_string(),
None => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("You need to provide a name for your Spoticord device.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
},
None => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("You need to provide a name for your Spoticord device.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
};
if let Err(why) = database
.update_user_device_name(command.user.id.to_string(), &device_name)
.await
{
if let DatabaseError::InvalidInputBody(_) = why {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description(
"Your device name must not exceed 16 characters and be at least 1 character long.",
)
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
error!("Error updating user device name: {:?}", why);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Something went wrong while trying to rename your Spoticord device.")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description(format!(
"Successfully changed the Spotify device name to **{}**",
utils::discord::escape(device_name)
))
.status(Status::Success)
.build(),
true,
)
.await;
})
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(NAME)
.description("Set a new device name that is displayed in Spotify")
.create_option(|option| {
option
.name("name")
.description("The new device name")
.kind(CommandOptionType::String)
.max_length(16)
.required(true)
})
}
+30 -52
View File
@@ -1,42 +1,19 @@
use log::error;
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::{
application_command::ApplicationCommandInteraction, InteractionResponseType,
},
model::prelude::interaction::application_command::ApplicationCommandInteraction,
prelude::Context,
Result as SerenityResult,
};
use crate::{
bot::commands::CommandOutput,
bot::commands::{respond_message, CommandOutput},
database::{Database, DatabaseError},
session::manager::SessionManager,
utils::embed::{EmbedBuilder, Status},
};
pub const NAME: &str = "unlink";
async fn respond_message(
ctx: &Context,
command: &ApplicationCommandInteraction,
msg: impl Into<String>,
ephemeral: bool,
) -> SerenityResult<()> {
command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| message.content(msg.into()).ephemeral(ephemeral))
})
.await
}
fn check_msg(result: SerenityResult<()>) {
if let Err(why) = result {
error!("Error sending message: {:?}", why);
}
}
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
Box::pin(async move {
let data = ctx.data.read().await;
@@ -45,9 +22,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
// Disconnect session if user has any
if let Some(session) = session_manager.find(command.user.id).await {
if let Err(why) = session.disconnect().await {
error!("Error disconnecting session: {:?}", why);
}
session.disconnect().await;
}
// Check if user exists in the first place
@@ -57,15 +32,16 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
{
if let DatabaseError::InvalidStatusCode(status) = why {
if status == 404 {
check_msg(
respond_message(
&ctx,
&command,
"You cannot unlink your Spotify account if you currently don't have a linked Spotify account.",
true,
)
.await,
);
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;
return;
}
@@ -73,28 +49,30 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
error!("Error deleting user account: {:?}", why);
check_msg(
respond_message(
respond_message(
&ctx,
&command,
"An unexpected error has occured while trying to unlink your account. Please try again later.",
EmbedBuilder::new()
.description("An unexpected error has occured while trying to unlink your account. Please try again later.")
.status(Status::Error)
.build(),
true,
)
.await,
);
.await;
return;
}
check_msg(
respond_message(
&ctx,
&command,
"Successfully unlinked your Spotify account from Spoticord",
true,
)
.await,
);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.description("Successfully unlinked your Spotify account from Spoticord")
.status(Status::Success)
.build(),
true,
)
.await;
})
}
+49
View File
@@ -0,0 +1,49 @@
use log::error;
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::{
application_command::ApplicationCommandInteraction, InteractionResponseType,
},
prelude::Context,
};
use crate::{
bot::commands::CommandOutput,
utils::{consts::VERSION, embed::Status},
};
pub const NAME: &str = "version";
pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutput {
Box::pin(async move {
if let Err(why) = command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| {
message.embed(|embed| {
embed
.title("Spoticord Version")
.author(|author| {
author
.name("Maintained by: RoDaBaFilms")
.url("https://rodabafilms.com/")
.icon_url("https://rodabafilms.com/logo_2021_nobg.png")
})
.description(format!("Current version: {}\n\nSpoticord is open source, check out [our GitHub](https://github.com/SpoticordMusic)", VERSION))
.color(Status::Info as u64)
})
})
})
.await
{
error!("Error sending message: {:?}", why);
}
})
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name(NAME)
.description("Shows the current running version of Spoticord")
}