Add remove command

This commit is contained in:
hodasemi 2018-11-21 09:58:06 +01:00
parent ffdfb37f9d
commit b3f210000c
7 changed files with 70 additions and 2 deletions

View file

@ -93,13 +93,15 @@ fn main() {
"help".to_string(),
"list".to_string(),
"skip".to_string(),
"remove".to_string(),
"ip".to_string(),
],
),
).cmd("stop", Stop::new(media_data.clone()))
.cmd("list", List::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

View file

@ -34,7 +34,7 @@ impl serenity::framework::standard::Command for List {
if playlist.len() == 1 { "song" } else { "songs" }
);
let max_output = 15;
let max_output = 10;
for (i, song) in playlist.iter().enumerate() {
if i < max_output {

View file

@ -3,5 +3,6 @@ pub mod ip;
pub mod list;
pub mod pause;
pub mod play;
pub mod remove;
pub mod skip;
pub mod stop;

View 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(())
}
}

View file

@ -67,6 +67,8 @@ impl serenity::framework::standard::Command for Skip {
};
*song = Some(handler.play_returning(source));
let song_name = self.media.name_mut();
*song_name.borrow_mut() = first.name.clone();
print_error!(
msg.channel_id

View file

@ -19,6 +19,7 @@ pub struct Song {
pub struct MediaData {
playlist: Mutex<RefCell<Vec<Song>>>,
current_song: Mutex<RefCell<Option<LockedAudio>>>,
song_name: Mutex<RefCell<String>>,
watcher_thread: Mutex<RefCell<Option<JoinHandle<()>>>>,
}
@ -38,6 +39,11 @@ impl MediaData {
*song.borrow_mut() = None;
}
{
let name = self.name_mut();
*name.borrow_mut() = String::new();
}
{
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
@ -68,6 +74,10 @@ impl MediaData {
self.playlist.lock().unwrap()
}
pub fn name_mut(&self) -> MutexGuard<RefCell<String>> {
self.song_name.lock().unwrap()
}
pub fn reset_thread(&self) {
let thread_mutex = self.watcher_thread.lock().unwrap();
*thread_mutex.borrow_mut() = None;
@ -99,9 +109,11 @@ impl MediaData {
if audio.finished {
let playlist = media_clone.playlist_mut();
let song_name = media_clone.name_mut();
if !Self::next_song(
song,
song_name.borrow_mut().deref_mut(),
playlist.borrow_mut().deref_mut(),
channel_id,
&manager_clone,
@ -147,6 +159,8 @@ impl MediaData {
};
*borrow = Some(handler.play_returning(source));
let song_name = media_clone.name_mut();
*song_name.borrow_mut() = first.name.clone();
print_error!(
channel_id.say(format!("Playing song: {}", first.name))
@ -176,6 +190,7 @@ impl MediaData {
fn next_song(
song: &mut LockedAudio,
name: &mut String,
playlist: &mut Vec<Song>,
channel_id: ChannelId,
manager_lock: &Arc<
@ -207,6 +222,7 @@ impl MediaData {
};
*song = handler.play_returning(source);
*name = first.name.clone();
print_error!(channel_id.say(format!("Playing song: {}", first.name)));
@ -223,6 +239,7 @@ impl Default for MediaData {
MediaData {
playlist: Mutex::new(RefCell::new(Vec::new())),
current_song: Mutex::new(RefCell::new(None)),
song_name: Mutex::new(RefCell::new(String::new())),
watcher_thread: Mutex::new(RefCell::new(None)),
}
}

View file

@ -5,6 +5,7 @@ pub use super::commands::ip::IP;
pub use super::commands::list::List;
pub use super::commands::pause::Pause;
pub use super::commands::play::Play;
pub use super::commands::remove::Remove;
pub use super::commands::skip::Skip;
pub use super::commands::stop::Stop;