cargo config. extracted play functionality. volume can be modified hopefully

This commit is contained in:
jude-lafitteIII 2020-05-19 16:29:57 +01:00
parent 8c6be25f02
commit 32aa189511
2 changed files with 44 additions and 27 deletions

2
.cargo/config Normal file
View File

@ -0,0 +1,2 @@
[build]
target-dir = "/home/jude/.rust_build/soundfx-rs"

View File

@ -28,8 +28,10 @@ use serenity::{
* *
}, },
voice::{ voice::{
AudioSource,
ffmpeg, ffmpeg,
pcm Handler as VoiceHandler,
pcm,
}, },
}; };
@ -47,7 +49,10 @@ use tokio::{
fs::File, fs::File,
io::empty, io::empty,
process::Command, process::Command,
sync::Mutex, sync::{
Mutex,
MutexGuard
},
time, time,
}; };
@ -287,7 +292,7 @@ SELECT *
} }
} }
async fn store_sound_source(&self) -> Result<String, Box<dyn std::error::Error>> { async fn store_sound_source(&self) -> Result<Box<dyn AudioSource>, Box<dyn std::error::Error>> {
let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp")); let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp"));
let path_name = format!("{}/sound-{}", caching_location, self.id); let path_name = format!("{}/sound-{}", caching_location, self.id);
@ -301,7 +306,7 @@ SELECT *
file.write_all(self.src.as_ref()).await?; file.write_all(self.src.as_ref()).await?;
} }
Ok(path_name) Ok(ffmpeg(path_name).await?)
} }
async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> { async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> {
@ -471,7 +476,7 @@ SELECT *
} }
struct GuildData { struct GuildData {
id: u64, pub id: u64,
pub name: Option<String>, pub name: Option<String>,
pub prefix: String, pub prefix: String,
pub volume: u8, pub volume: u8,
@ -629,6 +634,26 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientV
#[command("play")] #[command("play")]
#[aliases("p")] #[aliases("p")]
async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashSet<GuildId>>, pool: MySqlPool)
-> Result<(), Box<dyn std::error::Error>> {
let audio = handler.play_only(sound.store_sound_source().await?);
{
let mut locked = audio.lock().await;
locked.volume(guild.volume as f32 / 100.0);
}
sound.plays += 1;
sound.commit(pool).await?;
voice_guilds.insert(GuildId(guild.id));
Ok(())
}
let guild = match msg.guild(&ctx.cache).await { let guild = match msg.guild(&ctx.cache).await {
Some(guild) => guild, Some(guild) => guild,
@ -661,8 +686,6 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
match sound_res { match sound_res {
Some(sound) => { Some(sound) => {
let fp = sound.store_sound_source().await?;
let voice_manager_lock = ctx.data.read().await let voice_manager_lock = ctx.data.read().await
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data"); .get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data");
@ -671,29 +694,21 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let voice_guilds_lock = ctx.data.read().await let voice_guilds_lock = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data"); .get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let mut voice_guilds = voice_guilds_lock.lock().await; let voice_guilds = voice_guilds_lock.lock().await;
let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap();
match voice_manager.get_mut(guild_id) { match voice_manager.get_mut(guild_id) {
Some(handler) => { Some(handler) => {
// play sound // play sound
handler.play_only(ffmpeg(fp).await?); play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
sound.plays += 1;
sound.commit(pool).await?;
voice_guilds.insert(guild_id);
} }
None => { None => {
// try & join a voice channel // try & join a voice channel
match voice_manager.join(guild_id, user_channel) { match voice_manager.join(guild_id, user_channel) {
Some(handler) => { Some(handler) => {
handler.play_only(ffmpeg(fp).await?); play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
sound.plays += 1;
sound.commit(pool).await?;
voice_guilds.insert(guild_id);
} }
None => { None => {