chore: updates and formatting

This commit is contained in:
Miguel da Mota 2024-03-10 21:40:52 +01:00
parent 2f89b9dbb9
commit 72a1fe8af3
10 changed files with 175 additions and 131 deletions

View file

@ -1,5 +1,5 @@
pub mod info;
pub mod now_playing;
pub mod play;
pub mod skip;
pub mod stop;
pub mod now_playing;

View file

@ -21,14 +21,22 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let now_plaing = match music_queue::get_now_playing(&guild_id).await {
Some(ytdl) => ytdl,
None => {
return Embed::create_error_respose(username, "Not playing", "I'm not playing anything!");
return Embed::create_error_respose(
username,
"Not playing",
"I'm not playing anything!",
);
}
};
let now_handle = match music_queue::get_now_playing_track_handle(&guild_id).await {
Some(handle) => handle,
None => {
return Embed::create_error_respose(username, "Cannot get TrackHandle", "The TrackHandle is empty.");
return Embed::create_error_respose(
username,
"Cannot get TrackHandle",
"The TrackHandle is empty.",
);
}
};
@ -40,14 +48,27 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let handler = match manager.get(*guild_id) {
Some(handler) => handler,
None => {
return Embed::create_error_respose(username, "Error", "Error while getting the audio handler.");
return Embed::create_error_respose(
username,
"Error",
"Error while getting the audio handler.",
);
}
};
let position = now_handle.get_info().await.unwrap().position;
Embed::create_yt_playing(now_plaing, username, "Currently playing").await
.field("Position", format!("{}min {}sec", position.as_secs() / 60, position.as_secs() % 60), true)
Embed::create_yt_playing(now_plaing, username, "Currently playing")
.await
.field(
"Position",
format!(
"{}min {}sec",
position.as_secs() / 60,
position.as_secs() % 60
),
true,
)
}
pub fn register() -> CreateCommand {

View file

@ -19,13 +19,21 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
});
if query.is_none() {
return Embed::create_error_respose(username, "400: Bad request", "There is no query provided");
return Embed::create_error_respose(
username,
"400: Bad request",
"There is no query provided",
);
}
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return Embed::create_error_respose(username, "guild_id not found", "Could not find guild id.");
return Embed::create_error_respose(
username,
"guild_id not found",
"Could not find guild id.",
);
}
};

View file

@ -9,7 +9,11 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return Embed::create_error_respose(username, "guild_id not found", "Could not find guild id.");
return Embed::create_error_respose(
username,
"guild_id not found",
"Could not find guild id.",
);
}
};

View file

@ -38,14 +38,14 @@ impl EventHandler for Handler {
if let Interaction::Command(command) = interaction {
let _ = &command.defer(&ctx.http()).await.expect("Cannot defer");
let content = Some(match command.data.name.as_str() {
"info" => commands::info::run(&ctx, &command).await,
"play" => commands::play::run(&ctx, &command).await,
"stop" => commands::stop::run(&ctx, &command).await,
"skip" => commands::skip::run(&ctx, &command).await,
"nowplaying" => commands::now_playing::run(&ctx, &command).await,
_ => respond_with_error(&ctx, &command).await,
});
let content = Some(match command.data.name.as_str() {
"info" => commands::info::run(&ctx, &command).await,
"play" => commands::play::run(&ctx, &command).await,
"stop" => commands::stop::run(&ctx, &command).await,
"skip" => commands::skip::run(&ctx, &command).await,
"nowplaying" => commands::now_playing::run(&ctx, &command).await,
_ => respond_with_error(&ctx, &command).await,
});
if let Some(embed) = content {
let followup = CreateInteractionResponseFollowup::new().embed(embed);
@ -63,7 +63,8 @@ impl EventHandler for Handler {
let _command = Command::create_global_command(&ctx.http, commands::stop::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::play::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::skip::register()).await;
let _command = Command::create_global_command(&ctx.http, commands::now_playing::register()).await;
let _command =
Command::create_global_command(&ctx.http, commands::now_playing::register()).await;
println!("Commands are registered and Rustendo is ready for Freddy.");
}
@ -94,7 +95,11 @@ impl EventHandler for Handler {
}
pub async fn respond_with_error(_ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
Embed::create_error_respose(command.user.name.as_str(), "Command not found", "Cannot find the executed command")
Embed::create_error_respose(
command.user.name.as_str(),
"Command not found",
"Cannot find the executed command",
)
}
#[tokio::main]

View file

@ -1,3 +1,3 @@
pub mod music_manager;
pub mod music_events;
pub mod music_queue;
pub mod music_manager;
pub mod music_queue;

View file

@ -89,7 +89,6 @@ pub async fn attempt_to_queue_song(
.expect("Cannot get head of queue");
music_queue::set_now_playing(guild_id, Some(src.clone())).await;
let handler_lock = match manager.get(*guild_id) {
Some(handler) => handler,
None => {

View file

@ -12,7 +12,7 @@ type MusicQueueItem = Arc<Mutex<MusicQueue>>;
pub struct MusicQueue {
pub queue: VecDeque<YoutubeDl>,
pub now_playing: Option<YoutubeDl>,
pub now_playing_track_handle: Option<TrackHandle>
pub now_playing_track_handle: Option<TrackHandle>,
}
lazy_static! {
@ -69,11 +69,14 @@ pub async fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
}
pub async fn set_now_playing_track_handle(guild_id: &GuildId, track_handle: Option<TrackHandle>) {
with_music_queue(guild_id, |queue| queue.now_playing_track_handle = track_handle).await
with_music_queue(guild_id, |queue| {
queue.now_playing_track_handle = track_handle
})
.await
}
pub async fn get_now_playing_track_handle(guild_id: &GuildId) -> Option<TrackHandle> {
with_music_queue(guild_id, | queue| queue.now_playing_track_handle.to_owned()).await
with_music_queue(guild_id, |queue| queue.now_playing_track_handle.to_owned()).await
}
pub async fn is_empty(guild_id: &GuildId) -> bool {

View file

@ -11,46 +11,71 @@ impl Embed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.title(title)
.description(desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}", current_time, username)))
.title(title)
.description(desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
pub fn create_error_respose(username: &str, error_title: &str, error_desc: &str) -> CreateEmbed {
pub fn create_error_respose(
username: &str,
error_title: &str,
error_desc: &str,
) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.author(CreateEmbedAuthor::new("Oops, something went wrong."))
.title(error_title)
.description(error_desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}", current_time, username)))
.author(CreateEmbedAuthor::new("Oops, something went wrong."))
.title(error_title)
.description(error_desc)
.color(Color::from_rgb(224, 49, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
pub async fn create_yt_playing(mut src: YoutubeDl, username: &str, show_as_author: &str) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
pub async fn create_yt_playing(
mut src: YoutubeDl,
username: &str,
show_as_author: &str,
) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
// Get metadata
let metadata = src.aux_metadata().await.expect("Cannot get metadata");
let title = metadata.title.unwrap_or("Unknown title".to_string());
let artist = metadata.artist.unwrap_or("Unknown artist".to_string());
let duration = metadata.duration.unwrap_or(Duration::from_millis(0));
let thumbnail = metadata
.thumbnail
.unwrap_or("https://http.cat/images/403.jpg".to_string());
let link = metadata
.source_url
.unwrap_or("https://piped.moonleay.net/403".to_string());
// Get metadata
let metadata = src.aux_metadata().await.expect("Cannot get metadata");
let title = metadata.title.unwrap_or("Unknown title".to_string());
let artist = metadata.artist.unwrap_or("Unknown artist".to_string());
let duration = metadata.duration.unwrap_or(Duration::from_millis(0));
let thumbnail = metadata
.thumbnail
.unwrap_or("https://http.cat/images/403.jpg".to_string());
let link = metadata
.source_url
.unwrap_or("https://piped.moonleay.net/403".to_string());
CreateEmbed::new()
.author(CreateEmbedAuthor::new(show_as_author))
.title(title)
.url(link)
.thumbnail(thumbnail)
.field("Artist", artist, true)
.field("Duration", format!("{}min {}sec", duration.as_secs() / 60, duration.as_secs() % 60), true)
.color(Color::from_rgb(81, 224, 26))
.footer(CreateEmbedFooter::new(format!("> {} - {}",current_time, username)))
CreateEmbed::new()
.author(CreateEmbedAuthor::new(show_as_author))
.title(title)
.url(link)
.thumbnail(thumbnail)
.field("Artist", artist, true)
.field(
"Duration",
format!(
"{}min {}sec",
duration.as_secs() / 60,
duration.as_secs() % 60
),
true,
)
.color(Color::from_rgb(81, 224, 26))
.footer(CreateEmbedFooter::new(format!(
"> {} - {}",
current_time, username
)))
}
}