22using System . Diagnostics . Metrics ;
33using System . Text ;
44using System . Threading . Channels ;
5+ using BF2WebAdmin . Common ;
56using BF2WebAdmin . Common . Entities . Game ;
67using BF2WebAdmin . Server . Abstractions ;
78using BF2WebAdmin . Server . Commands ;
@@ -42,6 +43,7 @@ public class DiscordModule : BaseModule,
4243
4344 private readonly IGameServer _game ;
4445 private readonly IGameStreamService _gameStreamService ;
46+ private readonly ITaskRunner _taskRunner ;
4547 private readonly IDictionary < int , bool > _newPlayers = new Dictionary < int , bool > ( ) ;
4648 private readonly Channel < ( ISocketMessageChannel Channel , string ? Text , Embed ? Embed ) > _discordMessageChannel ;
4749 private IEnumerable < SocketTextChannel > _adminChannels ;
@@ -229,10 +231,11 @@ public class DiscordModule : BaseModule,
229231
230232 private ServerInfo . DiscordBotConfig Config => _game . ServerInfo . DiscordBot ;
231233
232- public DiscordModule ( IGameServer server , IGameStreamService gameStreamService , ILogger < DiscordModule > logger , CancellationTokenSource cts ) : base ( server , logger , cts )
234+ public DiscordModule ( IGameServer server , IGameStreamService gameStreamService , ITaskRunner taskRunner , ILogger < DiscordModule > logger , CancellationTokenSource cts ) : base ( server , logger , cts )
233235 {
234236 _game = server ;
235237 _gameStreamService = gameStreamService ;
238+ _taskRunner = taskRunner ;
236239
237240 if ( string . IsNullOrWhiteSpace ( server . ServerInfo . DiscordBot ? . Token ) )
238241 return ;
@@ -251,7 +254,7 @@ public DiscordModule(IGameServer server, IGameStreamService gameStreamService, I
251254 }
252255
253256 // TODO: Start async in an event callback - proper?
254- RunBackgroundTask ( "Discord Bot" , StartBotAsync ) ;
257+ _taskRunner . RunBackgroundTask ( "Discord Bot" , StartBotAsync , ModuleCancellationToken ) ;
255258 }
256259
257260 private async Task StartBotAsync ( )
@@ -418,7 +421,7 @@ private static string GetTeamFlag(string teamName)
418421
419422 private async Task UpdateActivityNameAsync ( )
420423 {
421- RunBackgroundTask ( "Update Discord bot activity name" , async ( ) =>
424+ _taskRunner . RunBackgroundTask ( "Update Discord bot activity name" , async ( ) =>
422425 {
423426 if ( _game . SocketState == SocketState . Disconnected )
424427 {
@@ -435,7 +438,7 @@ private async Task UpdateActivityNameAsync()
435438 var name = $ "{ _game . Players . Count ( ) } /{ _game . MaxPlayers } - { _game . Map ? . Name ?? "Unknown" } ";
436439 await _discord . SetActivityAsync ( _streamUrl is null ? new Game ( name ) : new StreamingGame ( name , _streamUrl ) ) ;
437440 }
438- } ) ;
441+ } , ModuleCancellationToken ) ;
439442 }
440443
441444 // Used to prevent FakeGameServer spam when fast forwarding
0 commit comments