Multiple bug fixes

-Fix manager commands getting repeated
-Lowered background task interval
-Fixed ban lookup resulting in a crash
This commit is contained in:
Noikoio 2018-07-24 14:48:51 -07:00
parent 8d44858d71
commit e02b1aa28c
3 changed files with 74 additions and 53 deletions

View file

@ -13,7 +13,7 @@ Class BackgroundWorker
Private ReadOnly _db As Database 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 = 30 ' How often the worker wakes up, in seconds Const Interval = 180 ' How often the worker wakes up, in seconds
Private _clock As IClock Private _clock As IClock
Sub New(instance As BirthdayBot, dbsettings As Database) Sub New(instance As BirthdayBot, dbsettings As Database)

View file

@ -130,7 +130,7 @@ Class GuildSettings
Using db = Await _db.OpenConnectionAsync() Using db = Await _db.OpenConnectionAsync()
Using c = db.CreateCommand() Using c = db.CreateCommand()
c.CommandText = $"select * from {BackingTableBans}" + c.CommandText = $"select * from {BackingTableBans} " +
"where guild_id = @Gid and user_id = @Uid" "where guild_id = @Gid and user_id = @Uid"
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = GuildId c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = GuildId
c.Parameters.Add("@Uid", NpgsqlDbType.Bigint).Value = userId c.Parameters.Add("@Uid", NpgsqlDbType.Bigint).Value = userId
@ -214,6 +214,7 @@ Class GuildSettings
"user_id bigint not null, " + "user_id bigint not null, " +
"PRIMARY KEY (guild_id, user_id)" + "PRIMARY KEY (guild_id, user_id)" +
")" ")"
c.ExecuteNonQuery()
End Using End Using
End Sub End Sub

View file

@ -6,11 +6,14 @@ Imports Discord.WebSocket
Friend Class HelpCommands Friend Class HelpCommands
Inherits CommandsCommon Inherits CommandsCommon
Private _helpEmbed As EmbedBuilder ' Lazily generated in the help command handler Private ReadOnly _helpEmbed As Embed
Private _helpManagerInfo As EmbedFieldBuilder ' Same Private ReadOnly _helpEmbedManager As Embed
Sub New(inst As BirthdayBot, db As Configuration) Sub New(inst As BirthdayBot, db As Configuration)
MyBase.New(inst, db) MyBase.New(inst, db)
Dim embeds = CreateHelpEmbed()
_helpEmbed = embeds.Item1
_helpEmbedManager = embeds.Item2
End Sub End Sub
Public Overrides ReadOnly Property Commands As IEnumerable(Of (String, CommandHandler)) Public Overrides ReadOnly Property Commands As IEnumerable(Of (String, CommandHandler))
@ -21,60 +24,77 @@ Friend Class HelpCommands
End Get End Get
End Property End Property
Private Function CreateHelpEmbed() As (EmbedBuilder, EmbedBuilder)
Dim title = "Help & About"
Dim description = "Birthday Bot: A utility to assist with acknowledging birthdays and other annual events." + vbLf +
"**Currently a work in progress. There will be bugs. Features may change or be removed.**"
Dim footer As New EmbedFooterBuilder With {
.Text = Discord.CurrentUser.Username,
.IconUrl = Discord.CurrentUser.GetAvatarUrl()
}
Dim cpfx = $"●`{CommandPrefix}"
' Normal section
Dim cmdField As New EmbedFieldBuilder With {
.Name = "Commands",
.Value =
$"{cpfx}help`, `{CommandPrefix}info`, `{CommandPrefix}tzdata`" + vbLf +
$" » Various help messages." + vbLf +
$"{cpfx}set (date) [zone]`" + vbLf +
$" » Registers your birth date, with optional time zone." + vbLf +
$" »» Examples: `{CommandPrefix}set jan-31 America/New_York`, `{CommandPrefix}set 15-aug Europe/Stockholm`." + vbLf +
$"{cpfx}set-tz (zone)`" + vbLf +
$" » Sets your local time zone. Only accepts certain values. See `{CommandPrefix}tzdata`." + vbLf +
$"{cpfx}remove`" + vbLf +
$" » Removes all your information from this bot."
}
' Manager section
Dim mpfx = cpfx + "config "
Dim managerField As New EmbedFieldBuilder With {
.Name = "Commands for server managers",
.Value =
$"{mpfx}role (role name or ID)`" + vbLf +
" » Specifies which role to apply to users having birthdays." + vbLf +
$"{mpfx}channel (channel name or ID)`" + vbLf +
" » Sets the birthday and event announcement channel. Leave blank to disable announcements." + vbLf +
$"{mpfx}set-tz (time zone name)`" + vbLf +
" » Sets the default time zone to use with all dates. Leave blank to revert to default." + vbLf +
$" » Only accepts certain values. See `{CommandPrefix}tzdata`." + vbLf +
$"{mpfx}ban/unban (user mention or ID)`" + vbLf +
" » Restricts or reallows access to this bot for the given user." + vbLf +
$"{mpfx}ban-all/unban-all`" + vbLf +
" » Restricts or reallows access to this bot for all users. Server managers are exempt." + vbLf +
$"{cpfx}override (user ID) (regular command)`" + vbLf +
" » Performs a command on behalf of the given user."
}
Dim helpNoManager As New EmbedBuilder
With helpNoManager
.Footer = footer
.Title = title
.Description = description
.AddField(cmdField)
End With
Dim helpManager As New EmbedBuilder
With helpManager
.Footer = footer
.Title = title
.Description = description
.AddField(cmdField)
.AddField(managerField)
End With
Return (helpNoManager, helpManager)
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. " + 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. " + "This bot requires the ability to be able to set and unset the specified role onto all users. " +
"It cannot function without it." + vbLf + "It cannot function without it." + vbLf +
"To designate a birthday role, issue the command `{0}config role (role name/ID)`." "To designate a birthday role, issue the command `{0}config role (role name/ID)`."
If _helpEmbed Is Nothing Then
Dim em As New EmbedBuilder
With em
.Footer = New EmbedFooterBuilder With {
.Text = Discord.CurrentUser.Username,
.IconUrl = Discord.CurrentUser.GetAvatarUrl()
}
.Title = "Help & About"
.Description = "Birthday Bot: A utility to assist with acknowledging birthdays and other annual events.\n" +
"**Currently a work in progress. There will be bugs. Features may change or be removed.**"
End With
Dim cpfx = $"●`{CommandPrefix}"
Dim cmdField As New EmbedFieldBuilder With {
.Name = "Commands",
.Value =
$"{cpfx}help`, `{CommandPrefix}info`, `{CommandPrefix}tzdata`" + vbLf +
$" » Various help messages." + vbLf +
$"{cpfx}set (date) [zone]`" + vbLf +
$" » Registers your birth date, with optional time zone." + vbLf +
$" »» Examples: `{CommandPrefix}set jan-31 America/New_York`, `{CommandPrefix}set 15-aug Europe/Stockholm`." + vbLf +
$"{cpfx}set-tz (zone)`" + vbLf +
$" » Sets your local time zone. Only accepts certain values. See `{CommandPrefix}tzdata`." + vbLf +
$"{cpfx}remove`" + vbLf +
$" » Removes all your information from this bot."
}
em.AddField(cmdField)
_helpEmbed = em
Dim mpfx = cpfx + "config "
_helpManagerInfo = New EmbedFieldBuilder With {
.Name = "Commands for server managers",
.Value =
$"{mpfx}role (role name or ID)`" + vbLf +
" » Specifies which role to apply to users having birthdays." + vbLf +
$"{mpfx}channel (channel name or ID)`" + vbLf +
" » Sets the birthday and event announcement channel. Leave blank to disable announcements." + vbLf +
$"{mpfx}set-tz (time zone name)`" + vbLf +
" » Sets the default time zone to use with all dates. Leave blank to revert to default." + vbLf +
$" » Only accepts certain values. See `{CommandPrefix}tzdata`." + vbLf +
$"{mpfx}ban/unban (user mention or ID)`" + vbLf +
" » Restricts or reallows access to this bot for the given user." + vbLf +
$"{mpfx}ban-all/unban-all`" + vbLf +
" » Restricts or reallows access to this bot for all users. Server managers are exempt." + vbLf +
$"{cpfx}override (user ID) (regular command)`" + vbLf +
" » Performs a command on behalf of the given user."
}
End If
' 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
@ -89,6 +109,6 @@ Friend Class HelpCommands
Dim showManagerCommands = reqUser.GuildPermissions.ManageGuild Dim showManagerCommands = reqUser.GuildPermissions.ManageGuild
Await reqChannel.SendMessageAsync(If(useFunctionMessage, String.Format(FunctionMsg, CommandPrefix), ""), Await reqChannel.SendMessageAsync(If(useFunctionMessage, String.Format(FunctionMsg, CommandPrefix), ""),
embed:=If(showManagerCommands, _helpEmbed.AddField(_helpManagerInfo), _helpEmbed)) embed:=If(showManagerCommands, _helpEmbedManager, _helpEmbed))
End Function End Function
End Class End Class