@@ -20,10 +20,11 @@ pub struct BotOptions {
2020 discord_token : String ,
2121}
2222
23- // User data, which is stored and accessible in all command invocations
23+ # [ derive ( Debug ) ]
2424struct Data {
2525 bot_options : BotOptions ,
2626 creds_registry : Arc < RwLock < CredsRegistry > > ,
27+ currently_playing_pid : Arc < RwLock < Option < u32 > > > ,
2728}
2829type Error = anyhow:: Error ;
2930type Context < ' a > = poise:: Context < ' a , Data , Error > ;
@@ -63,6 +64,7 @@ pub async fn run_bot(opts: BotOptions, stream_registry: Arc<RwLock<CredsRegistry
6364 Ok ( Data {
6465 bot_options : opts,
6566 creds_registry : stream_registry,
67+ currently_playing_pid : Arc :: new ( RwLock :: new ( None ) ) ,
6668 } )
6769 } )
6870 } ) ;
@@ -170,6 +172,8 @@ async fn play_spotify(ctx: Context<'_>, #[description = "Stream key"] key: Strin
170172
171173 let input = Input :: new ( true , reader, Codec :: Pcm , Container :: Raw , None ) ;
172174
175+ // TODO: send player a signal on stop, so it can shut down gracefully before it's Dropped
176+
173177 let mut call_handler = call_handler_lock. lock ( ) . await ;
174178 call_handler. play_source ( input) ;
175179
@@ -210,6 +214,16 @@ async fn stop(ctx: Context<'_>) -> Result<()> {
210214 }
211215 Some ( g) => g,
212216 } ;
217+
218+ {
219+ let mut pid = ctx. data ( ) . currently_playing_pid . read ( ) . unwrap ( ) ;
220+ if let Some ( pid) = pid. as_ref ( ) . take ( ) {
221+ tracing:: debug!( ?pid, "asking player to stop" ) ;
222+ let _ = nix:: sys:: signal:: kill ( Pid :: from_raw ( pid as i32 ) , Signal :: SIGUSR1 ) ;
223+ tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
224+ }
225+ }
226+
213227 let voice_manager = songbird:: get ( ctx. serenity_context ( ) ) . await . unwrap ( ) . clone ( ) ;
214228 let call_handler_lock = voice_manager. get ( guild. id ) ;
215229 if let Some ( call_handler_lock) = call_handler_lock {
0 commit comments