Add remove command
This commit is contained in:
parent
ffdfb37f9d
commit
b3f210000c
7 changed files with 70 additions and 2 deletions
|
@ -93,13 +93,15 @@ fn main() {
|
||||||
"help".to_string(),
|
"help".to_string(),
|
||||||
"list".to_string(),
|
"list".to_string(),
|
||||||
"skip".to_string(),
|
"skip".to_string(),
|
||||||
|
"remove".to_string(),
|
||||||
"ip".to_string(),
|
"ip".to_string(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
).cmd("stop", Stop::new(media_data.clone()))
|
).cmd("stop", Stop::new(media_data.clone()))
|
||||||
.cmd("list", List::new(media_data.clone()))
|
.cmd("list", List::new(media_data.clone()))
|
||||||
.cmd("skip", Skip::new(media_data.clone()))
|
.cmd("skip", Skip::new(media_data.clone()))
|
||||||
.cmd("ip", IP::new()),
|
.cmd("ip", IP::new())
|
||||||
|
.cmd("remove", Remove::new(media_data.clone())),
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = client
|
let _ = client
|
||||||
|
|
|
@ -34,7 +34,7 @@ impl serenity::framework::standard::Command for List {
|
||||||
if playlist.len() == 1 { "song" } else { "songs" }
|
if playlist.len() == 1 { "song" } else { "songs" }
|
||||||
);
|
);
|
||||||
|
|
||||||
let max_output = 15;
|
let max_output = 10;
|
||||||
|
|
||||||
for (i, song) in playlist.iter().enumerate() {
|
for (i, song) in playlist.iter().enumerate() {
|
||||||
if i < max_output {
|
if i < max_output {
|
||||||
|
|
|
@ -3,5 +3,6 @@ pub mod ip;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod pause;
|
pub mod pause;
|
||||||
pub mod play;
|
pub mod play;
|
||||||
|
pub mod remove;
|
||||||
pub mod skip;
|
pub mod skip;
|
||||||
pub mod stop;
|
pub mod stop;
|
||||||
|
|
45
src/player/commands/remove.rs
Normal file
45
src/player/commands/remove.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
use serenity;
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::super::prelude::*;
|
||||||
|
|
||||||
|
pub struct Remove {
|
||||||
|
media: Arc<MediaData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Remove {
|
||||||
|
pub fn new(media_data: Arc<MediaData>) -> Remove {
|
||||||
|
Remove { media: media_data }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serenity::framework::standard::Command for Remove {
|
||||||
|
#[allow(unreachable_code, unused_mut)]
|
||||||
|
fn execute(
|
||||||
|
&self,
|
||||||
|
mut _ctx: &mut serenity::client::Context,
|
||||||
|
msg: &serenity::model::channel::Message,
|
||||||
|
mut _args: serenity::framework::standard::Args,
|
||||||
|
) -> ::std::result::Result<(), serenity::framework::standard::CommandError> {
|
||||||
|
let song_name = self.media.name_mut();
|
||||||
|
let name: String = song_name.borrow().clone();
|
||||||
|
|
||||||
|
if !name.is_empty() {
|
||||||
|
match fs::remove_file(&name) {
|
||||||
|
Ok(_) => print_error!(
|
||||||
|
msg.channel_id
|
||||||
|
.say(format!("Remove current song ({}) from local disk", name))
|
||||||
|
),
|
||||||
|
Err(err) => print_error!(msg.channel_id.say(format!(
|
||||||
|
"Error removing file ({}): {:?}",
|
||||||
|
name,
|
||||||
|
err.kind()
|
||||||
|
))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -67,6 +67,8 @@ impl serenity::framework::standard::Command for Skip {
|
||||||
};
|
};
|
||||||
|
|
||||||
*song = Some(handler.play_returning(source));
|
*song = Some(handler.play_returning(source));
|
||||||
|
let song_name = self.media.name_mut();
|
||||||
|
*song_name.borrow_mut() = first.name.clone();
|
||||||
|
|
||||||
print_error!(
|
print_error!(
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub struct Song {
|
||||||
pub struct MediaData {
|
pub struct MediaData {
|
||||||
playlist: Mutex<RefCell<Vec<Song>>>,
|
playlist: Mutex<RefCell<Vec<Song>>>,
|
||||||
current_song: Mutex<RefCell<Option<LockedAudio>>>,
|
current_song: Mutex<RefCell<Option<LockedAudio>>>,
|
||||||
|
song_name: Mutex<RefCell<String>>,
|
||||||
watcher_thread: Mutex<RefCell<Option<JoinHandle<()>>>>,
|
watcher_thread: Mutex<RefCell<Option<JoinHandle<()>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +39,11 @@ impl MediaData {
|
||||||
*song.borrow_mut() = None;
|
*song.borrow_mut() = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let name = self.name_mut();
|
||||||
|
*name.borrow_mut() = String::new();
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
|
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
|
||||||
|
|
||||||
|
@ -68,6 +74,10 @@ impl MediaData {
|
||||||
self.playlist.lock().unwrap()
|
self.playlist.lock().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn name_mut(&self) -> MutexGuard<RefCell<String>> {
|
||||||
|
self.song_name.lock().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset_thread(&self) {
|
pub fn reset_thread(&self) {
|
||||||
let thread_mutex = self.watcher_thread.lock().unwrap();
|
let thread_mutex = self.watcher_thread.lock().unwrap();
|
||||||
*thread_mutex.borrow_mut() = None;
|
*thread_mutex.borrow_mut() = None;
|
||||||
|
@ -99,9 +109,11 @@ impl MediaData {
|
||||||
|
|
||||||
if audio.finished {
|
if audio.finished {
|
||||||
let playlist = media_clone.playlist_mut();
|
let playlist = media_clone.playlist_mut();
|
||||||
|
let song_name = media_clone.name_mut();
|
||||||
|
|
||||||
if !Self::next_song(
|
if !Self::next_song(
|
||||||
song,
|
song,
|
||||||
|
song_name.borrow_mut().deref_mut(),
|
||||||
playlist.borrow_mut().deref_mut(),
|
playlist.borrow_mut().deref_mut(),
|
||||||
channel_id,
|
channel_id,
|
||||||
&manager_clone,
|
&manager_clone,
|
||||||
|
@ -147,6 +159,8 @@ impl MediaData {
|
||||||
};
|
};
|
||||||
|
|
||||||
*borrow = Some(handler.play_returning(source));
|
*borrow = Some(handler.play_returning(source));
|
||||||
|
let song_name = media_clone.name_mut();
|
||||||
|
*song_name.borrow_mut() = first.name.clone();
|
||||||
|
|
||||||
print_error!(
|
print_error!(
|
||||||
channel_id.say(format!("Playing song: {}", first.name))
|
channel_id.say(format!("Playing song: {}", first.name))
|
||||||
|
@ -176,6 +190,7 @@ impl MediaData {
|
||||||
|
|
||||||
fn next_song(
|
fn next_song(
|
||||||
song: &mut LockedAudio,
|
song: &mut LockedAudio,
|
||||||
|
name: &mut String,
|
||||||
playlist: &mut Vec<Song>,
|
playlist: &mut Vec<Song>,
|
||||||
channel_id: ChannelId,
|
channel_id: ChannelId,
|
||||||
manager_lock: &Arc<
|
manager_lock: &Arc<
|
||||||
|
@ -207,6 +222,7 @@ impl MediaData {
|
||||||
};
|
};
|
||||||
|
|
||||||
*song = handler.play_returning(source);
|
*song = handler.play_returning(source);
|
||||||
|
*name = first.name.clone();
|
||||||
|
|
||||||
print_error!(channel_id.say(format!("Playing song: {}", first.name)));
|
print_error!(channel_id.say(format!("Playing song: {}", first.name)));
|
||||||
|
|
||||||
|
@ -223,6 +239,7 @@ impl Default for MediaData {
|
||||||
MediaData {
|
MediaData {
|
||||||
playlist: Mutex::new(RefCell::new(Vec::new())),
|
playlist: Mutex::new(RefCell::new(Vec::new())),
|
||||||
current_song: Mutex::new(RefCell::new(None)),
|
current_song: Mutex::new(RefCell::new(None)),
|
||||||
|
song_name: Mutex::new(RefCell::new(String::new())),
|
||||||
watcher_thread: Mutex::new(RefCell::new(None)),
|
watcher_thread: Mutex::new(RefCell::new(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ pub use super::commands::ip::IP;
|
||||||
pub use super::commands::list::List;
|
pub use super::commands::list::List;
|
||||||
pub use super::commands::pause::Pause;
|
pub use super::commands::pause::Pause;
|
||||||
pub use super::commands::play::Play;
|
pub use super::commands::play::Play;
|
||||||
|
pub use super::commands::remove::Remove;
|
||||||
pub use super::commands::skip::Skip;
|
pub use super::commands::skip::Skip;
|
||||||
pub use super::commands::stop::Stop;
|
pub use super::commands::stop::Stop;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue