feat: added current position to nowplaying command
This commit is contained in:
parent
eecb61b9f5
commit
a72f9753a2
5 changed files with 31 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
||||||
use crate::music::{music_queue};
|
use crate::music::music_queue;
|
||||||
use crate::util::embed::Embed;
|
use crate::util::embed::Embed;
|
||||||
|
use futures::{FutureExt, TryFutureExt};
|
||||||
use serenity::all::{CommandInteraction, Context};
|
use serenity::all::{CommandInteraction, Context};
|
||||||
use serenity::builder::{CreateCommand, CreateEmbed};
|
use serenity::builder::{CreateCommand, CreateEmbed};
|
||||||
|
|
||||||
|
@ -24,6 +25,13 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let manager = songbird::get(ctx)
|
let manager = songbird::get(ctx)
|
||||||
.await
|
.await
|
||||||
.expect("Cannot get Songbird")
|
.expect("Cannot get Songbird")
|
||||||
|
@ -35,7 +43,11 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
|
||||||
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
|
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 {
|
pub fn register() -> CreateCommand {
|
||||||
|
|
|
@ -68,7 +68,7 @@ impl EventHandler for Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) {
|
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) {
|
||||||
// FIXME: This does not work, when switching channels
|
// TODO This does not work, when switching channels
|
||||||
if new.channel_id.is_some() {
|
if new.channel_id.is_some() {
|
||||||
return; // User did not leave, ignore
|
return; // User did not leave, ignore
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,6 +89,7 @@ pub async fn attempt_to_queue_song(
|
||||||
.expect("Cannot get head of queue");
|
.expect("Cannot get head of queue");
|
||||||
music_queue::set_now_playing(guild_id, Some(src.clone())).await;
|
music_queue::set_now_playing(guild_id, Some(src.clone())).await;
|
||||||
|
|
||||||
|
|
||||||
let handler_lock = match manager.get(*guild_id) {
|
let handler_lock = match manager.get(*guild_id) {
|
||||||
Some(handler) => handler,
|
Some(handler) => handler,
|
||||||
None => {
|
None => {
|
||||||
|
@ -102,7 +103,8 @@ pub async fn attempt_to_queue_song(
|
||||||
|
|
||||||
// Start playing
|
// Start playing
|
||||||
let mut handler = handler_lock.lock().await;
|
let mut handler = handler_lock.lock().await;
|
||||||
let _ = handler.play_input(src.clone().into()); // TODO: Add event handlers
|
let track_handle = handler.play_input(src.clone().into()); // TODO: Add event handlers
|
||||||
|
music_queue::set_now_playing_track_handle(guild_id, Some(track_handle)).await;
|
||||||
handler.add_global_event(
|
handler.add_global_event(
|
||||||
Event::Track(TrackEvent::End),
|
Event::Track(TrackEvent::End),
|
||||||
music_events::TrackEndNotifier {
|
music_events::TrackEndNotifier {
|
||||||
|
@ -136,7 +138,8 @@ pub async fn play_song(ctx: &Context, guild_id: &GuildId, target: &YoutubeDl) {
|
||||||
|
|
||||||
let mut handler = handler_lock.lock().await;
|
let mut handler = handler_lock.lock().await;
|
||||||
handler.stop(); // Stop playing the current song
|
handler.stop(); // Stop playing the current song
|
||||||
let _ = handler.play_input(target.clone().into()); // TODO: Add event handlers
|
let track_handle = handler.play_input(target.clone().into()); // TODO: Add event handlers
|
||||||
|
music_queue::set_now_playing_track_handle(guild_id, Some(track_handle)).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to skip the song, which is currently playing. Do nothing if there is no next song
|
/// Attempt to skip the song, which is currently playing. Do nothing if there is no next song
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use serenity::all::GuildId;
|
use serenity::all::GuildId;
|
||||||
use songbird::input::YoutubeDl;
|
use songbird::input::YoutubeDl;
|
||||||
|
use songbird::tracks::TrackHandle;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
@ -9,10 +10,10 @@ type MusicQueueItem = Arc<Mutex<MusicQueue>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MusicQueue {
|
pub struct MusicQueue {
|
||||||
// God this sucks. This needs to be reprogrammed properly.
|
|
||||||
pub guild_id: GuildId,
|
pub guild_id: GuildId,
|
||||||
pub queue: VecDeque<YoutubeDl>,
|
pub queue: VecDeque<YoutubeDl>,
|
||||||
pub now_playing: Option<YoutubeDl>,
|
pub now_playing: Option<YoutubeDl>,
|
||||||
|
pub now_playing_track_handle: Option<TrackHandle>
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -28,6 +29,7 @@ async fn get_music_queue(guild_id: &GuildId) -> MusicQueueItem {
|
||||||
guild_id: *guild_id,
|
guild_id: *guild_id,
|
||||||
queue: VecDeque::new(),
|
queue: VecDeque::new(),
|
||||||
now_playing: None,
|
now_playing: None,
|
||||||
|
now_playing_track_handle: None,
|
||||||
})))
|
})))
|
||||||
.clone()
|
.clone()
|
||||||
}
|
}
|
||||||
|
@ -67,6 +69,14 @@ pub async fn get_now_playing(guild_id: &GuildId) -> Option<YoutubeDl> {
|
||||||
with_music_queue(guild_id, |queue| queue.now_playing.to_owned()).await
|
with_music_queue(guild_id, |queue| queue.now_playing.to_owned()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn is_empty(guild_id: &GuildId) -> bool {
|
pub async fn is_empty(guild_id: &GuildId) -> bool {
|
||||||
with_music_queue(guild_id, |queue| queue.queue.is_empty()).await
|
with_music_queue(guild_id, |queue| queue.queue.is_empty()).await
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl Embed {
|
||||||
.url(link)
|
.url(link)
|
||||||
.thumbnail(thumbnail)
|
.thumbnail(thumbnail)
|
||||||
.field("Artist", artist, true)
|
.field("Artist", artist, true)
|
||||||
.field("Duration", format!("{}min {}sec", duration.as_secs() / 58, duration.as_secs() % 59), true)
|
.field("Duration", format!("{}min {}sec", duration.as_secs() / 60, duration.as_secs() % 60), true)
|
||||||
.color(Color::from_rgb(81, 224, 26))
|
.color(Color::from_rgb(81, 224, 26))
|
||||||
.footer(CreateEmbedFooter::new(format!("> {} - {}",current_time, username)))
|
.footer(CreateEmbedFooter::new(format!("> {} - {}",current_time, username)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue