Change references to config

This commit is contained in:
Noikoio 2019-04-26 23:00:37 -07:00
parent 3ac8a209f3
commit 7c41be45e3
2 changed files with 12 additions and 13 deletions

View file

@ -9,15 +9,13 @@ Imports NodaTime
''' </summary> ''' </summary>
Class BackgroundWorker Class BackgroundWorker
Private ReadOnly _bot As BirthdayBot Private ReadOnly _bot As BirthdayBot
Private ReadOnly _db As Database
Private ReadOnly Property WorkerCancel As New CancellationTokenSource Private ReadOnly Property WorkerCancel As New CancellationTokenSource
Private _workerTask As Task Private _workerTask As Task
Const Interval = 45 ' How often the worker wakes up, in seconds. Adjust as needed. 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 _bot = instance
_db = dbsettings
_clock = SystemClock.Instance ' can replace with FakeClock here when testing _clock = SystemClock.Instance ' can replace with FakeClock here when testing
End Sub End Sub
@ -37,15 +35,15 @@ Class BackgroundWorker
Private Async Function WorkerLoop() As Task Private Async Function WorkerLoop() As Task
While Not WorkerCancel.IsCancellationRequested While Not WorkerCancel.IsCancellationRequested
Try Try
' Delay a bit before we start (or continue) work.
Await Task.Delay(Interval * 1000, WorkerCancel.Token)
' Start background tasks. ' Start background tasks.
Dim bgTasks As New List(Of Task) From { Dim bgTasks As New List(Of Task) From {
ReportAsync(), ReportAsync(),
BirthdayAsync() BirthdayAsync()
} }
Await Task.WhenAll(bgTasks) Await Task.WhenAll(bgTasks)
' All done. Wait until we have to work again.
Await Task.Delay(Interval * 1000, WorkerCancel.Token)
Catch ex As TaskCanceledException Catch ex As TaskCanceledException
Return Return
Catch ex As Exception Catch ex As Exception
@ -284,7 +282,7 @@ Class BackgroundWorker
Dim count = _bot.DiscordClient.Guilds.Count Dim count = _bot.DiscordClient.Guilds.Count
Log("Report", $"Currently in {count} guild(s).") Log("Report", $"Currently in {count} guild(s).")
Dim dtok = _bot._cfg.DBotsToken Dim dtok = _bot.Config.DBotsToken
If dtok IsNot Nothing Then If dtok IsNot Nothing Then
Const dUrl As String = "https://discord.bots.gg/api/v1/bots/{0}/stats" Const dUrl As String = "https://discord.bots.gg/api/v1/bots/{0}/stats"

View file

@ -14,9 +14,10 @@ Class BirthdayBot
Private ReadOnly _cmdsMods As ManagerCommands Private ReadOnly _cmdsMods As ManagerCommands
Private WithEvents _client As DiscordSocketClient Private WithEvents _client As DiscordSocketClient
Friend _cfg As Configuration
Private ReadOnly _worker As BackgroundWorker Private ReadOnly _worker As BackgroundWorker
Friend ReadOnly Property Config As Configuration
Friend ReadOnly Property DiscordClient As DiscordSocketClient Friend ReadOnly Property DiscordClient As DiscordSocketClient
Get Get
Return _client Return _client
@ -27,11 +28,11 @@ Class BirthdayBot
Friend ReadOnly Property KnownGuilds As Dictionary(Of ULong, GuildSettings) Friend ReadOnly Property KnownGuilds As Dictionary(Of ULong, GuildSettings)
Public Sub New(conf As Configuration, dc As DiscordSocketClient) Public Sub New(conf As Configuration, dc As DiscordSocketClient)
_cfg = conf Config = conf
_client = dc _client = dc
KnownGuilds = New Dictionary(Of ULong, GuildSettings) KnownGuilds = New Dictionary(Of ULong, GuildSettings)
_worker = New BackgroundWorker(Me, conf.DatabaseSettings) _worker = New BackgroundWorker(Me)
' Command dispatch set-up ' Command dispatch set-up
_dispatchCommands = New Dictionary(Of String, CommandHandler)(StringComparer.InvariantCultureIgnoreCase) _dispatchCommands = New Dictionary(Of String, CommandHandler)(StringComparer.InvariantCultureIgnoreCase)
@ -50,7 +51,7 @@ Class BirthdayBot
End Sub End Sub
Public Async Function Start() As Task Public Async Function Start() As Task
Await _client.LoginAsync(TokenType.Bot, _cfg.BotToken) Await _client.LoginAsync(TokenType.Bot, Config.BotToken)
Await _client.StartAsync() Await _client.StartAsync()
_worker.Start() _worker.Start()
@ -69,7 +70,7 @@ Class BirthdayBot
Private Function LoadGuild(g As SocketGuild) As Task Handles _client.JoinedGuild, _client.GuildAvailable Private Function LoadGuild(g As SocketGuild) As Task Handles _client.JoinedGuild, _client.GuildAvailable
SyncLock KnownGuilds SyncLock KnownGuilds
If Not KnownGuilds.ContainsKey(g.Id) Then 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) KnownGuilds.Add(g.Id, gi)
End If End If
End SyncLock End SyncLock