Reorganized files to separate features

With the addition of moderation commands coming up, it will be good
having a clear separation of separate features both in code as well
as in the way the files themselves are organized.
This code change will be coming soon.
This commit is contained in:
Noikoio 2017-07-21 20:51:00 -07:00
parent 80b83aeaab
commit 376e44f941
8 changed files with 40 additions and 33 deletions

View file

@ -75,7 +75,7 @@ namespace Noikoio.RegexBot.ConfigItem
if (section["blacklist"] != null) if (section["blacklist"] != null)
{ {
if (mode == FilterType.Whitelist) if (mode == FilterType.Whitelist)
throw new Rule.RuleImportException("Cannot have whitelist AND blacklist defined."); throw new RuleConfig.RuleImportException("Cannot have whitelist AND blacklist defined.");
mode = FilterType.Blacklist; mode = FilterType.Blacklist;
} }
if (mode == FilterType.None) list = new EntityList(); // might even be fine to keep it null? if (mode == FilterType.None) list = new EntityList(); // might even be fine to keep it null?

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using Noikoio.RegexBot.Feature.RegexResponder;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
namespace Noikoio.RegexBot.ConfigItem namespace Noikoio.RegexBot.ConfigItem
@ -10,17 +11,17 @@ namespace Noikoio.RegexBot.ConfigItem
{ {
private readonly string _name; private readonly string _name;
private ulong? _id; private ulong? _id;
private IEnumerable<Rule> _rules; private IEnumerable<RuleConfig> _rules;
private EntityList _moderators; private EntityList _moderators;
public string Name => _name; public string Name => _name;
public ulong? Id { public ulong? Id {
get => _id; set { if (!_id.HasValue) _id = value; } get => _id; set { if (!_id.HasValue) _id = value; }
} }
public IEnumerable<Rule> MatchResponseRules => _rules; public IEnumerable<RuleConfig> MatchResponseRules => _rules;
public EntityList Moderators => _moderators; public EntityList Moderators => _moderators;
public Server(string name, ulong? id, IEnumerable<Rule> rules, EntityList moderators) public Server(string name, ulong? id, IEnumerable<RuleConfig> rules, EntityList moderators)
{ {
_name = name; _name = name;
_id = id; _id = id;

View file

@ -147,7 +147,7 @@ namespace Noikoio.RegexBot
// Read rules // Read rules
// Also, parsed rules require a server reference. Creating it here. // Also, parsed rules require a server reference. Creating it here.
List<Rule> rules = new List<Rule>(); List<RuleConfig> rules = new List<RuleConfig>();
Server newserver = new Server(sname, sid, rules, mods); Server newserver = new Server(sname, sid, rules, mods);
foreach (JObject ruleconf in sconf["rules"]) foreach (JObject ruleconf in sconf["rules"])
@ -161,11 +161,11 @@ namespace Noikoio.RegexBot
} }
await SLog($"Adding rule \"{name}\""); await SLog($"Adding rule \"{name}\"");
Rule rule; RuleConfig rule;
try try
{ {
rule = new Rule(newserver, ruleconf); rule = new RuleConfig(newserver, ruleconf);
} catch (Rule.RuleImportException ex) } catch (RuleConfig.RuleImportException ex)
{ {
await SLog("-> Error: " + ex.Message); await SLog("-> Error: " + ex.Message);
return false; return false;

View file

@ -8,17 +8,18 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot namespace Noikoio.RegexBot.Feature.RegexResponder
{ {
/// <summary> /// <summary>
/// Bot subsystem that implements regex matching and response processing. /// Implements per-message regex matching and executes customizable responses.
/// Namesake of this project.
/// </summary> /// </summary>
partial class RuleResponder partial class EventProcessor
{ {
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private readonly ConfigLoader _conf; private readonly ConfigLoader _conf;
public RuleResponder(DiscordSocketClient client, ConfigLoader conf) public EventProcessor(DiscordSocketClient client, ConfigLoader conf)
{ {
_client = client; _client = client;
_conf = conf; _conf = conf;
@ -98,7 +99,7 @@ namespace Noikoio.RegexBot
/// Uses information from a single rule and checks if the incoming message is a match. /// Uses information from a single rule and checks if the incoming message is a match.
/// If it matches, the rule's responses are executed. To be run in the thread pool. /// If it matches, the rule's responses are executed. To be run in the thread pool.
/// </summary> /// </summary>
private async Task ProcessMessage(Server srv, Rule rule, SocketMessage msg) private async Task ProcessMessage(Server srv, RuleConfig rule, SocketMessage msg)
{ {
string msgcontent; string msgcontent;
@ -182,7 +183,7 @@ namespace Noikoio.RegexBot
return result.ToString(); return result.ToString();
} }
private bool IsFiltered(Rule r, SocketMessage m) private bool IsFiltered(RuleConfig r, SocketMessage m)
{ {
if (r.FilterMode == FilterType.None) return false; if (r.FilterMode == FilterType.None) return false;

View file

@ -1,18 +1,17 @@
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot namespace Noikoio.RegexBot.Feature.RegexResponder
{ {
// Contains code for handling each response in a rule. // Contains code for handling each response in a rule.
partial class RuleResponder partial class RegexResponder
{ {
private delegate Task ResponseProcessor(AsyncLogger l, string cmd, Rule r, SocketMessage m); private delegate Task ResponseProcessor(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m);
private readonly ReadOnlyDictionary<string, ResponseProcessor> _commands; private readonly ReadOnlyDictionary<string, ResponseProcessor> _commands;
#if DEBUG #if DEBUG
@ -20,7 +19,7 @@ namespace Noikoio.RegexBot
/// Throws an exception. Meant to be a quick error handling test. /// Throws an exception. Meant to be a quick error handling test.
/// No parameters. /// No parameters.
/// </summary> /// </summary>
private async Task RP_Crash(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Crash(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
await l("Will throw an exception."); await l("Will throw an exception.");
throw new Exception("Requested in response."); throw new Exception("Requested in response.");
@ -31,7 +30,7 @@ namespace Noikoio.RegexBot
/// The guild info displayed is the one in which the command is invoked. /// The guild info displayed is the one in which the command is invoked.
/// No parameters. /// No parameters.
/// </summary> /// </summary>
private Task RP_DumpID(AsyncLogger l, string cmd, Rule r, SocketMessage m) private Task RP_DumpID(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
var g = ((SocketGuildUser)m.Author).Guild; var g = ((SocketGuildUser)m.Author).Guild;
var result = new StringBuilder(); var result = new StringBuilder();
@ -56,7 +55,7 @@ namespace Noikoio.RegexBot
/// Sends a message to a specified channel. /// Sends a message to a specified channel.
/// Parameters: say (channel) (message) /// Parameters: say (channel) (message)
/// </summary> /// </summary>
private async Task RP_Say(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Say(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
string[] @in = SplitParams(cmd, 3); string[] @in = SplitParams(cmd, 3);
if (@in.Length != 3) if (@in.Length != 3)
@ -81,7 +80,7 @@ namespace Noikoio.RegexBot
/// Reports the incoming message to a given channel. /// Reports the incoming message to a given channel.
/// Parameters: report (channel) /// Parameters: report (channel)
/// </summary> /// </summary>
private async Task RP_Report(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Report(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
string[] @in = SplitParams(cmd); string[] @in = SplitParams(cmd);
if (@in.Length != 2) if (@in.Length != 2)
@ -137,7 +136,7 @@ namespace Noikoio.RegexBot
/// Deletes the incoming message. /// Deletes the incoming message.
/// No parameters. /// No parameters.
/// </summary> /// </summary>
private async Task RP_Remove(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Remove(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
// Parameters are not checked // Parameters are not checked
await m.DeleteAsync(); await m.DeleteAsync();
@ -147,7 +146,7 @@ namespace Noikoio.RegexBot
/// Executes an external program and sends standard output to the given channel. /// Executes an external program and sends standard output to the given channel.
/// Parameters: exec (channel) (command line) /// Parameters: exec (channel) (command line)
/// </summary> /// </summary>
private async Task RP_Exec(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Exec(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
var @in = SplitParams(cmd, 4); var @in = SplitParams(cmd, 4);
if (@in.Length < 3) if (@in.Length < 3)
@ -198,7 +197,7 @@ namespace Noikoio.RegexBot
/// No parameters. /// No parameters.
/// </summary> /// </summary>
// TODO add parameter for message auto-deleting // TODO add parameter for message auto-deleting
private async Task RP_Ban(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_Ban(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
SocketGuild g = ((SocketGuildUser)m.Author).Guild; SocketGuild g = ((SocketGuildUser)m.Author).Guild;
await g.AddBanAsync(m.Author); await g.AddBanAsync(m.Author);
@ -208,7 +207,7 @@ namespace Noikoio.RegexBot
/// Grants or revokes a specified role to/from a given user. /// Grants or revokes a specified role to/from a given user.
/// Parameters: grantrole/revokerole (user ID or @_) (role ID) /// Parameters: grantrole/revokerole (user ID or @_) (role ID)
/// </summary> /// </summary>
private async Task RP_GrantRevokeRole(AsyncLogger l, string cmd, Rule r, SocketMessage m) private async Task RP_GrantRevokeRole(AsyncLogger l, string cmd, RuleConfig r, SocketMessage m)
{ {
string[] @in = SplitParams(cmd); string[] @in = SplitParams(cmd);
if (@in.Length != 3) if (@in.Length != 3)

View file

@ -1,15 +1,16 @@
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace Noikoio.RegexBot.ConfigItem namespace Noikoio.RegexBot.Feature.RegexResponder
{ {
/// <summary> /// <summary>
/// Represents configuration for a single rule. /// Represents configuration for a single rule.
/// </summary> /// </summary>
[System.Diagnostics.DebuggerDisplay("Rule: {DisplayName}")] [System.Diagnostics.DebuggerDisplay("Rule: {DisplayName}")]
internal struct Rule internal struct RuleConfig
{ {
private string _displayName; private string _displayName;
private Server _server; private Server _server;
@ -42,7 +43,7 @@ namespace Noikoio.RegexBot.ConfigItem
/// <exception cref="RuleImportException>"> /// <exception cref="RuleImportException>">
/// Thrown when encountering a missing or invalid value. /// Thrown when encountering a missing or invalid value.
/// </exception> /// </exception>
public Rule(Server serverref, JObject ruleconf) public RuleConfig(Server serverref, JObject ruleconf)
{ {
_server = serverref; _server = serverref;

View file

@ -12,8 +12,8 @@ namespace Noikoio.RegexBot
{ {
private readonly ConfigLoader _config; private readonly ConfigLoader _config;
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private readonly RuleResponder _responder;
// Constructor loads all subsystems. Subsystem constructors hook up their event delegates.
internal RegexBot(ConfigLoader conf) internal RegexBot(ConfigLoader conf)
{ {
_client = new DiscordSocketClient(new DiscordSocketConfig() _client = new DiscordSocketClient(new DiscordSocketConfig()
@ -24,8 +24,11 @@ namespace Noikoio.RegexBot
}); });
_config = conf; _config = conf;
// Hook up handlers for basic functions
_client.Connected += _client_Connected; _client.Connected += _client_Connected;
_responder = new RuleResponder(_client, _config);
// Initialize features
new Feature.RegexResponder.EventProcessor(_client, _config);
} }
internal async Task Start() internal async Task Start()

View file

@ -4,8 +4,10 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Noikoio.RegexBot</RootNamespace> <RootNamespace>Noikoio.RegexBot</RootNamespace>
<AssemblyVersion>0.14.0.0</AssemblyVersion> <AssemblyVersion>0.15.0.0</AssemblyVersion>
<Description>Highly configurable Discord moderation bot</Description> <Description>Highly configurable Discord moderation bot</Description>
<Authors>Noikoio</Authors>
<Company />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">