Adding local play and shuffling
This commit is contained in:
parent
2d0a2f6497
commit
e928b340dd
7 changed files with 89 additions and 26 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
||||||
/target
|
target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
bot/
|
||||||
|
|
||||||
*.webm
|
*.webm
|
||||||
|
|
|
@ -6,6 +6,7 @@ authors = ["hodasemi <michaelh.95@t-online.de>"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
typemap = "~0.3"
|
typemap = "~0.3"
|
||||||
serde_json = "*"
|
serde_json = "*"
|
||||||
|
rand = "0.6"
|
||||||
|
|
||||||
[dependencies.serenity]
|
[dependencies.serenity]
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
extern crate serenity;
|
extern crate serenity;
|
||||||
|
|
||||||
extern crate parking_lot;
|
extern crate parking_lot;
|
||||||
|
extern crate rand;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate typemap;
|
extern crate typemap;
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,12 @@ use serenity;
|
||||||
use serenity::voice::LockedAudio;
|
use serenity::voice::LockedAudio;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::fs;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::sync::{Arc, MutexGuard};
|
use std::sync::{Arc, MutexGuard};
|
||||||
|
|
||||||
|
use rand::{seq::SliceRandom, thread_rng};
|
||||||
|
|
||||||
use super::super::prelude::*;
|
use super::super::prelude::*;
|
||||||
|
|
||||||
pub struct Play {
|
pub struct Play {
|
||||||
|
@ -66,6 +69,24 @@ impl Play {
|
||||||
manager.join(guild_id, connect_to);
|
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(
|
fn handle_http_request(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut serenity::client::Context,
|
ctx: &mut serenity::client::Context,
|
||||||
|
@ -98,16 +119,7 @@ impl Play {
|
||||||
|
|
||||||
print_error!(msg.channel_id.say(info));
|
print_error!(msg.channel_id.say(info));
|
||||||
|
|
||||||
{
|
self.append_songs(ctx, msg, source);
|
||||||
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_local_request(
|
fn handle_local_request(
|
||||||
|
@ -117,7 +129,25 @@ impl Play {
|
||||||
) {
|
) {
|
||||||
print_error!(self.media.reset(ctx, msg));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use serenity;
|
use serenity;
|
||||||
|
use serenity::voice::ffmpeg;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -54,7 +55,18 @@ impl serenity::framework::standard::Command for Skip {
|
||||||
let first = playlist.remove(0);
|
let first = playlist.remove(0);
|
||||||
|
|
||||||
handler.stop();
|
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!(
|
print_error!(
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use serenity;
|
use serenity;
|
||||||
use serenity::model::id::ChannelId;
|
use serenity::model::id::ChannelId;
|
||||||
|
use serenity::voice::ffmpeg;
|
||||||
use serenity::voice::{AudioSource, LockedAudio};
|
use serenity::voice::{AudioSource, LockedAudio};
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
@ -12,7 +13,6 @@ use std::time;
|
||||||
use super::prelude::*;
|
use super::prelude::*;
|
||||||
|
|
||||||
pub struct Song {
|
pub struct Song {
|
||||||
pub source: Box<AudioSource>,
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,6 +92,7 @@ impl MediaData {
|
||||||
let song_lock = media_clone.song_mut();
|
let song_lock = media_clone.song_mut();
|
||||||
let mut borrow = song_lock.borrow_mut();
|
let mut borrow = song_lock.borrow_mut();
|
||||||
|
|
||||||
|
// if there is currently something playing
|
||||||
if let Some(ref mut song) = borrow.deref_mut() {
|
if let Some(ref mut song) = borrow.deref_mut() {
|
||||||
let song_lock = song.clone();
|
let song_lock = song.clone();
|
||||||
let audio = song_lock.lock();
|
let audio = song_lock.lock();
|
||||||
|
@ -120,6 +121,7 @@ impl MediaData {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nothing is playing
|
||||||
{
|
{
|
||||||
let playlist_mutex = media_clone.playlist_mut();
|
let playlist_mutex = media_clone.playlist_mut();
|
||||||
let mut playlist = playlist_mutex.borrow_mut();
|
let mut playlist = playlist_mutex.borrow_mut();
|
||||||
|
@ -132,7 +134,19 @@ impl MediaData {
|
||||||
{
|
{
|
||||||
let first = playlist.remove(0);
|
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!(
|
print_error!(
|
||||||
channel_id.say(format!("Playing song: {}", first.name))
|
channel_id.say(format!("Playing song: {}", first.name))
|
||||||
|
@ -182,7 +196,17 @@ impl MediaData {
|
||||||
let first = playlist.remove(0);
|
let first = playlist.remove(0);
|
||||||
|
|
||||||
handler.stop();
|
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)));
|
print_error!(channel_id.say(format!("Playing song: {}", first.name)));
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use serenity::voice::ffmpeg;
|
|
||||||
use std::process::{Command, Output, Stdio};
|
use std::process::{Command, Output, Stdio};
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
|
@ -66,17 +65,11 @@ pub fn youtube_dl(uri: &str) -> Result<Vec<Song>, String> {
|
||||||
println!("file: {}", file);
|
println!("file: {}", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ffmpegs = Vec::new();
|
let mut songs = Vec::new();
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
match ffmpeg(&file) {
|
songs.push(Song { name: file })
|
||||||
Ok(mpeg) => ffmpegs.push(Song {
|
|
||||||
source: mpeg,
|
|
||||||
name: file,
|
|
||||||
}),
|
|
||||||
Err(_) => return Err("ffmpeg error".to_string()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ffmpegs)
|
Ok(songs)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue