From 7c41be45e32e3c6b96a8babf96268e5ee2ce8a27 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Fri, 26 Apr 2019 23:00:37 -0700 Subject: [PATCH] Change references to config --- BirthdayBot/BackgroundWorker.vb | 14 ++++++-------- BirthdayBot/BirthdayBot.vb | 11 ++++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/BirthdayBot/BackgroundWorker.vb b/BirthdayBot/BackgroundWorker.vb index 6ebfffd..38bc37e 100644 --- a/BirthdayBot/BackgroundWorker.vb +++ b/BirthdayBot/BackgroundWorker.vb @@ -9,15 +9,13 @@ Imports NodaTime ''' Class BackgroundWorker Private ReadOnly _bot As BirthdayBot - Private ReadOnly _db As Database Private ReadOnly Property WorkerCancel As New CancellationTokenSource Private _workerTask As Task Const Interval = 45 ' How often the worker wakes up, in seconds. Adjust as needed. - Private _clock As IClock + Private ReadOnly _clock As IClock - Sub New(instance As BirthdayBot, dbsettings As Database) + Sub New(instance As BirthdayBot) _bot = instance - _db = dbsettings _clock = SystemClock.Instance ' can replace with FakeClock here when testing End Sub @@ -37,15 +35,15 @@ Class BackgroundWorker Private Async Function WorkerLoop() As Task While Not WorkerCancel.IsCancellationRequested Try + ' Delay a bit before we start (or continue) work. + Await Task.Delay(Interval * 1000, WorkerCancel.Token) + ' Start background tasks. Dim bgTasks As New List(Of Task) From { ReportAsync(), BirthdayAsync() } Await Task.WhenAll(bgTasks) - - ' All done. Wait until we have to work again. - Await Task.Delay(Interval * 1000, WorkerCancel.Token) Catch ex As TaskCanceledException Return Catch ex As Exception @@ -284,7 +282,7 @@ Class BackgroundWorker Dim count = _bot.DiscordClient.Guilds.Count Log("Report", $"Currently in {count} guild(s).") - Dim dtok = _bot._cfg.DBotsToken + Dim dtok = _bot.Config.DBotsToken If dtok IsNot Nothing Then Const dUrl As String = "https://discord.bots.gg/api/v1/bots/{0}/stats" diff --git a/BirthdayBot/BirthdayBot.vb b/BirthdayBot/BirthdayBot.vb index d32d2fc..e921642 100644 --- a/BirthdayBot/BirthdayBot.vb +++ b/BirthdayBot/BirthdayBot.vb @@ -14,9 +14,10 @@ Class BirthdayBot Private ReadOnly _cmdsMods As ManagerCommands Private WithEvents _client As DiscordSocketClient - Friend _cfg As Configuration Private ReadOnly _worker As BackgroundWorker + Friend ReadOnly Property Config As Configuration + Friend ReadOnly Property DiscordClient As DiscordSocketClient Get Return _client @@ -27,11 +28,11 @@ Class BirthdayBot Friend ReadOnly Property KnownGuilds As Dictionary(Of ULong, GuildSettings) Public Sub New(conf As Configuration, dc As DiscordSocketClient) - _cfg = conf + Config = conf _client = dc KnownGuilds = New Dictionary(Of ULong, GuildSettings) - _worker = New BackgroundWorker(Me, conf.DatabaseSettings) + _worker = New BackgroundWorker(Me) ' Command dispatch set-up _dispatchCommands = New Dictionary(Of String, CommandHandler)(StringComparer.InvariantCultureIgnoreCase) @@ -50,7 +51,7 @@ Class BirthdayBot End Sub Public Async Function Start() As Task - Await _client.LoginAsync(TokenType.Bot, _cfg.BotToken) + Await _client.LoginAsync(TokenType.Bot, Config.BotToken) Await _client.StartAsync() _worker.Start() @@ -69,7 +70,7 @@ Class BirthdayBot Private Function LoadGuild(g As SocketGuild) As Task Handles _client.JoinedGuild, _client.GuildAvailable SyncLock KnownGuilds If Not KnownGuilds.ContainsKey(g.Id) Then - Dim gi = GuildSettings.LoadSettingsAsync(_cfg.DatabaseSettings, g.Id).GetAwaiter().GetResult() + Dim gi = GuildSettings.LoadSettingsAsync(Config.DatabaseSettings, g.Id).GetAwaiter().GetResult() KnownGuilds.Add(g.Id, gi) End If End SyncLock