diff --git a/BirthdayBot/BackgroundServices/Heartbeat.vb b/BirthdayBot/BackgroundServices/Heartbeat.vb index 852c1ef..15eba4b 100644 --- a/BirthdayBot/BackgroundServices/Heartbeat.vb +++ b/BirthdayBot/BackgroundServices/Heartbeat.vb @@ -15,11 +15,14 @@ Class Heartbeat Log($"Tick {tick:00000} - Bot uptime: {BotUptime()}") End If - If (BotInstance.DiscordClient.ConnectionState = Discord.ConnectionState.Disconnected) Then - Log("Client is disconnected! Restart the app if this persists.") - ' The library alone cannot be restarted as it is in an unknown state. It was not designed to be restarted. - ' This is the part where we'd signal something to restart us if we were fancy. - End If + ' Disconnection warn + For Each shard In BotInstance.DiscordClient.Shards + If shard.ConnectionState = Discord.ConnectionState.Disconnected Then + Log($"Shard {shard.ShardId} is disconnected! Restart the app if this persists.") + ' The library alone cannot be restarted as it is in an unknown state. It was not designed to be restarted. + ' This is the part where we'd signal something to restart us if we were fancy. + End If + Next Return Task.CompletedTask End Function diff --git a/BirthdayBot/BirthdayBot.vb b/BirthdayBot/BirthdayBot.vb index 1787f3d..9e843c2 100644 --- a/BirthdayBot/BirthdayBot.vb +++ b/BirthdayBot/BirthdayBot.vb @@ -15,22 +15,22 @@ Class BirthdayBot Private ReadOnly _cmdsHelp As HelpInfoCommands Private ReadOnly _cmdsMods As ManagerCommands - Private WithEvents _client As DiscordSocketClient + Private WithEvents Client As DiscordShardedClient Private ReadOnly _worker As BackgroundServiceRunner Friend ReadOnly Property Config As Configuration - Friend ReadOnly Property DiscordClient As DiscordSocketClient + Friend ReadOnly Property DiscordClient As DiscordShardedClient Get - Return _client + Return Client End Get End Property Friend ReadOnly Property GuildCache As ConcurrentDictionary(Of ULong, GuildStateInformation) - Public Sub New(conf As Configuration, dc As DiscordSocketClient) + Public Sub New(conf As Configuration, dc As DiscordShardedClient) Config = conf - _client = dc + Client = dc GuildCache = New ConcurrentDictionary(Of ULong, GuildStateInformation) _worker = New BackgroundServiceRunner(Me) @@ -56,8 +56,8 @@ Class BirthdayBot End Sub Public Async Function Start() As Task - Await _client.LoginAsync(TokenType.Bot, Config.BotToken) - Await _client.StartAsync() + Await Client.LoginAsync(TokenType.Bot, Config.BotToken) + Await Client.StartAsync() _worker.Start() Await Task.Delay(-1) @@ -68,28 +68,28 @@ Class BirthdayBot ''' Public Async Function Shutdown() As Task Await _worker.Cancel() - Await _client.LogoutAsync() - _client.Dispose() + Await Client.LogoutAsync() + Client.Dispose() End Function - Private Async Function LoadGuild(g As SocketGuild) As Task Handles _client.JoinedGuild, _client.GuildAvailable + Private Async Function LoadGuild(g As SocketGuild) As Task Handles Client.JoinedGuild, Client.GuildAvailable If Not GuildCache.ContainsKey(g.Id) Then Dim gi = Await GuildStateInformation.LoadSettingsAsync(Config.DatabaseSettings, g.Id) GuildCache.TryAdd(g.Id, gi) End If End Function - Private Function DiscardGuild(g As SocketGuild) As Task Handles _client.LeftGuild + Private Function DiscardGuild(g As SocketGuild) As Task Handles Client.LeftGuild Dim rm As GuildStateInformation = Nothing GuildCache.TryRemove(g.Id, rm) Return Task.CompletedTask End Function - Private Async Function SetStatus() As Task Handles _client.Connected - Await _client.SetGameAsync(CommandPrefix + "help") + Private Async Function SetStatus() As Task Handles Client.ShardConnected + Await Client.SetGameAsync(CommandPrefix + "help") End Function - Private Async Function Dispatch(msg As SocketMessage) As Task Handles _client.MessageReceived + Private Async Function Dispatch(msg As SocketMessage) As Task Handles Client.MessageReceived If TypeOf msg.Channel Is IDMChannel Then Return If msg.Author.IsBot Then Return diff --git a/BirthdayBot/BirthdayBot.vbproj b/BirthdayBot/BirthdayBot.vbproj index 18a8aa5..0d651ae 100644 --- a/BirthdayBot/BirthdayBot.vbproj +++ b/BirthdayBot/BirthdayBot.vbproj @@ -4,7 +4,7 @@ Exe BirthdayBot netcoreapp2.0 - 1.2.0 + 1.3.0 Noiiko Discord bot for birthday reminders. diff --git a/BirthdayBot/Configuration.vb b/BirthdayBot/Configuration.vb index d41a01c..60ada1e 100644 --- a/BirthdayBot/Configuration.vb +++ b/BirthdayBot/Configuration.vb @@ -10,6 +10,8 @@ Class Configuration Public ReadOnly Property DBotsToken As String Public ReadOnly Property DatabaseSettings As Database + Public ReadOnly Property ShardCount As Integer + Sub New() ' Looks for settings.json in the executable directory. Dim confPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) @@ -38,5 +40,15 @@ Class Configuration Throw New Exception("'SqlConnectionString' must be specified.") End If DatabaseSettings = New Database(sqlcs) + + Dim sc? = jc("ShardCount")?.Value(Of Integer)() + If Not sc.HasValue Then + ShardCount = 1 + Else + ShardCount = sc.Value + If ShardCount <= 0 Then + Throw New Exception("'ShardCount' must be a positive integer.") + End If + End If End Sub End Class diff --git a/BirthdayBot/Program.vb b/BirthdayBot/Program.vb index bdde3a6..83e6e92 100644 --- a/BirthdayBot/Program.vb +++ b/BirthdayBot/Program.vb @@ -25,9 +25,11 @@ Module Program .AlwaysDownloadUsers = True .DefaultRetryMode = Discord.RetryMode.RetryRatelimit .MessageCacheSize = 0 + .TotalShards = cfg.ShardCount + .ExclusiveBulkDelete = True End With - Dim client As New DiscordSocketClient(dc) + Dim client As New DiscordShardedClient(dc) AddHandler client.Log, AddressOf DNetLog _bot = New BirthdayBot(cfg, client) @@ -50,6 +52,7 @@ Module Program End Sub Private Function DNetLog(arg As LogMessage) As Task + If arg.Severity <= LogSeverity.Info Then Log("Discord.Net", $"{arg.Severity}: {arg.Message}") End If diff --git a/BirthdayBot/UserInterface/CommandsCommon.vb b/BirthdayBot/UserInterface/CommandsCommon.vb index 4aed0d3..661af60 100644 --- a/BirthdayBot/UserInterface/CommandsCommon.vb +++ b/BirthdayBot/UserInterface/CommandsCommon.vb @@ -38,7 +38,7 @@ Friend MustInherit Class CommandsCommon Protected ReadOnly Instance As BirthdayBot Protected ReadOnly BotConfig As Configuration - Protected ReadOnly Discord As DiscordSocketClient + Protected ReadOnly Discord As DiscordShardedClient Sub New(inst As BirthdayBot, db As Configuration) Instance = inst diff --git a/BirthdayBot/UserInterface/HelpInfoCommands.vb b/BirthdayBot/UserInterface/HelpInfoCommands.vb index 338f02d..7259db5 100644 --- a/BirthdayBot/UserInterface/HelpInfoCommands.vb +++ b/BirthdayBot/UserInterface/HelpInfoCommands.vb @@ -7,11 +7,9 @@ Friend Class HelpInfoCommands Private ReadOnly _helpEmbed As Embed Private ReadOnly _helpConfigEmbed As Embed - Private ReadOnly _discordClient As DiscordSocketClient - Sub New(inst As BirthdayBot, db As Configuration, client As DiscordSocketClient) + Sub New(inst As BirthdayBot, db As Configuration, client As DiscordShardedClient) MyBase.New(inst, db) - _discordClient = client Dim embeds = BuildHelpEmbeds() _helpEmbed = embeds.Item1 _helpConfigEmbed = embeds.Item2 @@ -146,7 +144,7 @@ Friend Class HelpInfoCommands Dim strStatus As New StringBuilder Dim asmnm = Reflection.Assembly.GetExecutingAssembly.GetName() strStatus.AppendLine("Birthday Bot v" + asmnm.Version.ToString(3)) - strStatus.AppendLine("Server count: " + _discordClient.Guilds.Count.ToString()) + strStatus.AppendLine("Server count: " + Discord.Guilds.Count.ToString()) strStatus.AppendLine("Uptime: " + BotUptime()) ' TODO fun stats @@ -155,7 +153,7 @@ Friend Class HelpInfoCommands Dim embed As New EmbedBuilder With { .Author = New EmbedAuthorBuilder() With { .Name = "Thank you for using Birthday Bot!", - .IconUrl = _discordClient.CurrentUser.GetAvatarUrl() + .IconUrl = Discord.CurrentUser.GetAvatarUrl() }, .Description = "Suggestions and feedback are always welcome. Please refer to the listing on Discord Bots " + "(discord.bots.gg) for information on reaching my personal server. I may not be available often, but I am happy to " +