loop command

This commit is contained in:
jellywx 2021-03-15 15:03:15 +00:00
parent 9e30529981
commit a4919e45bf
4 changed files with 99 additions and 27 deletions

51
Cargo.lock generated
View File

@ -120,6 +120,17 @@ dependencies = [
"num-traits 0.2.14",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "audiopus"
version = "0.2.0"
@ -422,6 +433,19 @@ dependencies = [
"num-traits 0.1.43",
]
[[package]]
name = "env_logger"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
dependencies = [
"atty",
"humantime",
"log 0.4.14",
"regex",
"termcolor",
]
[[package]]
name = "flate2"
version = "1.0.19"
@ -727,6 +751,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.2"
@ -1879,9 +1909,10 @@ dependencies = [
[[package]]
name = "soundfx-rs"
version = "1.1.1"
version = "1.2.0"
dependencies = [
"dotenv",
"env_logger",
"lazy_static",
"log 0.4.14",
"regex",
@ -2154,6 +2185,15 @@ dependencies = [
"winapi 0.2.8",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.23"
@ -2669,6 +2709,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -1,6 +1,6 @@
[package]
name = "soundfx-rs"
version = "1.1.1"
version = "1.2.0"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"
@ -11,6 +11,7 @@ dotenv = "0.15"
tokio = { version = "1.0", features = ["fs", "process", "io-util"] }
lazy_static = "1.4"
reqwest = "0.11"
env_logger = "0.8"
songbird = { git = "https://github.com/serenity-rs/songbird", branch = "current" }
regex = "1.4"
log = "0.4"

View File

@ -27,12 +27,7 @@ use serenity::{
utils::shard_id,
};
use songbird::{
create_player,
driver::{Config, CryptoMode, DecodeMode},
error::JoinResult,
Call, SerenityInit,
};
use songbird::{create_player, error::JoinResult, Call, SerenityInit};
use sqlx::mysql::MySqlPool;
@ -186,8 +181,13 @@ SELECT name, id, plays, public, server_id, uploader_id
let (handler, _) = join_channel(&ctx, guild, user_channel).await;
let _ =
play_audio(&mut sound, guild_data, &mut handler.lock().await, pool)
let _ = play_audio(
&mut sound,
guild_data,
&mut handler.lock().await,
pool,
false,
)
.await;
}
}
@ -202,13 +202,20 @@ async fn play_audio(
guild: GuildData,
call_handler: &mut MutexGuard<'_, Call>,
mysql_pool: MySqlPool,
loop_: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
{
let (track, track_handler) =
create_player(sound.store_sound_source(mysql_pool.clone()).await?);
create_player(sound.store_sound_source(mysql_pool.clone()).await?.into());
let _ = track_handler.set_volume(guild.volume as f32 / 100.0);
if loop_ {
let _ = track_handler.enable_loop();
} else {
let _ = track_handler.disable_loop();
}
call_handler.play(track);
}
@ -247,6 +254,8 @@ async fn join_channel(
// entry point
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
env_logger::init();
dotenv()?;
let token = env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment");
@ -265,6 +274,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("invite", &INFO_COMMAND)
.add_command("donate", &INFO_COMMAND)
// play commands
.add_command("loop", &LOOP_PLAY_COMMAND)
.add_command("play", &PLAY_COMMAND)
.add_command("p", &PLAY_COMMAND)
.add_command("stop", &STOP_PLAYING_COMMAND)
@ -319,6 +329,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
#[command]
#[permission_level(Managed)]
async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
play_cmd(ctx, msg, args, false).await
}
#[command]
#[permission_level(Managed)]
async fn loop_play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
play_cmd(ctx, msg, args, true).await
}
async fn play_cmd(ctx: &Context, msg: &Message, args: Args, loop_: bool) -> CommandResult {
let guild = match msg.guild(&ctx.cache).await {
Some(guild) => guild,
@ -367,7 +387,7 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut lock = call_handler.lock().await;
play_audio(sound, guild_data, &mut lock, pool).await?;
play_audio(sound, guild_data, &mut lock, pool, loop_).await?;
}
msg.channel_id
@ -396,6 +416,16 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
Ok(())
}
#[command]
#[permission_level(Managed)]
async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let voice_manager = songbird::get(ctx).await.unwrap();
let _ = voice_manager.leave(msg.guild_id.unwrap()).await;
Ok(())
}
#[command]
async fn help(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
msg.channel_id
@ -1098,16 +1128,6 @@ WHERE
Ok(())
}
#[command]
#[permission_level(Managed)]
async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let voice_manager = songbird::get(ctx).await.unwrap();
let _ = voice_manager.leave(msg.guild_id.unwrap()).await;
Ok(())
}
#[command]
#[permission_level(Managed)]
async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {

View File

@ -4,8 +4,8 @@ use sqlx::mysql::MySqlPool;
use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use songbird::ffmpeg;
use songbird::input::Input;
use songbird::input::restartable::Restartable;
use std::{env, path::Path};
pub struct Sound {
@ -136,7 +136,7 @@ SELECT src
pub async fn store_sound_source(
&self,
db_pool: MySqlPool,
) -> Result<Input, Box<dyn std::error::Error + Send + Sync>> {
) -> Result<Restartable, Box<dyn std::error::Error + Send + Sync>> {
let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp"));
let path_name = format!("{}/sound-{}", caching_location, self.id);
@ -148,7 +148,9 @@ SELECT src
file.write_all(&self.src(db_pool).await).await?;
}
Ok(ffmpeg(path_name).await.expect("FFMPEG ERROR!"))
Ok(Restartable::ffmpeg(path_name, false)
.await
.expect("FFMPEG ERROR!"))
}
pub async fn count_user_sounds(