mirror of
https://github.com/eRgo35/lyra.git
synced 2026-02-04 20:36:10 +01:00
new commands, music fixes and more
This commit is contained in:
@@ -42,9 +42,12 @@ pub async fn play(
|
||||
r"^((?:https?:)\/\/)?((?:www|m)\.)?((?:youtube\.com)).*(youtu.be\/|list=)([^#&?]*).*",
|
||||
)
|
||||
.unwrap();
|
||||
let regex_spotify_playlist = Regex::new(r"https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|playlist)\/|\?uri=spotify:playlist:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)").unwrap();
|
||||
|
||||
let is_playlist = regex_youtube_playlist.is_match(&song).unwrap();
|
||||
let is_spotify = regex_spotify.is_match(&song).unwrap();
|
||||
let is_playlist = regex_youtube_playlist.is_match(&song).unwrap()
|
||||
|| regex_spotify_playlist.is_match(&song).unwrap();
|
||||
let is_spotify =
|
||||
regex_spotify.is_match(&song).unwrap() || regex_spotify_playlist.is_match(&song).unwrap();
|
||||
let is_query = !song.starts_with("http");
|
||||
|
||||
let guild_id = ctx.guild_id().unwrap();
|
||||
@@ -83,6 +86,43 @@ pub async fn play(
|
||||
|
||||
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
|
||||
|
||||
if is_playlist && is_spotify {
|
||||
let raw_list = Command::new("node")
|
||||
.args(["./src/spotify-parser", &song])
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
.stdout;
|
||||
let list = String::from_utf8(raw_list.clone()).expect("Invalid UTF-8");
|
||||
|
||||
let tracks: Vec<String> = list.split("\n").map(str::to_string).collect();
|
||||
|
||||
for (index, url) in tracks.clone().iter().enumerate() {
|
||||
if url.is_empty() {
|
||||
break;
|
||||
}
|
||||
let src = YoutubeDl::new_ytdl_like(
|
||||
"yt-dlp",
|
||||
http_client.clone(),
|
||||
format!("ytsearch:{}", url.to_string()),
|
||||
);
|
||||
let aux_metadata = src.clone().aux_metadata().await.unwrap();
|
||||
let track = handler.enqueue_input(src.clone().into()).await;
|
||||
let _ = track
|
||||
.typemap()
|
||||
.write()
|
||||
.await
|
||||
.insert::<Metadata>(aux_metadata);
|
||||
|
||||
if index == 0 {
|
||||
let embed = generate_playlist_embed(ctx, track, tracks.len()).await;
|
||||
let response = CreateReply::default().embed(embed.unwrap());
|
||||
ctx.send(response).await?;
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if is_playlist {
|
||||
let raw_list = Command::new("yt-dlp")
|
||||
.args(["-j", "--flat-playlist", &song])
|
||||
@@ -98,6 +138,9 @@ pub async fn play(
|
||||
.collect();
|
||||
|
||||
for (index, url) in urls.clone().iter().enumerate() {
|
||||
if url.is_empty() {
|
||||
break;
|
||||
}
|
||||
let src = YoutubeDl::new_ytdl_like("yt-dlp", http_client.clone(), url.to_string());
|
||||
let aux_metadata = src.clone().aux_metadata().await.unwrap();
|
||||
let track = handler.enqueue_input(src.clone().into()).await;
|
||||
@@ -113,37 +156,37 @@ pub async fn play(
|
||||
ctx.send(response).await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if is_spotify {
|
||||
let exec = format!("node ./src/spotify --url {}", song);
|
||||
let query = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(exec)
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
.stdout;
|
||||
let query_str = String::from_utf8(query.clone()).expect("Invalid UTF-8");
|
||||
song = format!("ytsearch:{}", query_str.to_string());
|
||||
}
|
||||
|
||||
if is_query {
|
||||
song = format!("ytsearch:{}", song);
|
||||
}
|
||||
|
||||
let src = YoutubeDl::new_ytdl_like("yt-dlp", http_client, song);
|
||||
let embed = generate_embed(ctx, src.clone(), handler.queue()).await;
|
||||
let response = CreateReply::default().embed(embed.unwrap());
|
||||
ctx.send(response).await?;
|
||||
|
||||
let aux_metadata = src.clone().aux_metadata().await.unwrap();
|
||||
|
||||
let track = handler.enqueue_input(src.clone().into()).await;
|
||||
let _ = track
|
||||
.typemap()
|
||||
.write()
|
||||
.await
|
||||
.insert::<Metadata>(aux_metadata);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if is_spotify {
|
||||
let query = Command::new("node")
|
||||
.args(["./src/spotify-parser", &song])
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
.stdout;
|
||||
let query_str = String::from_utf8(query.clone()).expect("Invalid UTF-8");
|
||||
song = format!("ytsearch:{}", query_str.to_string());
|
||||
}
|
||||
|
||||
if is_query {
|
||||
song = format!("ytsearch:{}", song);
|
||||
}
|
||||
|
||||
let src = YoutubeDl::new_ytdl_like("yt-dlp", http_client, song);
|
||||
let embed = generate_embed(ctx, src.clone(), handler.queue()).await;
|
||||
let response = CreateReply::default().embed(embed.unwrap());
|
||||
ctx.send(response).await?;
|
||||
|
||||
let aux_metadata = src.clone().aux_metadata().await.unwrap();
|
||||
|
||||
let track = handler.enqueue_input(src.clone().into()).await;
|
||||
let _ = track
|
||||
.typemap()
|
||||
.write()
|
||||
.await
|
||||
.insert::<Metadata>(aux_metadata);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -166,7 +209,7 @@ async fn generate_embed(
|
||||
let timestamp = Timestamp::now();
|
||||
let duration_minutes = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() / 60;
|
||||
let duration_seconds = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() % 60;
|
||||
let mut description = format!("Song added to queue @ {}", queue.len());
|
||||
let mut description = format!("Song added to queue @ {}", queue.len() + 1);
|
||||
|
||||
if queue.len() == 0 {
|
||||
description = format!("Playing now!");
|
||||
|
||||
@@ -10,6 +10,8 @@ use serenity::{
|
||||
};
|
||||
use songbird::input::AuxMetadata;
|
||||
|
||||
const QUEUE_DISPLAY_LENGTH: usize = 10;
|
||||
|
||||
/// Shows next tracks in queue; \
|
||||
/// aliases: queue, q
|
||||
#[poise::command(prefix_command, slash_command, aliases("q"), category = "Music")]
|
||||
@@ -25,8 +27,9 @@ pub async fn queue(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let handler = handler_lock.lock().await;
|
||||
let queue = handler.queue();
|
||||
let mut queue_res = String::from("");
|
||||
let mut too_long = false;
|
||||
|
||||
for (index, song) in queue.current_queue().iter().enumerate() {
|
||||
for (index, song) in queue.clone().current_queue().iter().enumerate() {
|
||||
let meta_typemap = song.typemap().read().await;
|
||||
let metadata = meta_typemap.get::<Metadata>().unwrap();
|
||||
let AuxMetadata {
|
||||
@@ -39,8 +42,6 @@ pub async fn queue(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let duration_minutes = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() / 60;
|
||||
let duration_seconds = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() % 60;
|
||||
|
||||
// println!("{:?}", metadata.clone());
|
||||
|
||||
queue_res.push_str(&format!(
|
||||
"{}. {} - {} [{:02}:{:02}] \n",
|
||||
index,
|
||||
@@ -49,6 +50,17 @@ pub async fn queue(ctx: Context<'_>) -> Result<(), Error> {
|
||||
duration_minutes,
|
||||
duration_seconds
|
||||
));
|
||||
|
||||
if index + 1 == QUEUE_DISPLAY_LENGTH {
|
||||
too_long = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if too_long {
|
||||
queue_res.push_str(&format!(
|
||||
"and {} more...",
|
||||
queue.len() - QUEUE_DISPLAY_LENGTH
|
||||
));
|
||||
}
|
||||
|
||||
ctx.send(CreateReply::default().embed(embed(ctx, queue_res).await.unwrap()))
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
use crate::commands::music::metadata::Metadata;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{
|
||||
commands::embeds::{embed, error_embed},
|
||||
Context, Error,
|
||||
};
|
||||
use poise::CreateReply;
|
||||
use serenity::{
|
||||
builder::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter},
|
||||
model::{Colour, Timestamp},
|
||||
};
|
||||
use songbird::{input::AuxMetadata, tracks::TrackHandle};
|
||||
|
||||
/// Skips the currently playing song
|
||||
#[poise::command(prefix_command, slash_command, category = "Music")]
|
||||
@@ -17,21 +25,31 @@ pub async fn skip(ctx: Context<'_>) -> Result<(), Error> {
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
let queue = handler.queue();
|
||||
let _ = queue.skip();
|
||||
let _ = queue.clone().skip();
|
||||
let track_raw = queue.clone().current_queue();
|
||||
let track = track_raw.get(1);
|
||||
let queue_length = queue.len() - 1;
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default().embed(
|
||||
embed(
|
||||
ctx,
|
||||
"Skipped!",
|
||||
"Next song: {song}",
|
||||
&format!("Songs left in queue: {}", queue.len()),
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
let response;
|
||||
|
||||
match track {
|
||||
Some(track) => {
|
||||
response = CreateReply::default().embed(
|
||||
generate_embed(ctx, track.clone(), queue_length)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
None => {
|
||||
response = CreateReply::default().embed(
|
||||
embed(ctx, "Skipped!", "The queue is empty!", "")
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
ctx.send(response).await?;
|
||||
} else {
|
||||
let msg = "I am not in a voice channel!";
|
||||
ctx.send(CreateReply::default().embed(error_embed(ctx, msg).await.unwrap()))
|
||||
@@ -40,3 +58,55 @@ pub async fn skip(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn generate_embed(
|
||||
ctx: Context<'_>,
|
||||
track: TrackHandle,
|
||||
queue_length: usize,
|
||||
) -> Result<CreateEmbed, Error> {
|
||||
let meta_typemap = track.typemap().read().await;
|
||||
let metadata = meta_typemap.get::<Metadata>().unwrap();
|
||||
let AuxMetadata {
|
||||
title,
|
||||
thumbnail,
|
||||
source_url,
|
||||
artist,
|
||||
duration,
|
||||
..
|
||||
} = metadata;
|
||||
let timestamp = Timestamp::now();
|
||||
let duration_minutes = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() / 60;
|
||||
let duration_seconds = duration.unwrap_or(Duration::new(0, 0)).clone().as_secs() % 60;
|
||||
|
||||
let description = format!("Song skipped! Queue length is {}", queue_length);
|
||||
|
||||
let embed = CreateEmbed::default()
|
||||
.author(CreateEmbedAuthor::new("Skipped!").icon_url(ctx.author().clone().face()))
|
||||
.colour(Colour::from_rgb(255, 58, 97))
|
||||
.title(title.as_ref().unwrap())
|
||||
.url(source_url.as_ref().unwrap())
|
||||
.thumbnail(
|
||||
thumbnail
|
||||
.as_ref()
|
||||
.unwrap_or(&ctx.cache().current_user().face()),
|
||||
)
|
||||
.field(
|
||||
"Artist",
|
||||
artist.as_ref().unwrap_or(&"Unknown Artist".to_string()),
|
||||
true,
|
||||
)
|
||||
.field(
|
||||
"Duration",
|
||||
format!("{:02}:{:02}", duration_minutes, duration_seconds),
|
||||
true,
|
||||
)
|
||||
.field("DJ", ctx.author().name.clone(), true)
|
||||
.description(description)
|
||||
.timestamp(timestamp)
|
||||
.footer(
|
||||
CreateEmbedFooter::new(ctx.cache().current_user().name.to_string())
|
||||
.icon_url(ctx.cache().current_user().face()),
|
||||
);
|
||||
|
||||
Ok(embed)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user