RegexBot/Module/ModLogs/GuildState.cs

117 lines
4.4 KiB
C#
Raw Normal View History

using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using System;
namespace Noikoio.RegexBot.Module.ModLogs
{
/// <summary>
2018-03-27 22:15:13 +00:00
/// ModLogs guild-specific values.
/// </summary>
2018-03-27 22:15:13 +00:00
class GuildState
{
// Event reporting
private readonly EntityName _rptTarget;
2018-03-27 22:15:13 +00:00
private LogEntry.LogType _rptTypes;
/// <summary>
/// Target reporting channel.
/// </summary>
public EntityName? RptTarget => _rptTarget;
/// <summary>
/// Event types to send to the reporting channel.
/// </summary>
2018-03-27 22:15:13 +00:00
public LogEntry.LogType RptTypes => _rptTypes;
// Query command
private readonly string _qCmd; // command name
private readonly EntityList _qAccess; // list of those able to issue the command
2018-03-27 22:15:13 +00:00
private readonly LogEntry.LogType _qDefaultAnswer; // default entry types to display
/// <summary>
/// Query command. The first word in an incoming message, including prefix, that triggers a query.
/// </summary>
public string QrCommand => _qCmd;
/// <summary>
/// List of users permitted to invoke the query command.
/// If null, refer to the guild's Moderators list.
/// </summary>
public EntityList QrPermittedUsers => _qAccess;
/// <summary>
/// Event types to display in a query.
/// </summary>
2018-03-27 22:15:13 +00:00
public LogEntry.LogType QrTypes => _qDefaultAnswer;
2018-03-27 22:15:13 +00:00
public GuildState(JObject cfgRoot)
{
// AutoReporting settings
var arcfg = cfgRoot["AutoReporting"];
if (arcfg == null)
{
_rptTarget = default(EntityName); // NOTE: Change this if EntityName becomes a class later
2018-03-27 22:15:13 +00:00
_rptTypes = LogEntry.LogType.None;
}
else if (arcfg.Type == JTokenType.Object)
{
string chval = arcfg["Channel"]?.Value<string>();
if (chval == null) throw new RuleImportException("Reporting channel is not defined.");
if (!string.IsNullOrWhiteSpace(chval) && chval[0] == '#')
_rptTarget = new EntityName(chval.Substring(1, chval.Length-1), EntityType.Channel);
else
throw new RuleImportException("Reporting channel is not properly defined.");
// Require the channel's ID for now.
if (!_rptTarget.Id.HasValue) throw new RuleImportException("Reporting channel's ID must be specified.");
// TODO make optional
string rpval = arcfg["Events"]?.Value<string>();
2018-03-27 22:15:13 +00:00
try
{
_rptTypes = LogEntry.GetLogTypeFromString(rpval);
}
catch (ArgumentException ex)
{
throw new RuleImportException(ex.Message);
}
}
else
{
throw new RuleImportException("Section for AutoReporting is not correctly defined.");
}
// QueryCommand settings
var qccfg = cfgRoot["QueryCommand"];
if (qccfg == null)
{
_qCmd = null;
_qAccess = null;
2018-03-27 22:15:13 +00:00
_qDefaultAnswer = LogEntry.LogType.None;
}
else if (arcfg.Type == JTokenType.Object)
{
_qCmd = arcfg["Command"]?.Value<string>();
if (string.IsNullOrWhiteSpace(_qCmd))
throw new RuleImportException("Query command option must have a value.");
if (_qCmd.Contains(" "))
throw new RuleImportException("Query command must not contain spaces.");
var acl = arcfg["AllowedUsers"];
if (acl == null) _qAccess = null;
else _qAccess = new EntityList(acl);
// TODO make optional
string ansval = arcfg["DefaultEvents"]?.Value<string>();
try
{
2018-03-27 22:15:13 +00:00
_qDefaultAnswer = LogEntry.GetLogTypeFromString(ansval);
}
2018-03-27 22:15:13 +00:00
catch (ArgumentException ex)
{
2018-03-27 22:15:13 +00:00
throw new RuleImportException(ex.Message);
}
2018-03-27 22:15:13 +00:00
}
else
{
throw new RuleImportException("Section for QueryCommand is not correctly defined.");
}
}
}
}