diff --git a/DiscordBot/Data/FuzzTable.cs b/DiscordBot/Data/FuzzTable.cs index 05e0bc6a..8ac2ff14 100644 --- a/DiscordBot/Data/FuzzTable.cs +++ b/DiscordBot/Data/FuzzTable.cs @@ -57,9 +57,12 @@ public void Add(IEnumerable stream) // Lines starting with a '#' character are ignored, as are blank lines. // Each remaining line of the file is trimmed of leading and trailing whitespace. // Each line is added as a new choice, and duplicates are allowed for weighting. + // If the file is missing, nothing is done, but any other exception is thrown. // public void Load(string filename) { + if (!File.Exists(filename)) + return; foreach (string line in File.ReadLines(filename)) { string choice = line.Trim(); diff --git a/DiscordBot/Modules/UserModule.cs b/DiscordBot/Modules/UserModule.cs index 492a7410..d58924e0 100644 --- a/DiscordBot/Modules/UserModule.cs +++ b/DiscordBot/Modules/UserModule.cs @@ -623,10 +623,16 @@ public async Task DisableCodeTips() [Summary("Slap the specified user(s). Syntax : !slap @user1 [@user2 @user3...]")] public async Task SlapUser(params IUser[] users) { - var sb = new StringBuilder(); - - var uname = Context.User.GetUserPreferredName(); - + try + { + if (_slapObjects.Count == 0) + _slapObjects.Load(Settings.UserModuleSlapObjectsTable); + } + catch (Exception e) + { + await LoggingService.LogChannelAndFile($"Error while loading '{Settings.UserModuleSlapObjectsTable}'.\nEx:{e}", + ExtendedLogSeverity.LowWarning); + } if (_slapObjects.Count == 0) _slapObjects.Add(Settings.UserModuleSlapChoices); if (_slapObjects.Count == 0) @@ -637,35 +643,25 @@ public async Task SlapUser(params IUser[] users) if (_slapFails.Count == 0) _slapFails.Add("hurting themselves"); - // if (Settings.UserModuleSlapChoices == null || Settings.UserModuleSlapChoices.Count == 0) - // Settings.UserModuleSlapChoices = new List() { "fish", "mallet" }; - // if (Settings.UserModuleSlapFails == null || Settings.UserModuleSlapFails.Count == 0) - // Settings.UserModuleSlapFails = new List() { "hurting themselves" }; + var sb = new StringBuilder(); + var uname = Context.User.GetUserPreferredName(); + var mentions = users.ToMentionArray().ToCommaList(); 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($"**{uname}** tries to slap {mentions} "); sb.Append(" around a bit with a large "); sb.Append(_slapObjects.Pick(true)); - //sb.Append(Settings.UserModuleSlapChoices[_random.Next() % Settings.UserModuleSlapChoices.Count]); sb.Append(", but misses and ends up "); sb.Append(_slapFails.Pick(true)); - //sb.Append(Settings.UserModuleSlapFails[_random.Next() % Settings.UserModuleSlapFails.Count]); sb.Append("."); } else { + sb.Append($"**{uname}** slaps {mentions} "); sb.Append(" around a bit with a large "); - sb.Append(Settings.UserModuleSlapChoices[_random.Next() % Settings.UserModuleSlapChoices.Count]); + sb.Append(_slapObjects.Pick(true)); sb.Append("."); } diff --git a/DiscordBot/Settings/Deserialized/Settings.cs b/DiscordBot/Settings/Deserialized/Settings.cs index 5d98d839..19dbff60 100644 --- a/DiscordBot/Settings/Deserialized/Settings.cs +++ b/DiscordBot/Settings/Deserialized/Settings.cs @@ -25,7 +25,9 @@ public class BotSettings #region Fun Commands - //NOTE: Deserializer will not override a List from the file if a default one is made here. + public string UserModuleSlapObjectsTable { get; set; } = null; + // = "udc-slap.txt" + //NOTE: Deserializer will not override a List from the json if a default one is made here. public List UserModuleSlapChoices { get; set; } // = { "trout", "duck", "truck", "paddle", "magikarp", "sausage", "student loan", // "life choice", "bug report", "unhandled exception", "null pointer", "keyboard",