2018-04-05 22:46:15 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Webhook;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
2018-04-05 22:46:15 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
|
|
|
|
|
namespace Noikoio.RegexBot.Module.ModLogs
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2018-03-27 22:15:13 +00:00
|
|
|
|
/// ModLogs guild-specific values.
|
2018-03-02 03:25:08 +00:00
|
|
|
|
/// </summary>
|
2018-03-27 22:15:13 +00:00
|
|
|
|
class GuildState
|
2018-03-02 03:25:08 +00:00
|
|
|
|
{
|
|
|
|
|
// Event reporting
|
2018-04-05 22:46:15 +00:00
|
|
|
|
private DiscordWebhookClient _rptTarget;
|
2018-03-27 22:15:13 +00:00
|
|
|
|
private LogEntry.LogType _rptTypes;
|
2018-04-05 22:46:15 +00:00
|
|
|
|
private ulong _rptIgnore;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
/// <summary>
|
2018-04-05 22:46:15 +00:00
|
|
|
|
/// Webhook for log reporting.
|
2018-03-02 03:25:08 +00:00
|
|
|
|
/// </summary>
|
2018-04-05 22:46:15 +00:00
|
|
|
|
public DiscordWebhookClient RptTarget => _rptTarget;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event types to send to the reporting channel.
|
|
|
|
|
/// </summary>
|
2018-03-27 22:15:13 +00:00
|
|
|
|
public LogEntry.LogType RptTypes => _rptTypes;
|
2018-04-05 22:46:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Channel for AutoReporting to ignore.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong RptIgnore => _rptIgnore;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
|
|
|
|
|
// 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
|
2018-03-02 03:25:08 +00:00
|
|
|
|
/// <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-02 03:25:08 +00:00
|
|
|
|
|
2018-03-27 22:15:13 +00:00
|
|
|
|
public GuildState(JObject cfgRoot)
|
2018-03-02 03:25:08 +00:00
|
|
|
|
{
|
|
|
|
|
// AutoReporting settings
|
|
|
|
|
var arcfg = cfgRoot["AutoReporting"];
|
|
|
|
|
if (arcfg == null)
|
|
|
|
|
{
|
2018-04-05 22:46:15 +00:00
|
|
|
|
_rptTarget = null;
|
2018-03-27 22:15:13 +00:00
|
|
|
|
_rptTypes = LogEntry.LogType.None;
|
2018-04-05 22:46:15 +00:00
|
|
|
|
_rptIgnore = 0;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
else if (arcfg.Type == JTokenType.Object)
|
|
|
|
|
{
|
2018-04-05 22:46:15 +00:00
|
|
|
|
string whurl = arcfg["WebhookUrl"]?.Value<string>();
|
|
|
|
|
if (whurl == null) throw new RuleImportException("Webhook URL for log reporting is not specified.");
|
|
|
|
|
var wrx = WebhookUrlParts.Match(whurl);
|
|
|
|
|
if (!wrx.Success) throw new RuleImportException("Webhook URL for log reporting is not valid.");
|
|
|
|
|
var wid = ulong.Parse(wrx.Groups[1].Value);
|
|
|
|
|
var wtk = wrx.Groups[2].Value;
|
|
|
|
|
_rptTarget = new DiscordWebhookClient(wid, wtk,
|
|
|
|
|
new Discord.Rest.DiscordRestConfig() { DefaultRetryMode = RetryMode.RetryRatelimit });
|
|
|
|
|
// TODO figure out how to hook up the webhook client's log event
|
2018-03-02 03:25:08 +00:00
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
2018-04-05 22:46:15 +00:00
|
|
|
|
|
|
|
|
|
var ignoreId = arcfg["CacheIgnore"]?.Value<string>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(ignoreId)) _rptIgnore = 0;
|
|
|
|
|
else if (!ulong.TryParse(ignoreId, out _rptIgnore))
|
|
|
|
|
{
|
|
|
|
|
throw new RuleImportException("CacheIgnore was not set to a valid channel ID.");
|
|
|
|
|
}
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
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;
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
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-02 03:25:08 +00:00
|
|
|
|
}
|
2018-03-27 22:15:13 +00:00
|
|
|
|
catch (ArgumentException ex)
|
2018-03-02 03:25:08 +00:00
|
|
|
|
{
|
2018-03-27 22:15:13 +00:00
|
|
|
|
throw new RuleImportException(ex.Message);
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 22:15:13 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new RuleImportException("Section for QueryCommand is not correctly defined.");
|
|
|
|
|
}
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
2018-04-05 22:46:15 +00:00
|
|
|
|
|
|
|
|
|
private static Regex WebhookUrlParts =
|
|
|
|
|
new Regex(@"https?:\/\/discordapp.com\/api\/webhooks\/(\d+)\/([^/]+)?", RegexOptions.Compiled);
|
2018-03-02 03:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|