cargo config. extracted play functionality. volume can be modified hopefully
This commit is contained in:
parent
8c6be25f02
commit
32aa189511
2
.cargo/config
Normal file
2
.cargo/config
Normal file
@ -0,0 +1,2 @@
|
||||
[build]
|
||||
target-dir = "/home/jude/.rust_build/soundfx-rs"
|
55
src/main.rs
55
src/main.rs
@ -28,8 +28,10 @@ use serenity::{
|
||||
*
|
||||
},
|
||||
voice::{
|
||||
AudioSource,
|
||||
ffmpeg,
|
||||
pcm
|
||||
Handler as VoiceHandler,
|
||||
pcm,
|
||||
},
|
||||
};
|
||||
|
||||
@ -47,7 +49,10 @@ use tokio::{
|
||||
fs::File,
|
||||
io::empty,
|
||||
process::Command,
|
||||
sync::Mutex,
|
||||
sync::{
|
||||
Mutex,
|
||||
MutexGuard
|
||||
},
|
||||
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 path_name = format!("{}/sound-{}", caching_location, self.id);
|
||||
@ -301,7 +306,7 @@ SELECT *
|
||||
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> {
|
||||
@ -471,7 +476,7 @@ SELECT *
|
||||
}
|
||||
|
||||
struct GuildData {
|
||||
id: u64,
|
||||
pub id: u64,
|
||||
pub name: Option<String>,
|
||||
pub prefix: String,
|
||||
pub volume: u8,
|
||||
@ -629,6 +634,26 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientV
|
||||
#[command("play")]
|
||||
#[aliases("p")]
|
||||
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 {
|
||||
Some(guild) => guild,
|
||||
|
||||
@ -661,8 +686,6 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
|
||||
match sound_res {
|
||||
Some(sound) => {
|
||||
let fp = sound.store_sound_source().await?;
|
||||
|
||||
let voice_manager_lock = ctx.data.read().await
|
||||
.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
|
||||
.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) {
|
||||
Some(handler) => {
|
||||
// play sound
|
||||
handler.play_only(ffmpeg(fp).await?);
|
||||
|
||||
sound.plays += 1;
|
||||
sound.commit(pool).await?;
|
||||
|
||||
voice_guilds.insert(guild_id);
|
||||
play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
// try & join a voice channel
|
||||
match voice_manager.join(guild_id, user_channel) {
|
||||
Some(handler) => {
|
||||
handler.play_only(ffmpeg(fp).await?);
|
||||
|
||||
sound.plays += 1;
|
||||
sound.commit(pool).await?;
|
||||
|
||||
voice_guilds.insert(guild_id);
|
||||
play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
|
Loading…
Reference in New Issue
Block a user