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>
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.

View file

@ -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())

View file

@ -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.
''' </summary>
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)
''' <summary>
''' Flag for notifying servers that the bot is unable to manipulate its role.
''' </summary>
Public Property RoleWarning As Boolean
''' <summary>
''' Gets a list of cached users. Use sparingly.
''' </summary>
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

View file

@ -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