Renamed Features to Modules
This commit is contained in:
parent
17037700dd
commit
801292e1a6
24 changed files with 61 additions and 64 deletions
|
@ -7,12 +7,9 @@ using System.Threading.Tasks;
|
|||
namespace Noikoio.RegexBot
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for bot features
|
||||
/// Base class for bot modules
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This may have use in some sort of external plugin system later.
|
||||
/// </remarks>
|
||||
abstract class BotFeature
|
||||
abstract class BotModule
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly AsyncLogger _logger;
|
||||
|
@ -20,20 +17,20 @@ namespace Noikoio.RegexBot
|
|||
public abstract string Name { get; }
|
||||
protected DiscordSocketClient Client => _client;
|
||||
|
||||
public BotFeature(DiscordSocketClient client)
|
||||
public BotModule(DiscordSocketClient client)
|
||||
{
|
||||
_client = client;
|
||||
_logger = Logger.GetLogger(this.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes feature-specific configuration.
|
||||
/// Processes module-specific configuration.
|
||||
/// </summary>
|
||||
/// <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
|
||||
/// bot and all features to revert to previously used configuration values with no effort on the part
|
||||
/// of individual features.
|
||||
/// all modules to revert to previously used configuration values with no effort on the part of the
|
||||
/// module code itself.
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// Processed configuration data prepared for later use.
|
||||
|
@ -45,7 +42,7 @@ namespace Noikoio.RegexBot
|
|||
public abstract Task<object> ProcessConfiguration(JToken configSection);
|
||||
|
||||
/// <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>
|
||||
/// <returns>
|
||||
/// 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.");
|
||||
}
|
||||
|
||||
if (sc.FeatureConfigs.TryGetValue(this, out var item)) return item;
|
||||
if (sc.ModuleConfigs.TryGetValue(this, out var item)) return item;
|
||||
else return null;
|
||||
}
|
||||
|
||||
|
@ -88,7 +85,7 @@ namespace Noikoio.RegexBot
|
|||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public class ConfigSectionAttribute : Attribute
|
|
@ -14,9 +14,9 @@ namespace Noikoio.RegexBot.ConfigItem
|
|||
private readonly string _parsemsg;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether database features are enabled.
|
||||
/// Gets whether database storage is available.
|
||||
/// </summary>
|
||||
public bool Enabled => _enabled;
|
||||
public bool Available => _enabled;
|
||||
/// <summary>
|
||||
/// Constructor error message (only if not enabled)
|
||||
/// </summary>
|
||||
|
@ -49,7 +49,7 @@ namespace Noikoio.RegexBot.ConfigItem
|
|||
|
||||
public async Task<NpgsqlConnection> GetOpenConnectionAsync()
|
||||
{
|
||||
if (!Enabled) return null;
|
||||
if (!Available) return null;
|
||||
|
||||
var cs = new NpgsqlConnectionStringBuilder()
|
||||
{
|
||||
|
|
|
@ -10,18 +10,18 @@ namespace Noikoio.RegexBot.ConfigItem
|
|||
{
|
||||
private readonly ulong _id;
|
||||
private EntityList _moderators;
|
||||
private ReadOnlyDictionary<BotFeature, object> _featureData;
|
||||
private ReadOnlyDictionary<BotModule, object> _modData;
|
||||
|
||||
public ulong? Id => _id;
|
||||
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;
|
||||
_moderators = moderators;
|
||||
_featureData = featureconf;
|
||||
Debug.Assert(_moderators != null && _featureData != null);
|
||||
_modData = modconf;
|
||||
Debug.Assert(_moderators != null && _modData != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -137,9 +137,9 @@ namespace Noikoio.RegexBot
|
|||
EntityList mods = new EntityList(sconf["moderators"]);
|
||||
if (sconf["moderators"] != null) await SLog("Moderator " + mods.ToString());
|
||||
|
||||
// Load feature configurations
|
||||
Dictionary<BotFeature, object> customConfs = new Dictionary<BotFeature, object>();
|
||||
foreach (var item in _bot.Features)
|
||||
// Load module configurations
|
||||
Dictionary<BotModule, object> customConfs = new Dictionary<BotModule, object>();
|
||||
foreach (var item in _bot.Modules)
|
||||
{
|
||||
var attr = item.GetType().GetTypeInfo()
|
||||
.GetMethod("ProcessConfiguration").GetCustomAttribute<ConfigSectionAttribute>();
|
||||
|
@ -173,7 +173,7 @@ namespace Noikoio.RegexBot
|
|||
|
||||
// Switch to using new data
|
||||
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();
|
||||
|
|
|
@ -4,7 +4,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod
|
||||
namespace Noikoio.RegexBot.Module.AutoMod
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// should be implemented using <see cref="AutoRespond"/>.
|
||||
/// </remarks>
|
||||
class AutoMod : BotFeature
|
||||
class AutoMod : BotModule
|
||||
{
|
||||
public override string Name => "AutoMod";
|
||||
|
|
@ -8,7 +8,7 @@ using System.Text;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod
|
||||
namespace Noikoio.RegexBot.Module.AutoMod
|
||||
{
|
||||
/// <summary>
|
||||
/// Representation of a single AutoMod rule.
|
|
@ -7,7 +7,7 @@ using System.Collections.ObjectModel;
|
|||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod
|
||||
namespace Noikoio.RegexBot.Module.AutoMod
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all Response classes.
|
|
@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Bans the invoking user.
|
|
@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Kicks the invoking user.
|
|
@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Removes the invoking message.
|
|
@ -5,7 +5,7 @@ using System;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends a summary of the invoking message, along with information
|
|
@ -4,7 +4,7 @@ using System;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Manipulates a given user's role.
|
|
@ -3,7 +3,7 @@ using Noikoio.RegexBot.ConfigItem;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
|
||||
namespace Noikoio.RegexBot.Module.AutoMod.Responses
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends a message to the given target.
|
|
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||
namespace Noikoio.RegexBot.Module.AutoRespond
|
||||
{
|
||||
/// <summary>
|
||||
/// Similar to <see cref="AutoMod"/>, but lightweight.
|
||||
|
@ -19,9 +19,9 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
|||
/// </list>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
partial class AutoRespond : BotFeature
|
||||
partial class AutoRespond : BotModule
|
||||
{
|
||||
#region BotFeature implementation
|
||||
#region BotModule implementation
|
||||
public override string Name => "AutoRespond";
|
||||
|
||||
public AutoRespond(DiscordSocketClient client) : base(client)
|
|
@ -5,7 +5,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||
namespace Noikoio.RegexBot.Module.AutoRespond
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single autoresponse definition.
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||
namespace Noikoio.RegexBot.Module.AutoRespond
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores rate limit settings and caches.
|
|
@ -7,14 +7,14 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.EntityCache
|
||||
namespace Noikoio.RegexBot.Module.EntityCache
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// This feature should be initialized BEFORE any other features that make use of guild and user cache.
|
||||
/// The function of this module should be transparent to the user, and thus no configuration is needed.
|
||||
/// This module should be initialized BEFORE any other modules that make use of guild and user cache.
|
||||
/// </summary>
|
||||
class EntityCache : BotFeature
|
||||
class EntityCache : BotModule
|
||||
{
|
||||
private readonly DatabaseConfig _db;
|
||||
|
||||
|
@ -24,7 +24,7 @@ namespace Noikoio.RegexBot.Feature.EntityCache
|
|||
{
|
||||
_db = RegexBot.Config.Database;
|
||||
|
||||
if (_db.Enabled)
|
||||
if (_db.Available)
|
||||
{
|
||||
Sql.CreateCacheTables();
|
||||
|
|
@ -3,7 +3,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.EntityCache
|
||||
namespace Noikoio.RegexBot.Module.EntityCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains common constants and static methods for cache access.
|
|
@ -4,7 +4,7 @@ using System.Data.Common;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.EntityCache
|
||||
namespace Noikoio.RegexBot.Module.EntityCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a cached user.
|
|
@ -9,7 +9,7 @@ using System.Reflection;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.ModTools
|
||||
namespace Noikoio.RegexBot.Module.ModTools
|
||||
{
|
||||
[DebuggerDisplay("{Label}-type command")]
|
||||
abstract class CommandBase
|
|
@ -6,7 +6,7 @@ using System;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.ModTools.Commands
|
||||
namespace Noikoio.RegexBot.Module.ModTools.Commands
|
||||
{
|
||||
|
||||
class BanKick : CommandBase
|
|
@ -4,7 +4,7 @@ using Newtonsoft.Json.Linq;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.ModTools.Commands
|
||||
namespace Noikoio.RegexBot.Module.ModTools.Commands
|
||||
{
|
||||
class Say : CommandBase
|
||||
{
|
|
@ -8,14 +8,14 @@ using System.Linq;
|
|||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Noikoio.RegexBot.Feature.ModTools
|
||||
namespace Noikoio.RegexBot.Module.ModTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Entry point for the ModTools feature.
|
||||
/// This feature implements moderation commands that are defined and enabled in configuration.
|
||||
/// ModTools module object.
|
||||
/// Implements moderation commands that are individually defined and enabled in configuration.
|
||||
/// </summary>
|
||||
// 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";
|
||||
|
20
RegexBot.cs
20
RegexBot.cs
|
@ -7,16 +7,16 @@ using System.Threading.Tasks;
|
|||
namespace Noikoio.RegexBot
|
||||
{
|
||||
/// <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>
|
||||
class RegexBot
|
||||
{
|
||||
private static Configuration _config;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private BotFeature[] _features;
|
||||
private BotModule[] _modules;
|
||||
|
||||
internal static Configuration Config => _config;
|
||||
internal IEnumerable<BotFeature> Features => _features;
|
||||
internal IEnumerable<BotModule> Modules => _modules;
|
||||
|
||||
internal RegexBot()
|
||||
{
|
||||
|
@ -42,13 +42,13 @@ namespace Noikoio.RegexBot
|
|||
// Hook up handlers for basic functions
|
||||
_client.Connected += _client_Connected;
|
||||
|
||||
// Initialize features
|
||||
_features = new BotFeature[]
|
||||
// Initialize modules
|
||||
_modules = new BotModule[]
|
||||
{
|
||||
new Feature.AutoMod.AutoMod(_client),
|
||||
new Feature.ModTools.ModTools(_client),
|
||||
new Feature.AutoRespond.AutoRespond(_client),
|
||||
new Feature.EntityCache.EntityCache(_client) // EntityCache goes before anything else that uses its data
|
||||
new Module.AutoMod.AutoMod(_client),
|
||||
new Module.ModTools.ModTools(_client),
|
||||
new Module.AutoRespond.AutoRespond(_client),
|
||||
new Module.EntityCache.EntityCache(_client) // EntityCache goes before anything else that uses its data
|
||||
};
|
||||
var dlog = Logger.GetLogger("Discord.Net");
|
||||
_client.Log += async (arg) =>
|
||||
|
@ -56,7 +56,7 @@ namespace Noikoio.RegexBot
|
|||
String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""),
|
||||
arg.Message));
|
||||
|
||||
// With features initialized, finish loading configuration
|
||||
// With modules initialized, finish loading configuration
|
||||
var conf = _config.ReloadServerConfig().Result;
|
||||
if (conf == false)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue