loop command
This commit is contained in:
62
src/main.rs
62
src/main.rs
@ -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,9 +181,14 @@ 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)
|
||||
.await;
|
||||
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 {
|
||||
|
10
src/sound.rs
10
src/sound.rs
@ -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(
|
||||
|
Reference in New Issue
Block a user