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

View file

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

View file

@ -24,6 +24,7 @@ Friend Class ManagerCommands
{"channel", AddressOf ScmdChannel},
{"modrole", AddressOf ScmdModRole},
{"message", AddressOf ScmdAnnounceMsg},
{"messagepl", AddressOf ScmdAnnounceMsg},
{"zone", AddressOf ScmdZone},
{"block", AddressOf ScmdBlock},
{"unblock", AddressOf ScmdBlock},
@ -269,10 +270,17 @@ Friend Class ManagerCommands
Return
End If
Dim plural = param(0).ToLower().EndsWith("pl")
SyncLock Instance.KnownGuilds
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
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 Region