using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; namespace Noikoio.RegexBot.Module.ModLogs { /// /// ModLogs guild-specific configuration values. /// class GuildConfig { // Event reporting private readonly EntityName _rptTarget; private EventType _rptTypes; /// /// Target reporting channel. /// public EntityName? RptTarget => _rptTarget; /// /// Event types to send to the reporting channel. /// public EventType RptTypes => _rptTypes; // Query command private readonly string _qCmd; // command name private readonly EntityList _qAccess; // list of those able to issue the command private readonly EventType _qDefaultAnswer; // default entry types to display /// /// Query command. The first word in an incoming message, including prefix, that triggers a query. /// public string QrCommand => _qCmd; /// /// List of users permitted to invoke the query command. /// If null, refer to the guild's Moderators list. /// public EntityList QrPermittedUsers => _qAccess; /// /// Event types to display in a query. /// public EventType QrTypes => _qDefaultAnswer; public GuildConfig(JObject cfgRoot) { // AutoReporting settings var arcfg = cfgRoot["AutoReporting"]; if (arcfg == null) { _rptTarget = default(EntityName); // NOTE: Change this if EntityName becomes a class later _rptTypes = EventType.None; } else if (arcfg.Type == JTokenType.Object) { string chval = arcfg["Channel"]?.Value(); if (chval == null) throw new RuleImportException("Reporting channel is not defined."); if (!string.IsNullOrWhiteSpace(chval) && chval[0] == '#') _rptTarget = new EntityName(chval.Substring(1, chval.Length-1), EntityType.Channel); else throw new RuleImportException("Reporting channel is not properly defined."); // Require the channel's ID for now. if (!_rptTarget.Id.HasValue) throw new RuleImportException("Reporting channel's ID must be specified."); // TODO make optional string rpval = arcfg["Events"]?.Value(); _rptTypes = GetTypesFromString(rpval); } else { throw new RuleImportException("Section for AutoReporting is not correctly defined."); } // QueryCommand settings var qccfg = cfgRoot["QueryCommand"]; if (qccfg == null) { _qCmd = null; _qAccess = null; _qDefaultAnswer = EventType.None; } else if (arcfg.Type == JTokenType.Object) { _qCmd = arcfg["Command"]?.Value(); if (string.IsNullOrWhiteSpace(_qCmd)) throw new RuleImportException("Query command option must have a value."); if (_qCmd.Contains(" ")) throw new RuleImportException("Query command must not contain spaces."); var acl = arcfg["AllowedUsers"]; if (acl == null) _qAccess = null; else _qAccess = new EntityList(acl); // TODO make optional string ansval = arcfg["DefaultEvents"]?.Value(); _qDefaultAnswer = GetTypesFromString(ansval); } else { throw new RuleImportException("Section for QueryCommand is not correctly defined."); } } public static EventType GetTypesFromString(string input) { if (string.IsNullOrWhiteSpace(input)) throw new RuleImportException("Types are not properly defined."); var strTypes = input.Split( new char[] { ' ', ',', '/', '+' }, // and more? StringSplitOptions.RemoveEmptyEntries); EventType endResult = EventType.None; foreach (var item in strTypes) { try { var result = Enum.Parse(item, true); endResult |= result; } catch (ArgumentException) { throw new RuleImportException($"Unable to determine the given event type \"{item}\""); } } return endResult; } } }