@@ -133,8 +133,9 @@ public Bot(Id id, ConfBot config, BotInjector injector)
133
133
// Update idle status events
134
134
playManager . BeforeResourceStarted += ( s , e ) => { DisableIdleTickWorker ( ) ; return Task . CompletedTask ; } ;
135
135
playManager . PlaybackStopped += ( s , e ) => { EnableIdleTickWorker ( ) ; return Task . CompletedTask ; } ;
136
- // Used for the voice_mode script
136
+ // Used for custom scripts, like voice_mode, onsongstart
137
137
playManager . BeforeResourceStarted += BeforeResourceStarted ;
138
+ playManager . AfterResourceStarted += AfterResourceStarted ;
138
139
// Update the own status text to the current song title
139
140
playManager . AfterResourceStarted += ( s , e ) => UpdateBotStatus ( ) ;
140
141
playManager . PlaybackStopped += ( s , e ) => UpdateBotStatus ( ) ;
@@ -143,8 +144,8 @@ public Bot(Id id, ConfBot config, BotInjector injector)
143
144
if ( Injector . TryGet < HistoryManager > ( out var historyManager ) )
144
145
playManager . AfterResourceStarted += ( s , e ) =>
145
146
{
146
- if ( e . MetaData != null )
147
- historyManager . LogAudioResource ( new HistorySaveData ( e . PlayResource . AudioResource , e . MetaData . ResourceOwnerUid ) ) ;
147
+ if ( e . PlayInfo != null )
148
+ historyManager . LogAudioResource ( new HistorySaveData ( e . PlayResource . AudioResource , e . PlayInfo . ResourceOwnerUid ) ) ;
148
149
return Task . CompletedTask ;
149
150
} ;
150
151
// Update our thumbnail
@@ -338,6 +339,39 @@ private void OnClientLeftView(object? sender, ClientLeftView eventArgs)
338
339
sessionManager . RemoveSession ( eventArgs . ClientId ) ;
339
340
}
340
341
342
+ private async Task BeforeResourceStarted ( object ? sender , PlayInfoEventArgs e )
343
+ {
344
+ const string DefaultVoiceScript = "!whisper off" ;
345
+ const string DefaultWhisperScript = "!xecute (!whisper subscription) (!unsubscribe temporary) (!subscribe channeltemp (!getmy channel))" ;
346
+
347
+ var mode = config . Audio . SendMode . Value ;
348
+ string script ;
349
+ if ( mode . StartsWith ( "!" , StringComparison . Ordinal ) )
350
+ script = mode ;
351
+ else if ( mode . Equals ( "voice" , StringComparison . OrdinalIgnoreCase ) )
352
+ script = DefaultVoiceScript ;
353
+ else if ( mode . Equals ( "whisper" , StringComparison . OrdinalIgnoreCase ) )
354
+ script = DefaultWhisperScript ;
355
+ else
356
+ {
357
+ Log . Error ( "Invalid voice mode" ) ;
358
+ return ;
359
+ }
360
+
361
+ var info = CreateExecInfo ( e . Invoker ) ;
362
+ await CallScript ( info , script , false , true ) ;
363
+ }
364
+
365
+ private async Task AfterResourceStarted ( object ? sender , PlayInfoEventArgs e )
366
+ {
367
+ var onSongStart = config . Events . OnSongStart . Value ;
368
+ if ( ! string . IsNullOrEmpty ( onSongStart ) )
369
+ {
370
+ var info = CreateExecInfo ( ) ;
371
+ await CallScript ( info , onSongStart , false , true ) ;
372
+ }
373
+ }
374
+
341
375
#region Status: Description, Avatar
342
376
343
377
public Task UpdateBotStatus ( )
@@ -449,28 +483,7 @@ await resourceResolver.GetThumbnail(startEvent.PlayResource,
449
483
450
484
#endregion
451
485
452
- private async Task BeforeResourceStarted ( object ? sender , PlayInfoEventArgs e )
453
- {
454
- const string DefaultVoiceScript = "!whisper off" ;
455
- const string DefaultWhisperScript = "!xecute (!whisper subscription) (!unsubscribe temporary) (!subscribe channeltemp (!getmy channel))" ;
456
-
457
- var mode = config . Audio . SendMode . Value ;
458
- string script ;
459
- if ( mode . StartsWith ( "!" , StringComparison . Ordinal ) )
460
- script = mode ;
461
- else if ( mode . Equals ( "voice" , StringComparison . OrdinalIgnoreCase ) )
462
- script = DefaultVoiceScript ;
463
- else if ( mode . Equals ( "whisper" , StringComparison . OrdinalIgnoreCase ) )
464
- script = DefaultWhisperScript ;
465
- else
466
- {
467
- Log . Error ( "Invalid voice mode" ) ;
468
- return ;
469
- }
470
-
471
- var info = CreateExecInfo ( e . Invoker ) ;
472
- await CallScript ( info , script , false , true ) ;
473
- }
486
+ #region Script Execution
474
487
475
488
private async Task CallScript ( ExecutionInformation info , string command , bool answer , bool skipRights )
476
489
{
@@ -510,6 +523,35 @@ private ExecutionInformation CreateExecInfo(InvokerData? invoker = null, UserSes
510
523
return info ;
511
524
}
512
525
526
+ private async Task TryCatchCommand ( ExecutionInformation info , bool answer , Func < Task > action )
527
+ {
528
+ try
529
+ {
530
+ await action . Invoke ( ) ;
531
+ }
532
+ catch ( AudioBotException ex )
533
+ {
534
+ NLog . LogLevel commandErrorLevel = answer ? NLog . LogLevel . Debug : NLog . LogLevel . Warn ;
535
+ Log . Log ( commandErrorLevel , ex , "Command Error ({0})" , ex . Message ) ;
536
+ if ( answer )
537
+ {
538
+ await info . Write ( TextMod . Format ( config . Commands . Color , strings . error_call_error . Mod ( ) . Color ( Color . Red ) . Bold ( ) , ex . Message ) )
539
+ . CatchToLog ( Log ) ;
540
+ }
541
+ }
542
+ catch ( Exception ex )
543
+ {
544
+ Log . Error ( ex , "Unexpected command error: {0}" , ex . Message ) ;
545
+ if ( answer )
546
+ {
547
+ await info . Write ( TextMod . Format ( config . Commands . Color , strings . error_call_unexpected_error . Mod ( ) . Color ( Color . Red ) . Bold ( ) , ex . Message ) )
548
+ . CatchToLog ( Log ) ;
549
+ }
550
+ }
551
+ }
552
+
553
+ #endregion
554
+
513
555
#region Event: Idle
514
556
515
557
private async void OnIdle ( )
@@ -570,33 +612,6 @@ private async Task OnAloneChanged(object? sender, AloneChanged e)
570
612
571
613
#endregion
572
614
573
- private async Task TryCatchCommand ( ExecutionInformation info , bool answer , Func < Task > action )
574
- {
575
- try
576
- {
577
- await action . Invoke ( ) ;
578
- }
579
- catch ( AudioBotException ex )
580
- {
581
- NLog . LogLevel commandErrorLevel = answer ? NLog . LogLevel . Debug : NLog . LogLevel . Warn ;
582
- Log . Log ( commandErrorLevel , ex , "Command Error ({0})" , ex . Message ) ;
583
- if ( answer )
584
- {
585
- await info . Write ( TextMod . Format ( config . Commands . Color , strings . error_call_error . Mod ( ) . Color ( Color . Red ) . Bold ( ) , ex . Message ) )
586
- . CatchToLog ( Log ) ;
587
- }
588
- }
589
- catch ( Exception ex )
590
- {
591
- Log . Error ( ex , "Unexpected command error: {0}" , ex . Message ) ;
592
- if ( answer )
593
- {
594
- await info . Write ( TextMod . Format ( config . Commands . Color , strings . error_call_unexpected_error . Mod ( ) . Color ( Color . Red ) . Bold ( ) , ex . Message ) )
595
- . CatchToLog ( Log ) ;
596
- }
597
- }
598
- }
599
-
600
615
public BotInfo GetInfo ( ) => new BotInfo
601
616
{
602
617
Id = Id ,
0 commit comments