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"
|
69
src/main.rs
69
src/main.rs
@ -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,10 +306,10 @@ 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> {
|
||||||
let c = sqlx::query!(
|
let c = sqlx::query!(
|
||||||
"
|
"
|
||||||
SELECT COUNT(1) as count
|
SELECT COUNT(1) as count
|
||||||
@ -425,7 +430,7 @@ DELETE
|
|||||||
match sqlx::query!(
|
match sqlx::query!(
|
||||||
"
|
"
|
||||||
INSERT INTO sounds (name, server_id, uploader_id, public, src)
|
INSERT INTO sounds (name, server_id, uploader_id, public, src)
|
||||||
VALUES (?, ?, ?, 1, ?)
|
VALUES (?, ?, ?, 1, ?)
|
||||||
",
|
",
|
||||||
name, server_id, user_id, data
|
name, server_id, user_id, data
|
||||||
)
|
)
|
||||||
@ -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,
|
||||||
@ -483,8 +488,8 @@ impl GuildData {
|
|||||||
GuildData,
|
GuildData,
|
||||||
"
|
"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM servers
|
FROM servers
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
", guild_id
|
", guild_id
|
||||||
)
|
)
|
||||||
.fetch_one(&db_pool)
|
.fetch_one(&db_pool)
|
||||||
@ -501,7 +506,7 @@ WHERE id = ?
|
|||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"
|
"
|
||||||
INSERT INTO servers (id, name)
|
INSERT INTO servers (id, name)
|
||||||
VALUES (?, ?)
|
VALUES (?, ?)
|
||||||
", guild.id.as_u64(), guild.name
|
", guild.id.as_u64(), guild.name
|
||||||
)
|
)
|
||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
@ -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 => {
|
||||||
@ -1135,7 +1150,7 @@ async fn show_popular_sounds(ctx: &Context, msg: &Message, _args: Args) -> Comma
|
|||||||
Sound,
|
Sound,
|
||||||
"
|
"
|
||||||
SELECT * FROM sounds
|
SELECT * FROM sounds
|
||||||
ORDER BY plays
|
ORDER BY plays
|
||||||
"
|
"
|
||||||
)
|
)
|
||||||
.fetch_all(&pool)
|
.fetch_all(&pool)
|
||||||
@ -1155,7 +1170,7 @@ async fn show_random_sounds(ctx: &Context, msg: &Message, _args: Args) -> Comman
|
|||||||
Sound,
|
Sound,
|
||||||
"
|
"
|
||||||
SELECT * FROM sounds
|
SELECT * FROM sounds
|
||||||
ORDER BY rand()
|
ORDER BY rand()
|
||||||
"
|
"
|
||||||
)
|
)
|
||||||
.fetch_all(&pool)
|
.fetch_all(&pool)
|
||||||
|
Loading…
Reference in New Issue
Block a user