From 25901b0d2e8d759825889d4d900c911ad3d90570 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Sun, 2 Jun 2019 17:45:25 -0700 Subject: [PATCH] Create Common.vb --- BirthdayBot/BackgroundWorker.vb | 23 +---------------------- BirthdayBot/Common.vb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 BirthdayBot/Common.vb diff --git a/BirthdayBot/BackgroundWorker.vb b/BirthdayBot/BackgroundWorker.vb index 56bd0cd..1c22e78 100644 --- a/BirthdayBot/BackgroundWorker.vb +++ b/BirthdayBot/BackgroundWorker.vb @@ -205,27 +205,6 @@ Class BackgroundWorker Return newBirthdays End Function - 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 - If c = "\"c Or c = "_"c Or c = "~"c Or c = "*"c Then - result.Append("\") - End If - result.Append(c) - Next - Return result.ToString() - End Function - - Dim username = escapeFormattingCharacters(member.Username) - If member.Nickname IsNot Nothing Then - Return $"**{escapeFormattingCharacters(member.Nickname)}** ({username}#{member.Discriminator})" - End If - Return $"**{username}**#{member.Discriminator}" - End Function - Public Const DefaultAnnounce = "Please wish a happy birthday to %n!" Public Const DefaultAnnouncePl = "Please wish a happy birthday to our esteemed members: %n" @@ -251,7 +230,7 @@ Class BackgroundWorker ' Build sorted name list Dim namestrings As New List(Of String) For Each item In names - namestrings.Add(BirthdayAnnounceFormatName(item, announcePing)) + namestrings.Add(FormatName(item, announcePing)) Next namestrings.Sort(StringComparer.OrdinalIgnoreCase) diff --git a/BirthdayBot/Common.vb b/BirthdayBot/Common.vb new file mode 100644 index 0000000..dc1bdac --- /dev/null +++ b/BirthdayBot/Common.vb @@ -0,0 +1,33 @@ +Imports System.Text +Imports Discord.WebSocket + +Module Common + ''' + ''' Formats a user's name to a consistent, readable format which makes use of their nickname. + ''' + Public Function FormatName(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 + If c = "\"c Or c = "_"c Or c = "~"c Or c = "*"c Then + result.Append("\") + End If + result.Append(c) + Next + Return result.ToString() + End Function + + Dim username = escapeFormattingCharacters(member.Username) + If member.Nickname IsNot Nothing Then + Return $"**{escapeFormattingCharacters(member.Nickname)}** ({username}#{member.Discriminator})" + End If + Return $"**{username}**#{member.Discriminator}" + End Function + + Public ReadOnly Property MonthNames As New Dictionary(Of Integer, String) From { + {1, "Jan"}, {2, "Feb"}, {3, "Mar"}, {4, "Apr"}, {5, "May"}, {6, "Jun"}, + {7, "Jul"}, {8, "Aug"}, {9, "Sep"}, {10, "Oct"}, {11, "Nov"}, {12, "Dec"} + } +End Module