RMusicBot/src/player/commands/skip.rs

91 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 super::super::prelude::*;
2019-07-12 08:39:03 +00:00
use serenity::prelude::*;
use serenity::{
framework::standard::{macros::command, Args, CommandResult},
model::channel::Message,
};
#[command]
fn skip(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
if !channel_contains_author(ctx, msg) {
println!(
"user {} is not in the same voice channel as the bot",
msg.author.name
);
return Ok(());
2018-11-14 14:29:58 +00:00
}
2019-07-12 12:41:51 +00:00
let mut data = ctx.data.write();
2018-11-14 14:29:58 +00:00
2019-07-12 12:41:51 +00:00
if let Some(manager_lock) = data.get_mut::<VoiceManager>().cloned() {
let mut manager = manager_lock.lock();
2018-11-14 14:29:58 +00:00
2019-07-12 12:41:51 +00:00
let guild_id = check_result_return_ok!(guild_id(ctx, msg));
2019-07-12 08:39:03 +00:00
if let Some(handler) = handler(guild_id, &mut manager) {
2019-07-12 12:41:51 +00:00
let media = match data.get_mut::<Media>() {
Some(media) => media,
None => {
display_error_ok!(msg.channel_id.say(&ctx.http, "could not find media data"));
2019-04-28 12:13:36 +00:00
return Ok(());
}
};
2019-07-12 08:39:03 +00:00
// if current song is the last song in this playlist, just return
2019-07-12 12:41:51 +00:00
if media.playlist().is_empty() {
2019-07-12 08:39:03 +00:00
print_error!(msg
.channel_id
2019-07-12 12:41:51 +00:00
.say(&ctx.http, "playlist is empty, no next song available"));
2019-04-28 12:13:36 +00:00
2019-07-12 08:39:03 +00:00
return Ok(());
} else {
// remove the current song from the playlist
2019-07-12 12:41:51 +00:00
let first = media.playlist_mut().remove(0);
2019-04-28 12:13:36 +00:00
2019-07-12 08:39:03 +00:00
// stop the current song from playing
handler.stop();
// load next song into memory
let source = match ffmpeg(first.name.clone()) {
Ok(mpeg) => mpeg,
Err(_) => {
2019-07-12 12:41:51 +00:00
media.playlist_mut().clear();
*media.song_mut() = None;
2019-07-12 08:39:03 +00:00
return Ok(());
}
};
// start the next song if possible
/*
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)));
}
2019-07-12 08:39:03 +00:00
None => println!("error getting callback from media"),
}
2019-07-12 08:39:03 +00:00
*/
2019-07-12 12:41:51 +00:00
*media.song_mut() = Some(handler.play_returning(source));
2019-07-12 08:39:03 +00:00
*media.song_name_mut() = first.name.clone();
2019-07-12 12:41:51 +00:00
print_error!(msg.channel_id.say(
&ctx.http,
format!("Skipped current song, now playing: {}", first.name)
));
2018-11-14 14:29:58 +00:00
}
}
}
2019-07-12 08:39:03 +00:00
Ok(())
2018-11-14 14:29:58 +00:00
}