Improved bad role setting notification

This commit is contained in:
Noikoio 2018-07-24 15:29:35 -07:00
parent e02b1aa28c
commit 8cec168e1b
4 changed files with 44 additions and 27 deletions

View file

@ -57,26 +57,24 @@ Class BackgroundWorker
''' </summary> ''' </summary>
Private Async Function BirthdayWorkAsync(guild As SocketGuild) As Task Private Async Function BirthdayWorkAsync(guild As SocketGuild) As Task
' Gather required information ' Gather required information
Dim roleId, channelId As ULong?
Dim tz As String Dim tz As String
Dim users As IEnumerable(Of GuildUserSettings) Dim users As IEnumerable(Of GuildUserSettings)
Dim role As SocketRole = Nothing
Dim channel As SocketTextChannel = Nothing
SyncLock _bot.KnownGuilds SyncLock _bot.KnownGuilds
If Not _bot.KnownGuilds.ContainsKey(guild.Id) Then Return If Not _bot.KnownGuilds.ContainsKey(guild.Id) Then Return
Dim gs = _bot.KnownGuilds(guild.Id) Dim gs = _bot.KnownGuilds(guild.Id)
roleId = gs.RoleId
channelId = gs.AnnounceChannelId
tz = gs.TimeZone tz = gs.TimeZone
users = gs.Users 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 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 ' Determine who's currently having a birthday
Dim birthdays = BirthdayCalculate(users, tz) 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. ' Note: Don't quit here if zero people are having birthdays. Roles may still need to be removed by BirthdayApply.

View file

@ -5,6 +5,10 @@ Imports Discord
Imports Discord.WebSocket Imports Discord.WebSocket
Class BirthdayBot 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 _dispatchCommands As Dictionary(Of String, CommandHandler)
Private ReadOnly _cmdsUser As UserCommands Private ReadOnly _cmdsUser As UserCommands
Private ReadOnly _cmdsHelp As HelpCommands Private ReadOnly _cmdsHelp As HelpCommands
@ -98,19 +102,29 @@ Class BirthdayBot
Dim channel = CType(msg.Channel, SocketTextChannel) Dim channel = CType(msg.Channel, SocketTextChannel)
Dim author = CType(msg.Author, SocketGuildUser) Dim author = CType(msg.Author, SocketGuildUser)
' Ban check - but bypass if the author is a manager. Dim roleWarning As Boolean
If Not author.GuildPermissions.ManageGuild Then
SyncLock KnownGuilds ' Ban and role warning check
If KnownGuilds(channel.Guild.Id).IsUserBannedAsync(author.Id).GetAwaiter().GetResult() Then 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 Return
End If End If
End SyncLock End If
End If
roleWarning = gi.RoleWarning
End SyncLock
Dim h As CommandHandler = Nothing Dim h As CommandHandler = Nothing
If _dispatchCommands.TryGetValue(csplit(0).Substring(CommandPrefix.Length), h) Then If _dispatchCommands.TryGetValue(csplit(0).Substring(CommandPrefix.Length), h) Then
Try Try
Await h(csplit, channel, author) Await h(csplit, channel, author)
If roleWarning Then
Await channel.SendMessageAsync(RoleWarningMsg)
End If
Catch ex As Exception Catch ex As Exception
channel.SendMessageAsync(":x: An unknown error occurred. It has been reported to the bot owner.").Wait() channel.SendMessageAsync(":x: An unknown error occurred. It has been reported to the bot owner.").Wait()
Log("Error", ex.ToString()) Log("Error", ex.ToString())

View file

@ -9,7 +9,7 @@ Imports NpgsqlTypes
''' guild options, and provides some database abstractions regarding them all. ''' 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. ''' Object instances are loaded when entering a guild and discarded when the bot leaves the guild.
''' </summary> ''' </summary>
Class GuildSettings Friend Class GuildSettings
Public ReadOnly Property GuildId As ULong Public ReadOnly Property GuildId As ULong
Private ReadOnly _db As Database Private ReadOnly _db As Database
Private _role As ULong? Private _role As ULong?
@ -18,10 +18,15 @@ Class GuildSettings
Private _modded As Boolean Private _modded As Boolean
Private _userCache As Dictionary(Of ULong, GuildUserSettings) Private _userCache As Dictionary(Of ULong, GuildUserSettings)
''' <summary>
''' Flag for notifying servers that the bot is unable to manipulate its role.
''' </summary>
Public Property RoleWarning As Boolean
''' <summary> ''' <summary>
''' Gets a list of cached users. Use sparingly. ''' Gets a list of cached users. Use sparingly.
''' </summary> ''' </summary>
Friend ReadOnly Property Users As IEnumerable(Of GuildUserSettings) Public ReadOnly Property Users As IEnumerable(Of GuildUserSettings)
Get Get
Dim items As New List(Of GuildUserSettings) Dim items As New List(Of GuildUserSettings)
For Each item In _userCache.Values For Each item In _userCache.Values
@ -77,7 +82,12 @@ Class GuildSettings
_db = dbconfig _db = dbconfig
GuildId = CULng(reader.GetInt64(0)) GuildId = CULng(reader.GetInt64(0))
' Weird: if using a ternary operator with a ULong?, Nothing resolves to 0 despite Option Strict On. ' 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)) If Not reader.IsDBNull(2) Then _channel = CULng(reader.GetInt64(2))
_tz = If(reader.IsDBNull(3), Nothing, reader.GetString(3)) _tz = If(reader.IsDBNull(3), Nothing, reader.GetString(3))
_modded = reader.GetBoolean(4) _modded = reader.GetBoolean(4)
@ -180,6 +190,7 @@ Class GuildSettings
Public Async Function UpdateRoleAsync(roleId As ULong) As Task Public Async Function UpdateRoleAsync(roleId As ULong) As Task
_role = roleId _role = roleId
RoleWarning = False
Await UpdateDatabaseAsync() Await UpdateDatabaseAsync()
End Function End Function

View file

@ -90,11 +90,6 @@ Friend Class HelpCommands
End Function End Function
Private Async Function CmdHelp(param As String(), reqChannel As SocketTextChannel, reqUser As SocketGuildUser) As Task 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. ' Determine if an additional message about an invalid role should be added.
Dim useFunctionMessage = False Dim useFunctionMessage = False
Dim gs As GuildSettings Dim gs As GuildSettings
@ -108,7 +103,6 @@ Friend Class HelpCommands
' Determine if the user asking is a manager ' Determine if the user asking is a manager
Dim showManagerCommands = reqUser.GuildPermissions.ManageGuild Dim showManagerCommands = reqUser.GuildPermissions.ManageGuild
Await reqChannel.SendMessageAsync(If(useFunctionMessage, String.Format(FunctionMsg, CommandPrefix), ""), Await reqChannel.SendMessageAsync("", embed:=If(showManagerCommands, _helpEmbedManager, _helpEmbed))
embed:=If(showManagerCommands, _helpEmbedManager, _helpEmbed))
End Function End Function
End Class End Class