Add announce message ping option

This commit is contained in:
Noikoio 2019-05-27 22:28:10 -07:00
parent be82c0c9ac
commit 757ba7476e
4 changed files with 61 additions and 7 deletions

View file

@ -98,12 +98,14 @@ Class BackgroundWorker
Dim role As SocketRole = Nothing
Dim channel As SocketTextChannel = Nothing
Dim announce As (String, String)
Dim announceping As Boolean
SyncLock _bot.KnownGuilds
If Not _bot.KnownGuilds.ContainsKey(guild.Id) Then Return 0
Dim gs = _bot.KnownGuilds(guild.Id)
tz = gs.TimeZone
users = gs.Users
announce = gs.AnnounceMessages
announceping = gs.AnnouncePing
If gs.AnnounceChannelId.HasValue Then channel = guild.GetTextChannel(gs.AnnounceChannelId.Value)
If gs.RoleId.HasValue Then role = guild.GetRole(gs.RoleId.Value)
@ -135,7 +137,7 @@ Class BackgroundWorker
End Try
If announceNames.Count <> 0 Then
' Send out announcement message
Await BirthdayAnnounceAsync(announce, channel, announceNames)
Await BirthdayAnnounceAsync(announce, announceping, channel, announceNames)
End If
Return announceNames.Count
@ -203,8 +205,9 @@ Class BackgroundWorker
Return newBirthdays
End Function
Private Function BirthdayAnnounceFormatName(member As SocketGuildUser) As String
' TODO add option for using pings instead, add handling for it here
Private Function BirthdayAnnounceFormatName(member As SocketGuildUser, ping As Boolean) As String
If ping Then Return member.Mention
Dim escapeFormattingCharacters = Function(input As String) As String
Dim result As New StringBuilder
For Each c As Char In input
@ -231,6 +234,7 @@ Class BackgroundWorker
''' who have just had their birthday role added.
''' </summary>
Private Async Function BirthdayAnnounceAsync(announce As (String, String),
announcePing As Boolean,
c As SocketTextChannel,
names As IEnumerable(Of SocketGuildUser)) As Task
If c Is Nothing Then Return
@ -247,7 +251,7 @@ Class BackgroundWorker
' Build sorted name list
Dim namestrings As New List(Of String)
For Each item In names
namestrings.Add(BirthdayAnnounceFormatName(item))
namestrings.Add(BirthdayAnnounceFormatName(item, announcePing))
Next
namestrings.Sort(StringComparer.OrdinalIgnoreCase)

View file

@ -18,6 +18,7 @@ Friend Class GuildSettings
Private _moderated As Boolean
Private _announceMsg As String
Private _announceMsgPl As String
Private _announcePing As Boolean
Private _userCache As Dictionary(Of ULong, GuildUserSettings)
Private _roleWarning As Boolean
@ -114,6 +115,16 @@ Friend Class GuildSettings
End Get
End Property
''' <summary>
''' Gets whether to ping users in the announcement message instead of displaying their names.
''' </summary>
''' <returns></returns>
Public ReadOnly Property AnnouncePing As Boolean
Get
Return _announcePing
End Get
End Property
' Called by LoadSettingsAsync. Double-check ordinals when changes are made.
Private Sub New(reader As DbDataReader, dbconfig As Database)
_db = dbconfig
@ -131,6 +142,7 @@ Friend Class GuildSettings
If Not reader.IsDBNull(5) Then _modRole = CULng(reader.GetInt64(5))
_announceMsg = If(reader.IsDBNull(6), Nothing, reader.GetString(6))
_announceMsgPl = If(reader.IsDBNull(7), Nothing, reader.GetString(7))
_announcePing = reader.GetBoolean(8)
' Get user information loaded up.
Dim userresult = GuildUserSettings.GetGuildUsersAsync(dbconfig, GuildId)
@ -276,6 +288,11 @@ Friend Class GuildSettings
Await UpdateDatabaseAsync()
End Function
Public Async Function UpdateAnnouncePingAsync(value As Boolean) As Task
_announcePing = value
Await UpdateDatabaseAsync()
End Function
#Region "Database"
Public Const BackingTable = "settings"
Public Const BackingTableBans = "banned_users"
@ -290,7 +307,8 @@ Friend Class GuildSettings
"moderated boolean not null default FALSE, " +
"moderator_role bigint null, " +
"announce_message text null, " +
"announce_message_pl text null" +
"announce_message_pl text null, " +
"announce_ping boolean not null default FALSE" +
")"
c.ExecuteNonQuery()
End Using
@ -312,7 +330,7 @@ Friend Class GuildSettings
Using db = Await dbsettings.OpenConnectionAsync()
Using c = db.CreateCommand()
' Take note of ordinals for use in the constructor
c.CommandText = "select guild_id, role_id, channel_announce_id, time_zone, moderated, moderator_role, announce_message, announce_message_pl " +
c.CommandText = "select guild_id, role_id, channel_announce_id, time_zone, moderated, moderator_role, announce_message, announce_message_pl, announce_ping " +
$"from {BackingTable} where guild_id = @Gid"
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = guild
c.Prepare()
@ -349,7 +367,8 @@ Friend Class GuildSettings
"moderated = @Moderated, " +
"moderator_role = @ModRole, " +
"announce_message = @AnnounceMsg, " +
"announce_message_pl = @AnnounceMsgPl " +
"announce_message_pl = @AnnounceMsgPl, " +
"announce_ping = @AnnouncePing " +
"where guild_id = @Gid"
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = GuildId
With c.Parameters.Add("@RoleId", NpgsqlDbType.Bigint)
@ -395,6 +414,7 @@ Friend Class GuildSettings
.Value = DBNull.Value
End If
End With
c.Parameters.Add("@AnnouncePing", NpgsqlDbType.Boolean).Value = _announcePing
c.Prepare()
Await c.ExecuteNonQueryAsync()
End Using

View file

@ -68,6 +68,8 @@ Friend Class HelpInfoCommands
" » Sets the announcement channel. Leave blank to disable." + vbLf +
$"{mpfx}message (message)`, `{CommandPrefix}config messagepl (message)`" + vbLf +
$" » Sets a custom announcement message. See `{CommandPrefix}help-message`." + vbLf +
$"{mpfx}ping (off|on)`" + vbLf +
$" » Sets whether to ping the respective users in the announcement message." + vbLf +
$"{mpfx}zone (time zone name)`" + vbLf +
$" » Sets the default server time zone. See `{CommandPrefix}help-tzdata`."
}

View file

@ -25,6 +25,7 @@ Friend Class ManagerCommands
{"modrole", AddressOf ScmdModRole},
{"message", AddressOf ScmdAnnounceMsg},
{"messagepl", AddressOf ScmdAnnounceMsg},
{"ping", AddressOf ScmdPing},
{"zone", AddressOf ScmdZone},
{"block", AddressOf ScmdBlock},
{"unblock", AddressOf ScmdBlock},
@ -87,6 +88,33 @@ Friend Class ManagerCommands
End If
End Function
Private Async Function ScmdPing(param As String(), reqChannel As SocketTextChannel) As Task
Const inputErr = ":x: You must specify either `off` or `on` in this setting."
If param.Length <> 2 Then
Await reqChannel.SendMessageAsync(inputErr)
Return
End If
Dim input = param(1).ToLower()
Dim setting As Boolean
Dim result As String
If input = "off" Then
setting = False
result = ":white_check_mark: Announcement pings are now **off**."
ElseIf input = "on" Then
setting = True
result = ":white_check_mark: Announcement pings are now **on**."
Else
Await reqChannel.SendMessageAsync(inputErr)
Return
End If
SyncLock Instance.KnownGuilds
Instance.KnownGuilds(reqChannel.Guild.Id).UpdateAnnouncePingAsync(setting).Wait()
End SyncLock
Await reqChannel.SendMessageAsync(result)
End Function
' Announcement channel set
Private Async Function ScmdChannel(param As String(), reqChannel As SocketTextChannel) As Task
If param.Length = 1 Then