From 8cec168e1bfadc418ddf86255e34eef33e9840af Mon Sep 17 00:00:00 2001 From: Noikoio Date: Tue, 24 Jul 2018 15:29:35 -0700 Subject: [PATCH] Improved bad role setting notification --- BirthdayBot/BackgroundWorker.vb | 20 ++++++++--------- BirthdayBot/BirthdayBot.vb | 26 +++++++++++++++++------ BirthdayBot/Data/GuildSettings.vb | 17 ++++++++++++--- BirthdayBot/UserInterface/HelpCommands.vb | 8 +------ 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/BirthdayBot/BackgroundWorker.vb b/BirthdayBot/BackgroundWorker.vb index 0ba3beb..d2e803e 100644 --- a/BirthdayBot/BackgroundWorker.vb +++ b/BirthdayBot/BackgroundWorker.vb @@ -57,26 +57,24 @@ Class BackgroundWorker ''' Private Async Function BirthdayWorkAsync(guild As SocketGuild) As Task ' Gather required information - Dim roleId, channelId As ULong? Dim tz As String Dim users As IEnumerable(Of GuildUserSettings) + Dim role As SocketRole = Nothing + Dim channel As SocketTextChannel = Nothing SyncLock _bot.KnownGuilds If Not _bot.KnownGuilds.ContainsKey(guild.Id) Then Return Dim gs = _bot.KnownGuilds(guild.Id) - roleId = gs.RoleId - channelId = gs.AnnounceChannelId tz = gs.TimeZone users = gs.Users + + If gs.AnnounceChannelId.HasValue Then channel = guild.GetTextChannel(gs.AnnounceChannelId.Value) + If gs.RoleId.HasValue Then role = guild.GetRole(gs.RoleId.Value) + If role Is Nothing Then + gs.RoleWarning = True + Return + End If End SyncLock - ' Resolve snowflakes to Discord.Net classes - Dim role As SocketRole = Nothing - If roleId.HasValue Then role = guild.GetRole(roleId.Value) - If role Is Nothing Then Return ' Unable to work without it - - Dim channel As SocketTextChannel = Nothing - If channelId.HasValue Then channel = guild.GetTextChannel(channelId.Value) - ' Determine who's currently having a birthday Dim birthdays = BirthdayCalculate(users, tz) ' Note: Don't quit here if zero people are having birthdays. Roles may still need to be removed by BirthdayApply. diff --git a/BirthdayBot/BirthdayBot.vb b/BirthdayBot/BirthdayBot.vb index 659e3a0..e76c903 100644 --- a/BirthdayBot/BirthdayBot.vb +++ b/BirthdayBot/BirthdayBot.vb @@ -5,6 +5,10 @@ Imports Discord Imports Discord.WebSocket Class BirthdayBot + Const RoleWarningMsg As String = + "Note: This bot does not have a role set or is unable to use the role specified. " + + "Update the designated role with `bb.config role (role name/ID). This bot cannot function without it." + Private ReadOnly _dispatchCommands As Dictionary(Of String, CommandHandler) Private ReadOnly _cmdsUser As UserCommands Private ReadOnly _cmdsHelp As HelpCommands @@ -98,19 +102,29 @@ Class BirthdayBot Dim channel = CType(msg.Channel, SocketTextChannel) Dim author = CType(msg.Author, SocketGuildUser) - ' Ban check - but bypass if the author is a manager. - If Not author.GuildPermissions.ManageGuild Then - SyncLock KnownGuilds - If KnownGuilds(channel.Guild.Id).IsUserBannedAsync(author.Id).GetAwaiter().GetResult() Then + Dim roleWarning As Boolean + + ' Ban and role warning check + SyncLock KnownGuilds + Dim gi = KnownGuilds(channel.Guild.Id) + + ' Skip ban check if user is a manager + If Not author.GuildPermissions.ManageGuild Then + If gi.IsUserBannedAsync(author.Id).GetAwaiter().GetResult() Then Return End If - End SyncLock - End If + End If + + roleWarning = gi.RoleWarning + End SyncLock Dim h As CommandHandler = Nothing If _dispatchCommands.TryGetValue(csplit(0).Substring(CommandPrefix.Length), h) Then Try Await h(csplit, channel, author) + If roleWarning Then + Await channel.SendMessageAsync(RoleWarningMsg) + End If Catch ex As Exception channel.SendMessageAsync(":x: An unknown error occurred. It has been reported to the bot owner.").Wait() Log("Error", ex.ToString()) diff --git a/BirthdayBot/Data/GuildSettings.vb b/BirthdayBot/Data/GuildSettings.vb index 7dbd35c..3c780e5 100644 --- a/BirthdayBot/Data/GuildSettings.vb +++ b/BirthdayBot/Data/GuildSettings.vb @@ -9,7 +9,7 @@ Imports NpgsqlTypes ''' guild options, and provides some database abstractions regarding them all. ''' Object instances are loaded when entering a guild and discarded when the bot leaves the guild. ''' -Class GuildSettings +Friend Class GuildSettings Public ReadOnly Property GuildId As ULong Private ReadOnly _db As Database Private _role As ULong? @@ -18,10 +18,15 @@ Class GuildSettings Private _modded As Boolean Private _userCache As Dictionary(Of ULong, GuildUserSettings) + ''' + ''' Flag for notifying servers that the bot is unable to manipulate its role. + ''' + Public Property RoleWarning As Boolean + ''' ''' Gets a list of cached users. Use sparingly. ''' - Friend ReadOnly Property Users As IEnumerable(Of GuildUserSettings) + Public ReadOnly Property Users As IEnumerable(Of GuildUserSettings) Get Dim items As New List(Of GuildUserSettings) For Each item In _userCache.Values @@ -77,7 +82,12 @@ Class GuildSettings _db = dbconfig GuildId = CULng(reader.GetInt64(0)) ' Weird: if using a ternary operator with a ULong?, Nothing resolves to 0 despite Option Strict On. - If Not reader.IsDBNull(1) Then _role = CULng(reader.GetInt64(1)) + If Not reader.IsDBNull(1) Then + _role = CULng(reader.GetInt64(1)) + RoleWarning = False + Else + RoleWarning = True + End If If Not reader.IsDBNull(2) Then _channel = CULng(reader.GetInt64(2)) _tz = If(reader.IsDBNull(3), Nothing, reader.GetString(3)) _modded = reader.GetBoolean(4) @@ -180,6 +190,7 @@ Class GuildSettings Public Async Function UpdateRoleAsync(roleId As ULong) As Task _role = roleId + RoleWarning = False Await UpdateDatabaseAsync() End Function diff --git a/BirthdayBot/UserInterface/HelpCommands.vb b/BirthdayBot/UserInterface/HelpCommands.vb index cb6d903..6adb731 100644 --- a/BirthdayBot/UserInterface/HelpCommands.vb +++ b/BirthdayBot/UserInterface/HelpCommands.vb @@ -90,11 +90,6 @@ Friend Class HelpCommands End Function Private Async Function CmdHelp(param As String(), reqChannel As SocketTextChannel, reqUser As SocketGuildUser) As Task - Const FunctionMsg = "Attention server manager: A designated birthday role has not yet been set. " + - "This bot requires the ability to be able to set and unset the specified role onto all users. " + - "It cannot function without it." + vbLf + - "To designate a birthday role, issue the command `{0}config role (role name/ID)`." - ' Determine if an additional message about an invalid role should be added. Dim useFunctionMessage = False Dim gs As GuildSettings @@ -108,7 +103,6 @@ Friend Class HelpCommands ' Determine if the user asking is a manager Dim showManagerCommands = reqUser.GuildPermissions.ManageGuild - Await reqChannel.SendMessageAsync(If(useFunctionMessage, String.Format(FunctionMsg, CommandPrefix), ""), - embed:=If(showManagerCommands, _helpEmbedManager, _helpEmbed)) + Await reqChannel.SendMessageAsync("", embed:=If(showManagerCommands, _helpEmbedManager, _helpEmbed)) End Function End Class