From 1e2e615a239d6aaa9a36a62aa25c8e92301c344e Mon Sep 17 00:00:00 2001 From: Noikoio Date: Sat, 21 Oct 2017 13:14:51 -0700 Subject: [PATCH] Require Guild ID in server definition Due to how guild configuration is retrieved by features, it's easier to have the ID be defined explicitly in server configuration instead of having to look it up each time it's necessary. --- ConfigItem/ServerConfig.cs | 16 +++++----------- Configuration.cs | 26 +++++++------------------- Feature/AutoRespond/AutoRespond.cs | 3 +-- docs/docs/serverdef.md | 5 ++--- 4 files changed, 15 insertions(+), 35 deletions(-) diff --git a/ConfigItem/ServerConfig.cs b/ConfigItem/ServerConfig.cs index d1b52bb..88fe65f 100644 --- a/ConfigItem/ServerConfig.cs +++ b/ConfigItem/ServerConfig.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.Diagnostics; namespace Noikoio.RegexBot.ConfigItem @@ -9,25 +8,20 @@ namespace Noikoio.RegexBot.ConfigItem /// class ServerConfig { - private readonly string _name; - private ulong? _id; + private readonly ulong _id; private EntityList _moderators; private ReadOnlyDictionary _featureData; - public string Name => _name; - public ulong? Id { - get => _id; set { if (!_id.HasValue) _id = value; } - } + public ulong? Id => _id; public EntityList Moderators => _moderators; public ReadOnlyDictionary FeatureConfigs => _featureData; - public ServerConfig(string name, ulong? id, EntityList moderators, ReadOnlyDictionary featureconf) + public ServerConfig(ulong id, EntityList moderators, ReadOnlyDictionary featureconf) { - _name = name; _id = id; _moderators = moderators; _featureData = featureconf; - Debug.Assert(_name != null && _moderators != null); + Debug.Assert(_moderators != null && _featureData != null); } } } diff --git a/Configuration.cs b/Configuration.cs index dc739f4..ab3fa5b 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -117,28 +117,16 @@ namespace Noikoio.RegexBot foreach (JObject sconf in conf["servers"].Children()) { // Server name - if (sconf["name"] == null || string.IsNullOrWhiteSpace(sconf["name"].Value())) + //if (sconf["id"] == null || sconf["id"].Type != JTokenType.Integer)) + if (sconf["id"] == null) { - await Log("Error: Server definition is missing a name."); + await Log("Error: Server ID is missing within definition."); return false; } - string snamestr = sconf["name"].Value(); - string sname; - ulong? sid; - - int snseparator = snamestr.IndexOf("::"); - if (ulong.TryParse(snamestr.Substring(0, snseparator), out var id)) - { - sid = id; - sname = snamestr.Substring(snseparator + 2); - } - else - { - sid = null; - sname = snamestr; - } + ulong sid = sconf["id"].Value(); + string sname = sconf["name"]?.Value(); - var SLog = Logger.GetLogger(LogPrefix + "/" + sname); + var SLog = Logger.GetLogger(LogPrefix + "/" + (sname ?? sid.ToString())); // Load server moderator list EntityList mods = new EntityList(sconf["moderators"]); @@ -180,7 +168,7 @@ namespace Noikoio.RegexBot // Switch to using new data List> rulesfinal = new List>(); - newservers.Add(new ServerConfig(sname, sid, mods, new ReadOnlyDictionary(customConfs))); + newservers.Add(new ServerConfig(sid, mods, new ReadOnlyDictionary(customConfs))); } _servers = newservers.ToArray(); diff --git a/Feature/AutoRespond/AutoRespond.cs b/Feature/AutoRespond/AutoRespond.cs index 2de818c..f5afc65 100644 --- a/Feature/AutoRespond/AutoRespond.cs +++ b/Feature/AutoRespond/AutoRespond.cs @@ -34,8 +34,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond // Determine channel type - if not a guild channel, stop. var ch = arg.Channel as SocketGuildChannel; if (ch == null) return; - - // TODO either search server by name or remove server name support entirely + var defs = GetConfig(ch.Guild.Id) as IEnumerable; if (defs == null) return; diff --git a/docs/docs/serverdef.md b/docs/docs/serverdef.md index cfa9117..0e68ec7 100644 --- a/docs/docs/serverdef.md +++ b/docs/docs/serverdef.md @@ -3,9 +3,8 @@ Server definitions are defined within the `servers` array. Each definition represents unique configuration for a single server. Defining multiple servers allows for a single bot instance to be uniquely configured for use in several servers at once. The following is a list of accepted members within a server definition. -* name *(string)* - **Required.** A string containing the server ID, and optionally the name, of the server that this definition represents. - * If you wish to enter both a name and ID, you must first enter the ID, followed by two colon (:) characters, followed by the name. - * For example: `"285450825525927585::My Testing Server"` +* id *(integer)* - **Required.** A value containing the server ID, and optionally the name, of the server that this definition represents. +* name *(string)* - The server name. Only used during configuration (re)load to make logs more readable. * moderators *[(entity list)](entitylist.html)* - A list of entities to consider as moderators. Actions done by the members of those in this list are able to execute *ModTools* commands and are exempt from certain *AutoMod* rules if a particular rule has its *AllowModBypass* setting set to *false*. * [AutoMod](automod.html) *(name/value pairs)* - Auto-moderation matching and response definitions. * [AutoResponses](autorespond.html) *(name/value pairs)* - Definitions for automatic responses.