Adding local play and shuffling

This commit is contained in:
hodasemi 2018-11-14 16:43:53 +01:00
parent 2d0a2f6497
commit e928b340dd
7 changed files with 89 additions and 26 deletions

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
/target
target/
**/*.rs.bk
bot/
*.webm

View file

@ -6,6 +6,7 @@ authors = ["hodasemi <michaelh.95@t-online.de>"]
[dependencies]
typemap = "~0.3"
serde_json = "*"
rand = "0.6"
[dependencies.serenity]
default-features = false

View file

@ -1,6 +1,7 @@
extern crate serenity;
extern crate parking_lot;
extern crate rand;
extern crate serde_json;
extern crate typemap;

View file

@ -4,9 +4,12 @@ use serenity;
use serenity::voice::LockedAudio;
use std::cell::RefCell;
use std::fs;
use std::ops::DerefMut;
use std::sync::{Arc, MutexGuard};
use rand::{seq::SliceRandom, thread_rng};
use super::super::prelude::*;
pub struct Play {
@ -66,6 +69,24 @@ impl Play {
manager.join(guild_id, connect_to);
}
fn append_songs(
&self,
ctx: &mut serenity::client::Context,
msg: &serenity::model::channel::Message,
mut source: Vec<Song>,
) {
{
let playlist_mutex = self.media.playlist_mut();
playlist_mutex.borrow_mut().append(&mut source);
}
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
Self::check_join_channel(&manager_lock, msg);
MediaData::start_thread(&self.media, msg.channel_id, &manager_lock);
}
fn handle_http_request(
&self,
ctx: &mut serenity::client::Context,
@ -98,16 +119,7 @@ impl Play {
print_error!(msg.channel_id.say(info));
{
let playlist_mutex = self.media.playlist_mut();
playlist_mutex.borrow_mut().append(&mut source);
}
let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned().unwrap();
Self::check_join_channel(&manager_lock, msg);
MediaData::start_thread(&self.media, msg.channel_id, &manager_lock);
self.append_songs(ctx, msg, source);
}
fn handle_local_request(
@ -117,7 +129,25 @@ impl Play {
) {
print_error!(self.media.reset(ctx, msg));
// TODO: read local file directory and play songs in arbitrary order
let files = fs::read_dir("./").unwrap();
let mut songs = Vec::new();
for file in files {
let os_name = file.unwrap().file_name();
let name = os_name.to_str().unwrap();
if name != "RMusicBot" && name != "bot.conf" {
songs.push(Song {
name: name.to_string(),
});
}
}
let mut rng = thread_rng();
songs.shuffle(&mut rng);
self.append_songs(ctx, msg, songs);
}
}

View file

@ -1,4 +1,5 @@
use serenity;
use serenity::voice::ffmpeg;
use std::sync::Arc;
@ -54,7 +55,18 @@ impl serenity::framework::standard::Command for Skip {
let first = playlist.remove(0);
handler.stop();
*song = Some(handler.play_returning(first.source));
let source = match ffmpeg(first.name.clone()) {
Ok(mpeg) => mpeg,
Err(_) => {
playlist.clear();
*song = None;
return Ok(());
}
};
*song = Some(handler.play_returning(source));
print_error!(
msg.channel_id

View file

@ -1,5 +1,6 @@
use serenity;
use serenity::model::id::ChannelId;
use serenity::voice::ffmpeg;
use serenity::voice::{AudioSource, LockedAudio};
use std::cell::RefCell;
@ -12,7 +13,6 @@ use std::time;
use super::prelude::*;
pub struct Song {
pub source: Box<AudioSource>,
pub name: String,
}
@ -92,6 +92,7 @@ impl MediaData {
let song_lock = media_clone.song_mut();
let mut borrow = song_lock.borrow_mut();
// if there is currently something playing
if let Some(ref mut song) = borrow.deref_mut() {
let song_lock = song.clone();
let audio = song_lock.lock();
@ -120,6 +121,7 @@ impl MediaData {
continue;
}
// nothing is playing
{
let playlist_mutex = media_clone.playlist_mut();
let mut playlist = playlist_mutex.borrow_mut();
@ -132,7 +134,19 @@ impl MediaData {
{
let first = playlist.remove(0);
*borrow = Some(handler.play_returning(first.source));
let source = match ffmpeg(first.name.clone()) {
Ok(mpeg) => mpeg,
Err(_) => {
playlist.clear();
*borrow = None;
handler.stop();
media_clone.reset_thread();
return;
}
};
*borrow = Some(handler.play_returning(source));
print_error!(
channel_id.say(format!("Playing song: {}", first.name))
@ -182,7 +196,17 @@ impl MediaData {
let first = playlist.remove(0);
handler.stop();
*song = handler.play_returning(first.source);
let source = match ffmpeg(first.name.clone()) {
Ok(mpeg) => mpeg,
Err(_) => {
playlist.clear();
return false;
}
};
*song = handler.play_returning(source);
print_error!(channel_id.say(format!("Playing song: {}", first.name)));

View file

@ -1,4 +1,3 @@
use serenity::voice::ffmpeg;
use std::process::{Command, Output, Stdio};
use std::str::from_utf8;
@ -66,17 +65,11 @@ pub fn youtube_dl(uri: &str) -> Result<Vec<Song>, String> {
println!("file: {}", file);
}
let mut ffmpegs = Vec::new();
let mut songs = Vec::new();
for file in files {
match ffmpeg(&file) {
Ok(mpeg) => ffmpegs.push(Song {
source: mpeg,
name: file,
}),
Err(_) => return Err("ffmpeg error".to_string()),
}
songs.push(Song { name: file })
}
Ok(ffmpegs)
Ok(songs)
}