RMusicBot/src/player/commands/skip.rs

95 lines
3 KiB
Rust
Raw Normal View History

2018-11-14 14:29:58 +00:00
use serenity;
2018-11-14 15:43:53 +00:00
use serenity::voice::ffmpeg;
2018-11-14 14:29:58 +00:00
use std::sync::Arc;
use super::super::prelude::*;
pub struct Skip {
media: Arc<MediaData>,
}
impl Skip {
pub fn new(media_data: Arc<MediaData>) -> Skip {
Skip { media: media_data }
}
}
impl serenity::framework::standard::Command for Skip {
#[allow(unreachable_code, unused_mut)]
fn execute(
&self,
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
_: serenity::framework::standard::Args,
) -> ::std::result::Result<(), serenity::framework::standard::CommandError> {
2018-11-23 12:18:07 +00:00
if !channel_contains_author(ctx, msg) {
println!(
"user {} is not in the same voice channel as the bot",
msg.author.name
);
return Ok(());
}
2019-04-28 12:13:36 +00:00
if let Some(mut manager_lock) = ctx.data.lock().get::<VoiceManager>().cloned() {
let mut manager = manager_lock.lock();
2018-11-14 14:29:58 +00:00
2019-04-28 12:13:36 +00:00
let guild_id = match guild_id(msg.channel_id) {
Some(id) => id,
None => {
println!("error getting guild id");
print_error!(msg.channel_id.say("error getting guild id"));
2018-11-14 14:29:58 +00:00
2019-04-28 12:13:36 +00:00
return Ok(());
}
};
if let Some(handler) = handler(guild_id, &mut manager) {
let playlist_mutex = self.media.playlist_mut()?;
let mut playlist = playlist_mutex.borrow_mut();
let song_mutex = self.media.song_mut()?;
let mut song = song_mutex.borrow_mut();
if playlist.is_empty() {
print_error!(msg
.channel_id
.say("playlist is empty, no next song available"));
return Ok(());
} else {
let first = playlist.remove(0);
handler.stop();
let source = match ffmpeg(first.name.clone()) {
Ok(mpeg) => mpeg,
Err(_) => {
playlist.clear();
*song = None;
return Ok(());
}
};
match self.media.next_callback.borrow().as_ref() {
Some(callback) => {
*song =
Some(handler.play_returning_and_callback(source, callback.clone()));
let song_name = self.media.name_mut()?;
*song_name.borrow_mut() = first.name.clone();
print_error!(msg
.channel_id
.say(format!("Skipped current song, now playing: {}", first.name)));
}
None => println!("error getting callback from media"),
}
}
2018-11-14 14:29:58 +00:00
}
}
Ok(())
}
}