From 757ba7476ee03b32523f31744c0dc38457d4e832 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Mon, 27 May 2019 22:28:10 -0700 Subject: [PATCH] Add announce message ping option --- BirthdayBot/BackgroundWorker.vb | 12 +++++--- BirthdayBot/Data/GuildSettings.vb | 26 +++++++++++++++-- BirthdayBot/UserInterface/HelpInfoCommands.vb | 2 ++ BirthdayBot/UserInterface/ManagerCommands.vb | 28 +++++++++++++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/BirthdayBot/BackgroundWorker.vb b/BirthdayBot/BackgroundWorker.vb index 212001c..56bd0cd 100644 --- a/BirthdayBot/BackgroundWorker.vb +++ b/BirthdayBot/BackgroundWorker.vb @@ -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. ''' 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) diff --git a/BirthdayBot/Data/GuildSettings.vb b/BirthdayBot/Data/GuildSettings.vb index 95b641e..7dccbef 100644 --- a/BirthdayBot/Data/GuildSettings.vb +++ b/BirthdayBot/Data/GuildSettings.vb @@ -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 + ''' + ''' Gets whether to ping users in the announcement message instead of displaying their names. + ''' + ''' + 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 diff --git a/BirthdayBot/UserInterface/HelpInfoCommands.vb b/BirthdayBot/UserInterface/HelpInfoCommands.vb index 91e8161..299bac5 100644 --- a/BirthdayBot/UserInterface/HelpInfoCommands.vb +++ b/BirthdayBot/UserInterface/HelpInfoCommands.vb @@ -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`." } diff --git a/BirthdayBot/UserInterface/ManagerCommands.vb b/BirthdayBot/UserInterface/ManagerCommands.vb index 980e3e6..9b886c1 100644 --- a/BirthdayBot/UserInterface/ManagerCommands.vb +++ b/BirthdayBot/UserInterface/ManagerCommands.vb @@ -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