Add name display token; pluralized announce message

This commit is contained in:
Noikoio 2019-05-19 23:45:32 -07:00
parent 2f8859881c
commit 8e26a21b15
3 changed files with 47 additions and 23 deletions

View file

@ -97,13 +97,13 @@ Class BackgroundWorker
Dim users As IEnumerable(Of GuildUserSettings) Dim users As IEnumerable(Of GuildUserSettings)
Dim role As SocketRole = Nothing Dim role As SocketRole = Nothing
Dim channel As SocketTextChannel = Nothing Dim channel As SocketTextChannel = Nothing
Dim announceMsg As String Dim announce As (String, String)
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
announceMsg = gs.AnnounceMessage announce = gs.AnnounceMessages
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 +135,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(announceMsg, channel, announceNames) Await BirthdayAnnounceAsync(announce, channel, announceNames)
End If End If
Return announceNames.Count Return announceNames.Count
@ -208,7 +208,7 @@ Class BackgroundWorker
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
If c = "\" Or c = "_" Or c = "~" Or c = "*" Then If c = "\"c Or c = "_"c Or c = "~"c Or c = "*"c Then
result.Append("\") result.Append("\")
End If End If
result.Append(c) result.Append(c)
@ -227,21 +227,22 @@ Class BackgroundWorker
''' Makes (or attempts to make) an announcement in the specified channel that includes all users ''' Makes (or attempts to make) an announcement in the specified channel that includes all users
''' who have just had their birthday role added. ''' who have just had their birthday role added.
''' </summary> ''' </summary>
Private Async Function BirthdayAnnounceAsync(ByVal announceMsg As String, Private Async Function BirthdayAnnounceAsync(announce As (String, String),
c As SocketTextChannel, c As SocketTextChannel,
names As IEnumerable(Of SocketGuildUser)) As Task names As IEnumerable(Of SocketGuildUser)) As Task
Const DefaultAnnounce = "Please wish a happy birthday to %n!"
Const DefaultAnnouncePl = "Please wish a happy birthday to our esteemed members: %n"
Const DefaultAnnounce = "Please wish a happy birthday to our esteemed member(s):"
If c Is Nothing Then Return If c Is Nothing Then Return
' TODO streamline this whole thing once a customizable message is implemented. Dim announceMsg As String
' Plan: "{custommessage}\n{namedisplay}" If names.Count = 1 Then
Dim result As String announceMsg = If(announce.Item1, DefaultAnnounce)
Else
If announceMsg Is Nothing Then announceMsg = If(announce.Item2, DefaultAnnouncePl)
announceMsg = DefaultAnnounce
End If End If
announceMsg = announceMsg.TrimEnd() announceMsg = announceMsg.TrimEnd()
If Not announceMsg.Contains("%n") Then announceMsg += " %n"
' Build sorted name list ' Build sorted name list
Dim namestrings As New List(Of String) Dim namestrings As New List(Of String)
@ -259,10 +260,9 @@ Class BackgroundWorker
first = False first = False
namedisplay.Append(item) namedisplay.Append(item)
Next Next
result = announceMsg + " " + namedisplay.ToString()
Try Try
Await c.SendMessageAsync(result) Await c.SendMessageAsync(announceMsg.Replace("%n", namedisplay.ToString()))
Catch ex As Discord.Net.HttpException Catch ex As Discord.Net.HttpException
' Ignore ' Ignore
End Try End Try

View file

@ -17,6 +17,7 @@ Friend Class GuildSettings
Private _tz As String Private _tz As String
Private _moderated As Boolean Private _moderated As Boolean
Private _announceMsg As String Private _announceMsg As String
Private _announceMsgPl As String
Private _userCache As Dictionary(Of ULong, GuildUserSettings) Private _userCache As Dictionary(Of ULong, GuildUserSettings)
Private _roleWarning As Boolean Private _roleWarning As Boolean
@ -107,9 +108,9 @@ Friend Class GuildSettings
''' <summary> ''' <summary>
''' Gets the guild-specific birthday announcement message. ''' Gets the guild-specific birthday announcement message.
''' </summary> ''' </summary>
Public ReadOnly Property AnnounceMessage As String Public ReadOnly Property AnnounceMessages As (String, String)
Get Get
Return _announceMsg Return (_announceMsg, _announceMsgPl)
End Get End Get
End Property End Property
@ -129,6 +130,7 @@ Friend Class GuildSettings
_moderated = reader.GetBoolean(4) _moderated = reader.GetBoolean(4)
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))
' Get user information loaded up. ' Get user information loaded up.
Dim userresult = GuildUserSettings.GetGuildUsersAsync(dbconfig, GuildId) Dim userresult = GuildUserSettings.GetGuildUsersAsync(dbconfig, GuildId)
@ -269,6 +271,11 @@ Friend Class GuildSettings
Await UpdateDatabaseAsync() Await UpdateDatabaseAsync()
End Function End Function
Public Async Function UpdateAnnounceMessagePlAsync(messagePl As String) As Task
_announceMsgPl = messagePl
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"
@ -282,7 +289,8 @@ Friend Class GuildSettings
"time_zone text null, " + "time_zone text null, " +
"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" +
")" ")"
c.ExecuteNonQuery() c.ExecuteNonQuery()
End Using End Using
@ -304,7 +312,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 " + c.CommandText = "select guild_id, role_id, channel_announce_id, time_zone, moderated, moderator_role, announce_message, announce_message_pl " +
$"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()
@ -340,7 +348,8 @@ Friend Class GuildSettings
"time_zone = @TimeZone, " + "time_zone = @TimeZone, " +
"moderated = @Moderated, " + "moderated = @Moderated, " +
"moderator_role = @ModRole, " + "moderator_role = @ModRole, " +
"announce_message = @AnnounceMsg " + "announce_message = @AnnounceMsg, " +
"announce_message_pl = @AnnounceMsgPl " +
"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)
@ -373,8 +382,15 @@ Friend Class GuildSettings
End If End If
End With End With
With c.Parameters.Add("@AnnounceMsg", NpgsqlDbType.Text) With c.Parameters.Add("@AnnounceMsg", NpgsqlDbType.Text)
If AnnounceMessage IsNot Nothing Then If _announceMsg IsNot Nothing Then
.Value = AnnounceMessage .Value = _announceMsg
Else
.Value = DBNull.Value
End If
End With
With c.Parameters.Add("@AnnounceMsgPl", NpgsqlDbType.Text)
If _announceMsgPl IsNot Nothing Then
.Value = _announceMsgPl
Else Else
.Value = DBNull.Value .Value = DBNull.Value
End If End If

View file

@ -24,6 +24,7 @@ Friend Class ManagerCommands
{"channel", AddressOf ScmdChannel}, {"channel", AddressOf ScmdChannel},
{"modrole", AddressOf ScmdModRole}, {"modrole", AddressOf ScmdModRole},
{"message", AddressOf ScmdAnnounceMsg}, {"message", AddressOf ScmdAnnounceMsg},
{"messagepl", AddressOf ScmdAnnounceMsg},
{"zone", AddressOf ScmdZone}, {"zone", AddressOf ScmdZone},
{"block", AddressOf ScmdBlock}, {"block", AddressOf ScmdBlock},
{"unblock", AddressOf ScmdBlock}, {"unblock", AddressOf ScmdBlock},
@ -269,10 +270,17 @@ Friend Class ManagerCommands
Return Return
End If End If
Dim plural = param(0).ToLower().EndsWith("pl")
SyncLock Instance.KnownGuilds SyncLock Instance.KnownGuilds
Instance.KnownGuilds(reqChannel.Guild.Id).UpdateAnnounceMessageAsync(param(1)).Wait() If plural Then
Instance.KnownGuilds(reqChannel.Guild.Id).UpdateAnnounceMessagePlAsync(param(1)).Wait()
Else
Instance.KnownGuilds(reqChannel.Guild.Id).UpdateAnnounceMessageAsync(param(1)).Wait()
End If
End SyncLock End SyncLock
Await reqChannel.SendMessageAsync(":white_check_mark: The birthday announcement message has been updated.") Dim report = $":white_check_mark: The {If(plural, "plural", "singular")} birthday announcement message has been updated."
Await reqChannel.SendMessageAsync(report)
End Function End Function
#End Region #End Region