Skip to content
32 changes: 30 additions & 2 deletions DiscordBot/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

public static string GetSha256(this string input)
{
var hash = new SHA256CryptoServiceProvider();

Check warning on line 106 in DiscordBot/Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test

'SHA256CryptoServiceProvider' is obsolete: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'
// Convert the input string to a byte array and compute the hash.
var data = hash.ComputeHash(Encoding.UTF8.GetBytes(input));

Expand All @@ -122,7 +122,7 @@
/// <summary>
/// Returns true if the string contains only upper case characters, including spaces and all punctuation ie; "I NEED HELP!?!?!?!#$?!" will return true
/// </summary>
public static bool IsAlLCaps(this string str)
public static bool IsAllCaps(this string str)
{
return Regex.IsMatch(str, @"^[A-Z\s\p{P}]+$");
}
Expand All @@ -133,4 +133,32 @@
return string.Empty;
return char.ToUpper(str[0]) + str[1..];
}
}

/// <summary>
/// Joins a list of strings with commas into a natural phrase:
/// "apples"
/// "apples and bananas"
/// "apples, bananas, and cherries"
/// </summary>
/// <param name="nouns">array or list of element phrases to be listed</param>
/// <param name="conj">final conjunction; defaults to "and" if not given</param>
public static string ToCommaList(this string[] nouns, string conj=null)
{
if (conj == null)
conj = "and";
var sb = new StringBuilder();
for (int i = 0; i < nouns.Length; i++)
{
if (i > 0)
{
if (nouns.Length > 2)
sb.Append(',');
sb.Append(' ');
if (i == nouns.Length-1)
sb.Append(conj).Append(' ');
}
sb.Append(nouns[i]);
}
return sb.ToString();
}
}
18 changes: 17 additions & 1 deletion DiscordBot/Extensions/UserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ public static string GetUserLoggingString(this IUser user)
return $"{user.Username} `{user.Id}`";
return $"{guildUser.GetPreferredAndUsername()} `{guildUser.Id}`";
}
}

public static string[] ToUserPreferredNameArray(this IUser[] users)
{
var names = new string[users.Length];
for (int i = 0; i < users.Length; i++)
names[i] = GetUserPreferredName(users[i]);
return names;
}

public static string[] ToMentionArray(this IUser[] users)
{
var mentions = new string[users.Length];
for (int i = 0; i < users.Length; i++)
mentions[i] = users[i].Mention;
return mentions;
}
}
32 changes: 29 additions & 3 deletions DiscordBot/Modules/UserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,36 @@ public async Task SlapUser(params IUser[] users)
var sb = new StringBuilder();

var uname = Context.User.GetUserPreferredName();
sb.Append($"**{uname}** slaps ");
foreach (var user in users) sb.Append(user.Mention).Append(" ");

sb.Append("around a bit with a large ").Append(Settings.UserModuleSlapChoices[_random.Next() % Settings.UserModuleSlapChoices.Count]).Append(".");
if (Settings.UserModuleSlapChoices == null || Settings.UserModuleSlapChoices.Count == 0)
Settings.UserModuleSlapChoices = new List<string>() { "fish", "mallet" };
if (Settings.UserModuleSlapFails == null || Settings.UserModuleSlapFails.Count == 0)
Settings.UserModuleSlapFails = new List<string>() { "hurting themselves" };

bool fail = (_random.Next(1, 100) < 5);

if (fail)
sb.Append($"**{uname}** tries to slap ");
else
sb.Append($"**{uname}** slaps ");

var mentions = users.ToMentionArray();
sb.Append(mentions.ToCommaList());

if (fail)
{
sb.Append(" around a bit with a large ");
sb.Append(Settings.UserModuleSlapChoices[_random.Next() % Settings.UserModuleSlapChoices.Count]);
sb.Append(", but misses and ends up ");
sb.Append(Settings.UserModuleSlapFails[_random.Next() % Settings.UserModuleSlapFails.Count]);
sb.Append(".");
}
else
{
sb.Append(" around a bit with a large ");
sb.Append(Settings.UserModuleSlapChoices[_random.Next() % Settings.UserModuleSlapChoices.Count]);
sb.Append(".");
}

await Context.Channel.SendMessageAsync(sb.ToString());
await Context.Message.DeleteAfterSeconds(seconds: 1);
Expand Down
4 changes: 2 additions & 2 deletions DiscordBot/Services/UnityHelp/UnityHelpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private async Task OnThreadCreated(SocketThreadChannel thread)
}

var threadTitle = thread.Name;
if (threadTitle.IsAlLCaps())
if (threadTitle.IsAllCaps())
{
threadTitle = thread.Name.ToLower();
}
Expand Down Expand Up @@ -750,4 +750,4 @@ public int GetTrackedQuestionCount()

#endregion // Utility Methods

}
}
8 changes: 7 additions & 1 deletion DiscordBot/Settings/Deserialized/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public class BotSettings

#region Fun Commands

public List<string> UserModuleSlapChoices { get; set; } = new List<string>() { "trout", "duck", "truck", "paddle", "magikarp", "sausage", "student loan", "life choice", "bug report", "unhandled exception", "null pointer", "keyboard", "cheese wheel", "banana peel", "unresolved bug", "low poly donut" };
//NOTE: Deserializer will not override a List<string> from the file if a default one is made here.
public List<string> UserModuleSlapChoices { get; set; }
// = { "trout", "duck", "truck", "paddle", "magikarp", "sausage", "student loan",
// "life choice", "bug report", "unhandled exception", "null pointer", "keyboard",
// "cheese wheel", "banana peel", "unresolved bug", "low poly donut" };
public List<string> UserModuleSlapFails { get; set; }
// = { "hurting themselves" };

#endregion // Fun Commands

Expand Down
Loading