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

View file

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

View file

@ -68,6 +68,8 @@ Friend Class HelpInfoCommands
" » Sets the announcement channel. Leave blank to disable." + vbLf + " » Sets the announcement channel. Leave blank to disable." + vbLf +
$"{mpfx}message (message)`, `{CommandPrefix}config messagepl (message)`" + vbLf + $"{mpfx}message (message)`, `{CommandPrefix}config messagepl (message)`" + vbLf +
$" » Sets a custom announcement message. See `{CommandPrefix}help-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 + $"{mpfx}zone (time zone name)`" + vbLf +
$" » Sets the default server time zone. See `{CommandPrefix}help-tzdata`." $" » Sets the default server time zone. See `{CommandPrefix}help-tzdata`."
} }

View file

@ -25,6 +25,7 @@ Friend Class ManagerCommands
{"modrole", AddressOf ScmdModRole}, {"modrole", AddressOf ScmdModRole},
{"message", AddressOf ScmdAnnounceMsg}, {"message", AddressOf ScmdAnnounceMsg},
{"messagepl", AddressOf ScmdAnnounceMsg}, {"messagepl", AddressOf ScmdAnnounceMsg},
{"ping", AddressOf ScmdPing},
{"zone", AddressOf ScmdZone}, {"zone", AddressOf ScmdZone},
{"block", AddressOf ScmdBlock}, {"block", AddressOf ScmdBlock},
{"unblock", AddressOf ScmdBlock}, {"unblock", AddressOf ScmdBlock},
@ -87,6 +88,33 @@ Friend Class ManagerCommands
End If End If
End Function 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 ' Announcement channel set
Private Async Function ScmdChannel(param As String(), reqChannel As SocketTextChannel) As Task Private Async Function ScmdChannel(param As String(), reqChannel As SocketTextChannel) As Task
If param.Length = 1 Then If param.Length = 1 Then