Renamed Features to Modules

This commit is contained in:
Noikoio 2017-11-11 19:12:24 -08:00
parent 17037700dd
commit 801292e1a6
24 changed files with 61 additions and 64 deletions

View file

@ -7,12 +7,9 @@ using System.Threading.Tasks;
namespace Noikoio.RegexBot namespace Noikoio.RegexBot
{ {
/// <summary> /// <summary>
/// Base class for bot features /// Base class for bot modules
/// </summary> /// </summary>
/// <remarks> abstract class BotModule
/// This may have use in some sort of external plugin system later.
/// </remarks>
abstract class BotFeature
{ {
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private readonly AsyncLogger _logger; private readonly AsyncLogger _logger;
@ -20,20 +17,20 @@ namespace Noikoio.RegexBot
public abstract string Name { get; } public abstract string Name { get; }
protected DiscordSocketClient Client => _client; protected DiscordSocketClient Client => _client;
public BotFeature(DiscordSocketClient client) public BotModule(DiscordSocketClient client)
{ {
_client = client; _client = client;
_logger = Logger.GetLogger(this.Name); _logger = Logger.GetLogger(this.Name);
} }
/// <summary> /// <summary>
/// Processes feature-specific configuration. /// Processes module-specific configuration.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Feature code <i>should not</i> hold on to this data, but instead use <see cref="GetConfig(ulong)"/> to retrieve /// Module code <i>should not</i> hold on to this data, but instead use <see cref="GetConfig(ulong)"/> to retrieve
/// them. This is in the event that configuration is reverted to an earlier state and allows for the /// them. This is in the event that configuration is reverted to an earlier state and allows for the
/// bot and all features to revert to previously used configuration values with no effort on the part /// all modules to revert to previously used configuration values with no effort on the part of the
/// of individual features. /// module code itself.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// Processed configuration data prepared for later use. /// Processed configuration data prepared for later use.
@ -45,7 +42,7 @@ namespace Noikoio.RegexBot
public abstract Task<object> ProcessConfiguration(JToken configSection); public abstract Task<object> ProcessConfiguration(JToken configSection);
/// <summary> /// <summary>
/// Gets this feature's relevant configuration data associated with the given Discord guild. /// Gets this module's relevant configuration data associated with the given Discord guild.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// The stored configuration data, or null if none exists. /// The stored configuration data, or null if none exists.
@ -58,7 +55,7 @@ namespace Noikoio.RegexBot
throw new ArgumentException("There is no known configuration associated with the given Guild ID."); throw new ArgumentException("There is no known configuration associated with the given Guild ID.");
} }
if (sc.FeatureConfigs.TryGetValue(this, out var item)) return item; if (sc.ModuleConfigs.TryGetValue(this, out var item)) return item;
else return null; else return null;
} }
@ -88,7 +85,7 @@ namespace Noikoio.RegexBot
/// <summary> /// <summary>
/// Indicates which section under an individual Discord guild configuration should be passed to the /// Indicates which section under an individual Discord guild configuration should be passed to the
/// feature's <see cref="BotFeature.ProcessConfiguration(JToken)"/> method during configuration load. /// module's <see cref="BotModule.ProcessConfiguration(JToken)"/> method during configuration load.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)] [AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class ConfigSectionAttribute : Attribute public class ConfigSectionAttribute : Attribute

View file

@ -14,9 +14,9 @@ namespace Noikoio.RegexBot.ConfigItem
private readonly string _parsemsg; private readonly string _parsemsg;
/// <summary> /// <summary>
/// Gets whether database features are enabled. /// Gets whether database storage is available.
/// </summary> /// </summary>
public bool Enabled => _enabled; public bool Available => _enabled;
/// <summary> /// <summary>
/// Constructor error message (only if not enabled) /// Constructor error message (only if not enabled)
/// </summary> /// </summary>
@ -49,7 +49,7 @@ namespace Noikoio.RegexBot.ConfigItem
public async Task<NpgsqlConnection> GetOpenConnectionAsync() public async Task<NpgsqlConnection> GetOpenConnectionAsync()
{ {
if (!Enabled) return null; if (!Available) return null;
var cs = new NpgsqlConnectionStringBuilder() var cs = new NpgsqlConnectionStringBuilder()
{ {

View file

@ -10,18 +10,18 @@ namespace Noikoio.RegexBot.ConfigItem
{ {
private readonly ulong _id; private readonly ulong _id;
private EntityList _moderators; private EntityList _moderators;
private ReadOnlyDictionary<BotFeature, object> _featureData; private ReadOnlyDictionary<BotModule, object> _modData;
public ulong? Id => _id; public ulong? Id => _id;
public EntityList Moderators => _moderators; public EntityList Moderators => _moderators;
public ReadOnlyDictionary<BotFeature, object> FeatureConfigs => _featureData; public ReadOnlyDictionary<BotModule, object> ModuleConfigs => _modData;
public ServerConfig(ulong id, EntityList moderators, ReadOnlyDictionary<BotFeature, object> featureconf) public ServerConfig(ulong id, EntityList moderators, ReadOnlyDictionary<BotModule, object> modconf)
{ {
_id = id; _id = id;
_moderators = moderators; _moderators = moderators;
_featureData = featureconf; _modData = modconf;
Debug.Assert(_moderators != null && _featureData != null); Debug.Assert(_moderators != null && _modData != null);
} }
} }
} }

View file

@ -137,9 +137,9 @@ namespace Noikoio.RegexBot
EntityList mods = new EntityList(sconf["moderators"]); EntityList mods = new EntityList(sconf["moderators"]);
if (sconf["moderators"] != null) await SLog("Moderator " + mods.ToString()); if (sconf["moderators"] != null) await SLog("Moderator " + mods.ToString());
// Load feature configurations // Load module configurations
Dictionary<BotFeature, object> customConfs = new Dictionary<BotFeature, object>(); Dictionary<BotModule, object> customConfs = new Dictionary<BotModule, object>();
foreach (var item in _bot.Features) foreach (var item in _bot.Modules)
{ {
var attr = item.GetType().GetTypeInfo() var attr = item.GetType().GetTypeInfo()
.GetMethod("ProcessConfiguration").GetCustomAttribute<ConfigSectionAttribute>(); .GetMethod("ProcessConfiguration").GetCustomAttribute<ConfigSectionAttribute>();
@ -173,7 +173,7 @@ namespace Noikoio.RegexBot
// Switch to using new data // Switch to using new data
List<Tuple<Regex, string[]>> rulesfinal = new List<Tuple<Regex, string[]>>(); List<Tuple<Regex, string[]>> rulesfinal = new List<Tuple<Regex, string[]>>();
newservers.Add(new ServerConfig(sid, mods, new ReadOnlyDictionary<BotFeature, object>(customConfs))); newservers.Add(new ServerConfig(sid, mods, new ReadOnlyDictionary<BotModule, object>(customConfs)));
} }
_servers = newservers.ToArray(); _servers = newservers.ToArray();

View file

@ -4,7 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod namespace Noikoio.RegexBot.Module.AutoMod
{ {
/// <summary> /// <summary>
/// Implements per-message regex matching and executes customizable responses. /// Implements per-message regex matching and executes customizable responses.
@ -14,7 +14,7 @@ namespace Noikoio.RegexBot.Feature.AutoMod
/// Strictly for use as a moderation tool only. Triggers that simply reply to messages /// Strictly for use as a moderation tool only. Triggers that simply reply to messages
/// should be implemented using <see cref="AutoRespond"/>. /// should be implemented using <see cref="AutoRespond"/>.
/// </remarks> /// </remarks>
class AutoMod : BotFeature class AutoMod : BotModule
{ {
public override string Name => "AutoMod"; public override string Name => "AutoMod";

View file

@ -8,7 +8,7 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod namespace Noikoio.RegexBot.Module.AutoMod
{ {
/// <summary> /// <summary>
/// Representation of a single AutoMod rule. /// Representation of a single AutoMod rule.

View file

@ -7,7 +7,7 @@ using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod namespace Noikoio.RegexBot.Module.AutoMod
{ {
/// <summary> /// <summary>
/// Base class for all Response classes. /// Base class for all Response classes.

View file

@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Bans the invoking user. /// Bans the invoking user.

View file

@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Kicks the invoking user. /// Kicks the invoking user.

View file

@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Removes the invoking message. /// Removes the invoking message.

View file

@ -5,7 +5,7 @@ using System;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Sends a summary of the invoking message, along with information /// Sends a summary of the invoking message, along with information

View file

@ -4,7 +4,7 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Manipulates a given user's role. /// Manipulates a given user's role.

View file

@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses namespace Noikoio.RegexBot.Module.AutoMod.Responses
{ {
/// <summary> /// <summary>
/// Sends a message to the given target. /// Sends a message to the given target.

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoRespond namespace Noikoio.RegexBot.Module.AutoRespond
{ {
/// <summary> /// <summary>
/// Similar to <see cref="AutoMod"/>, but lightweight. /// Similar to <see cref="AutoMod"/>, but lightweight.
@ -19,9 +19,9 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
/// </list> /// </list>
/// </para> /// </para>
/// </summary> /// </summary>
partial class AutoRespond : BotFeature partial class AutoRespond : BotModule
{ {
#region BotFeature implementation #region BotModule implementation
public override string Name => "AutoRespond"; public override string Name => "AutoRespond";
public AutoRespond(DiscordSocketClient client) : base(client) public AutoRespond(DiscordSocketClient client) : base(client)

View file

@ -5,7 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace Noikoio.RegexBot.Feature.AutoRespond namespace Noikoio.RegexBot.Module.AutoRespond
{ {
/// <summary> /// <summary>
/// Represents a single autoresponse definition. /// Represents a single autoresponse definition.

View file

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Noikoio.RegexBot.Feature.AutoRespond namespace Noikoio.RegexBot.Module.AutoRespond
{ {
/// <summary> /// <summary>
/// Stores rate limit settings and caches. /// Stores rate limit settings and caches.

View file

@ -7,14 +7,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.EntityCache namespace Noikoio.RegexBot.Module.EntityCache
{ {
/// <summary> /// <summary>
/// Caches information regarding all known guilds, channels, and users. /// Caches information regarding all known guilds, channels, and users.
/// The function of this feature should be transparent to the user, and thus no configuration is needed. /// The function of this module should be transparent to the user, and thus no configuration is needed.
/// This feature should be initialized BEFORE any other features that make use of guild and user cache. /// This module should be initialized BEFORE any other modules that make use of guild and user cache.
/// </summary> /// </summary>
class EntityCache : BotFeature class EntityCache : BotModule
{ {
private readonly DatabaseConfig _db; private readonly DatabaseConfig _db;
@ -24,7 +24,7 @@ namespace Noikoio.RegexBot.Feature.EntityCache
{ {
_db = RegexBot.Config.Database; _db = RegexBot.Config.Database;
if (_db.Enabled) if (_db.Available)
{ {
Sql.CreateCacheTables(); Sql.CreateCacheTables();

View file

@ -3,7 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
namespace Noikoio.RegexBot.Feature.EntityCache namespace Noikoio.RegexBot.Module.EntityCache
{ {
/// <summary> /// <summary>
/// Contains common constants and static methods for cache access. /// Contains common constants and static methods for cache access.

View file

@ -4,7 +4,7 @@ using System.Data.Common;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.EntityCache namespace Noikoio.RegexBot.Module.EntityCache
{ {
/// <summary> /// <summary>
/// Represents a cached user. /// Represents a cached user.

View file

@ -9,7 +9,7 @@ using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.ModTools namespace Noikoio.RegexBot.Module.ModTools
{ {
[DebuggerDisplay("{Label}-type command")] [DebuggerDisplay("{Label}-type command")]
abstract class CommandBase abstract class CommandBase

View file

@ -6,7 +6,7 @@ using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.ModTools.Commands namespace Noikoio.RegexBot.Module.ModTools.Commands
{ {
class BanKick : CommandBase class BanKick : CommandBase

View file

@ -4,7 +4,7 @@ using Newtonsoft.Json.Linq;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.ModTools.Commands namespace Noikoio.RegexBot.Module.ModTools.Commands
{ {
class Say : CommandBase class Say : CommandBase
{ {

View file

@ -8,14 +8,14 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.ModTools namespace Noikoio.RegexBot.Module.ModTools
{ {
/// <summary> /// <summary>
/// Entry point for the ModTools feature. /// ModTools module object.
/// This feature implements moderation commands that are defined and enabled in configuration. /// Implements moderation commands that are individually defined and enabled in configuration.
/// </summary> /// </summary>
// We are not using Discord.Net's Commands extension, as it does not allow for changes during runtime. // We are not using Discord.Net's Commands extension, as it does not allow for changes during runtime.
class ModTools : BotFeature class ModTools : BotModule
{ {
public override string Name => "ModTools"; public override string Name => "ModTools";

View file

@ -7,16 +7,16 @@ using System.Threading.Tasks;
namespace Noikoio.RegexBot namespace Noikoio.RegexBot
{ {
/// <summary> /// <summary>
/// Main class. On start, initializes bot features and passes the DiscordSocketClient to them /// Main class. On start, initializes bot modules and passes the DiscordSocketClient to them
/// </summary> /// </summary>
class RegexBot class RegexBot
{ {
private static Configuration _config; private static Configuration _config;
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private BotFeature[] _features; private BotModule[] _modules;
internal static Configuration Config => _config; internal static Configuration Config => _config;
internal IEnumerable<BotFeature> Features => _features; internal IEnumerable<BotModule> Modules => _modules;
internal RegexBot() internal RegexBot()
{ {
@ -42,13 +42,13 @@ namespace Noikoio.RegexBot
// Hook up handlers for basic functions // Hook up handlers for basic functions
_client.Connected += _client_Connected; _client.Connected += _client_Connected;
// Initialize features // Initialize modules
_features = new BotFeature[] _modules = new BotModule[]
{ {
new Feature.AutoMod.AutoMod(_client), new Module.AutoMod.AutoMod(_client),
new Feature.ModTools.ModTools(_client), new Module.ModTools.ModTools(_client),
new Feature.AutoRespond.AutoRespond(_client), new Module.AutoRespond.AutoRespond(_client),
new Feature.EntityCache.EntityCache(_client) // EntityCache goes before anything else that uses its data new Module.EntityCache.EntityCache(_client) // EntityCache goes before anything else that uses its data
}; };
var dlog = Logger.GetLogger("Discord.Net"); var dlog = Logger.GetLogger("Discord.Net");
_client.Log += async (arg) => _client.Log += async (arg) =>
@ -56,7 +56,7 @@ namespace Noikoio.RegexBot
String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""), String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""),
arg.Message)); arg.Message));
// With features initialized, finish loading configuration // With modules initialized, finish loading configuration
var conf = _config.ReloadServerConfig().Result; var conf = _config.ReloadServerConfig().Result;
if (conf == false) if (conf == false)
{ {