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 (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;
}
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;
namespace Noikoio.RegexBot.ConfigItem
@ -10,17 +11,17 @@ namespace Noikoio.RegexBot.ConfigItem
{
private readonly string _name;
private ulong? _id;
private IEnumerable<Rule> _rules;
private IEnumerable<RuleConfig> _rules;
private EntityList _moderators;
public string Name => _name;
public ulong? Id {
get => _id; set { if (!_id.HasValue) _id = value; }
}
public IEnumerable<Rule> MatchResponseRules => _rules;
public IEnumerable<RuleConfig> MatchResponseRules => _rules;
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;
_id = id;

View file

@ -147,7 +147,7 @@ namespace Noikoio.RegexBot
// Read rules
// 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);
foreach (JObject ruleconf in sconf["rules"])
@ -161,11 +161,11 @@ namespace Noikoio.RegexBot
}
await SLog($"Adding rule \"{name}\"");
Rule rule;
RuleConfig rule;
try
{
rule = new Rule(newserver, ruleconf);
} catch (Rule.RuleImportException ex)
rule = new RuleConfig(newserver, ruleconf);
} catch (RuleConfig.RuleImportException ex)
{
await SLog("-> Error: " + ex.Message);
return false;

View file

@ -8,17 +8,18 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Noikoio.RegexBot
namespace Noikoio.RegexBot.Feature.RegexResponder
{
/// <summary>
/// Bot subsystem that implements regex matching and response processing.
/// Implements per-message regex matching and executes customizable responses.
/// Namesake of this project.
/// </summary>
partial class RuleResponder
partial class EventProcessor
{
private readonly DiscordSocketClient _client;
private readonly ConfigLoader _conf;
public RuleResponder(DiscordSocketClient client, ConfigLoader conf)
public EventProcessor(DiscordSocketClient client, ConfigLoader conf)
{
_client = client;
_conf = conf;
@ -98,7 +99,7 @@ namespace Noikoio.RegexBot
/// 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.
/// </summary>
private async Task ProcessMessage(Server srv, Rule rule, SocketMessage msg)
private async Task ProcessMessage(Server srv, RuleConfig rule, SocketMessage msg)
{
string msgcontent;
@ -182,7 +183,7 @@ namespace Noikoio.RegexBot
return result.ToString();
}
private bool IsFiltered(Rule r, SocketMessage m)
private bool IsFiltered(RuleConfig r, SocketMessage m)
{
if (r.FilterMode == FilterType.None) return false;

View file

@ -1,18 +1,17 @@
using Discord;
using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace Noikoio.RegexBot
namespace Noikoio.RegexBot.Feature.RegexResponder
{
// 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;
#if DEBUG
@ -20,7 +19,7 @@ namespace Noikoio.RegexBot
/// Throws an exception. Meant to be a quick error handling test.
/// No parameters.
/// </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.");
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.
/// No parameters.
/// </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 result = new StringBuilder();
@ -56,7 +55,7 @@ namespace Noikoio.RegexBot
/// Sends a message to a specified channel.
/// Parameters: say (channel) (message)
/// </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);
if (@in.Length != 3)
@ -81,7 +80,7 @@ namespace Noikoio.RegexBot
/// Reports the incoming message to a given channel.
/// Parameters: report (channel)
/// </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);
if (@in.Length != 2)
@ -137,7 +136,7 @@ namespace Noikoio.RegexBot
/// Deletes the incoming message.
/// No parameters.
/// </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
await m.DeleteAsync();
@ -147,7 +146,7 @@ namespace Noikoio.RegexBot
/// Executes an external program and sends standard output to the given channel.
/// Parameters: exec (channel) (command line)
/// </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);
if (@in.Length < 3)
@ -198,7 +197,7 @@ namespace Noikoio.RegexBot
/// No parameters.
/// </summary>
// 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;
await g.AddBanAsync(m.Author);
@ -208,7 +207,7 @@ namespace Noikoio.RegexBot
/// Grants or revokes a specified role to/from a given user.
/// Parameters: grantrole/revokerole (user ID or @_) (role ID)
/// </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);
if (@in.Length != 3)

View file

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

View file

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

View file

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