2017-06-23 19:31:47 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-08-08 19:48:30 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-07-22 03:51:00 +00:00
|
|
|
|
namespace Noikoio.RegexBot.Feature.RegexResponder
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-07-22 03:51:00 +00:00
|
|
|
|
/// Implements per-message regex matching and executes customizable responses.
|
|
|
|
|
/// Namesake of this project.
|
2017-06-23 19:31:47 +00:00
|
|
|
|
/// </summary>
|
2017-07-26 22:36:59 +00:00
|
|
|
|
partial class EventProcessor : BotFeature
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly DiscordSocketClient _client;
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
public override string Name => "RegexResponder";
|
|
|
|
|
|
|
|
|
|
public EventProcessor(DiscordSocketClient client) : base(client)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
|
|
|
|
|
_client.MessageReceived += OnMessageReceived;
|
|
|
|
|
_client.MessageUpdated += OnMessageUpdated;
|
|
|
|
|
|
|
|
|
|
_commands = new ReadOnlyDictionary<string, ResponseProcessor>(
|
|
|
|
|
new Dictionary<string, ResponseProcessor>() {
|
|
|
|
|
#if DEBUG
|
|
|
|
|
{ "crash", RP_Crash },
|
|
|
|
|
{ "dumpid", RP_DumpID },
|
|
|
|
|
#endif
|
|
|
|
|
{ "report", RP_Report },
|
|
|
|
|
{ "say", RP_Say },
|
|
|
|
|
{ "remove", RP_Remove },
|
|
|
|
|
{ "delete", RP_Remove },
|
|
|
|
|
{ "erase", RP_Remove },
|
|
|
|
|
{ "exec", RP_Exec },
|
|
|
|
|
{ "ban", RP_Ban },
|
|
|
|
|
{ "grantrole", RP_GrantRevokeRole },
|
|
|
|
|
{ "revokerole", RP_GrantRevokeRole }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Event handlers
|
|
|
|
|
private async Task OnMessageReceived(SocketMessage arg)
|
|
|
|
|
=> await ReceiveMessage(arg);
|
|
|
|
|
private async Task OnMessageUpdated(Cacheable<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
|
|
|
|
|
=> await ReceiveMessage(arg2);
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Receives incoming messages and creates tasks to handle them if necessary.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task ReceiveMessage(SocketMessage arg)
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
// Determine channel type - if not a guild channel, stop.
|
|
|
|
|
var ch = arg.Channel as SocketGuildChannel;
|
|
|
|
|
if (ch == null) return;
|
|
|
|
|
|
|
|
|
|
if (arg.Author == _client.CurrentUser) return; // Don't ever self-trigger
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
// Looking up server information and extracting settings
|
2017-07-26 22:36:59 +00:00
|
|
|
|
SocketGuild g = ch.Guild;
|
|
|
|
|
ServerConfig sd = null;
|
|
|
|
|
foreach (var item in RegexBot.Config.Servers)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (item.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// Finding server by ID
|
|
|
|
|
if (g.Id == item.Id)
|
|
|
|
|
{
|
|
|
|
|
sd = item;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Finding server by name and caching ID
|
|
|
|
|
if (string.Equals(item.Name, g.Name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
item.Id = g.Id;
|
|
|
|
|
sd = item;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await Logger.GetLogger(Configuration.LogPrefix)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
($"Suggestion: Server \"{item.Name}\" can be defined as \"{item.Id}::{item.Name}\"");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sd == null) return; // No server configuration found
|
2017-07-26 22:36:59 +00:00
|
|
|
|
var rules = GetConfig(ch.Guild.Id) as IEnumerable<RuleConfig>;
|
|
|
|
|
if (rules == null) return;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
// Further processing is sent to the thread pool
|
2017-07-26 22:36:59 +00:00
|
|
|
|
foreach (var rule in rules)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
await Task.Run(async () => await ProcessMessage(sd, rule, arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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>
|
2017-07-26 22:36:59 +00:00
|
|
|
|
private async Task ProcessMessage(ServerConfig srv, RuleConfig rule, SocketMessage msg)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
string msgcontent;
|
|
|
|
|
|
|
|
|
|
// Embed mode?
|
|
|
|
|
if (rule.MatchEmbeds)
|
|
|
|
|
{
|
|
|
|
|
var embeds = new StringBuilder();
|
|
|
|
|
foreach (var e in msg.Embeds) embeds.AppendLine(EmbedToString(e));
|
|
|
|
|
msgcontent = embeds.ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
msgcontent = msg.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Min/max message length check
|
|
|
|
|
if (rule.MinLength.HasValue && msgcontent.Length <= rule.MinLength.Value) return;
|
|
|
|
|
if (rule.MaxLength.HasValue && msgcontent.Length >= rule.MaxLength.Value) return;
|
|
|
|
|
|
|
|
|
|
// Moderator bypass check
|
2017-08-08 19:48:30 +00:00
|
|
|
|
if (rule.AllowModBypass == true && srv.Moderators.ExistsInList(msg)) return;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
// Individual rule filtering check
|
|
|
|
|
if (IsFiltered(rule, msg)) return;
|
|
|
|
|
|
|
|
|
|
// And finally, pattern matching checks
|
|
|
|
|
bool success = false;
|
|
|
|
|
foreach (var regex in rule.Regex)
|
|
|
|
|
{
|
|
|
|
|
success = regex.Match(msgcontent).Success;
|
|
|
|
|
if (success) break;
|
|
|
|
|
}
|
|
|
|
|
if (!success) return;
|
|
|
|
|
|
|
|
|
|
// Prepare to execute responses
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await Log($"\"{rule.DisplayName}\" triggered in {srv.Name}/#{msg.Channel} by {msg.Author.ToString()}");
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
foreach (string rcmd in rule.Responses)
|
|
|
|
|
{
|
|
|
|
|
string cmd = rcmd.TrimStart(' ').Split(' ')[0].ToLower();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ResponseProcessor response;
|
|
|
|
|
if (!_commands.TryGetValue(cmd, out response))
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await Log($"Unknown command defined in response: \"{cmd}\"");
|
2017-06-23 19:31:47 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await response.Invoke(rcmd, rule, msg);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await Log($"Encountered an error while processing \"{cmd}\". Details follow:");
|
|
|
|
|
await Log(ex.ToString());
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
[ConfigSection("rules")]
|
|
|
|
|
public override async Task<object> ProcessConfiguration(JToken configSection)
|
|
|
|
|
{
|
|
|
|
|
List<RuleConfig> rules = new List<RuleConfig>();
|
|
|
|
|
foreach (JObject ruleconf in configSection)
|
|
|
|
|
{
|
|
|
|
|
// Try and get at least the name before passing it to RuleItem
|
|
|
|
|
string name = ruleconf["name"]?.Value<string>();
|
|
|
|
|
if (name == null)
|
|
|
|
|
{
|
|
|
|
|
await Log("Display name not defined within a rule section.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
await Log($"Adding rule \"{name}\"");
|
|
|
|
|
|
|
|
|
|
RuleConfig rule;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
rule = new RuleConfig(ruleconf);
|
|
|
|
|
}
|
|
|
|
|
catch (RuleImportException ex)
|
|
|
|
|
{
|
|
|
|
|
await Log("-> Error: " + ex.Message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
rules.Add(rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rules.AsReadOnly();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
2017-06-23 19:31:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Turns an embed into a single string for regex matching purposes
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string EmbedToString(Embed e)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
if (e.Author.HasValue) result.AppendLine(e.Author.Value.Name ?? "" + e.Author.Value.Url ?? "");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(e.Title)) result.AppendLine(e.Title);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(e.Description)) result.AppendLine(e.Description);
|
|
|
|
|
|
|
|
|
|
foreach (var f in e.Fields)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(f.Name)) result.AppendLine(f.Name);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(f.Value)) result.AppendLine(f.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.Footer.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result.AppendLine(e.Footer.Value.Text ?? "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-22 03:51:00 +00:00
|
|
|
|
private bool IsFiltered(RuleConfig r, SocketMessage m)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (r.FilterMode == FilterType.None) return false;
|
|
|
|
|
|
2017-08-08 19:48:30 +00:00
|
|
|
|
bool inFilter = r.FilterList.ExistsInList(m);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
if (r.FilterMode == FilterType.Whitelist)
|
|
|
|
|
{
|
|
|
|
|
if (!inFilter) return true;
|
2017-08-08 19:48:30 +00:00
|
|
|
|
return r.FilterExemptions.ExistsInList(m);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
else if (r.FilterMode == FilterType.Blacklist)
|
|
|
|
|
{
|
|
|
|
|
if (!inFilter) return false;
|
2017-08-08 19:48:30 +00:00
|
|
|
|
return !r.FilterExemptions.ExistsInList(m);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false; // this shouldn't happen™
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string[] SplitParams(string cmd, int? limit = null)
|
|
|
|
|
{
|
|
|
|
|
if (limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return cmd.Split(new char[] { ' ' }, limit.Value, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ProcessText(string input, SocketMessage m)
|
|
|
|
|
{
|
|
|
|
|
// Maybe in the future this will do more.
|
|
|
|
|
// For now, replaces all instances of @_ with the message sender.
|
|
|
|
|
return input
|
|
|
|
|
.Replace("@_", m.Author.Mention)
|
|
|
|
|
.Replace("@\\_", "@_");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Receives a string (beginning with @ or #) and returns an object
|
|
|
|
|
/// suitable for sending out messages
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<IMessageChannel> GetMessageTargetAsync(string targetName, SocketMessage m)
|
|
|
|
|
{
|
|
|
|
|
const string AEShort = "Target name is too short.";
|
|
|
|
|
|
|
|
|
|
EntityType et;
|
|
|
|
|
if (targetName.Length <= 1) throw new ArgumentException(AEShort);
|
|
|
|
|
|
|
|
|
|
if (targetName[0] == '#') et = EntityType.Channel;
|
|
|
|
|
else if (targetName[0] == '@') et = EntityType.User;
|
|
|
|
|
else throw new ArgumentException("Target is not specified to be either a channel or user.");
|
|
|
|
|
|
|
|
|
|
targetName = targetName.Substring(1);
|
|
|
|
|
if (targetName == "_")
|
|
|
|
|
{
|
|
|
|
|
if (et == EntityType.Channel) return m.Channel;
|
2017-07-22 02:50:28 +00:00
|
|
|
|
else return await m.Author.GetOrCreateDMChannelAsync();
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EntityName ei = new EntityName(targetName, et);
|
|
|
|
|
SocketGuild g = ((SocketGuildUser)m.Author).Guild;
|
|
|
|
|
|
|
|
|
|
if (et == EntityType.Channel)
|
|
|
|
|
{
|
|
|
|
|
if (targetName.Length < 2 || targetName.Length > 100)
|
|
|
|
|
throw new ArgumentException(AEShort);
|
|
|
|
|
|
|
|
|
|
foreach (var ch in g.TextChannels)
|
|
|
|
|
{
|
|
|
|
|
if (ei.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (ei.Id.Value == ch.Id) return ch;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-08 19:36:49 +00:00
|
|
|
|
if (string.Equals(ei.Name, ch.Name, StringComparison.OrdinalIgnoreCase)) return ch;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ei.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// The easy way
|
2017-07-22 02:50:28 +00:00
|
|
|
|
return await _client.GetUser(ei.Id.Value).GetOrCreateDMChannelAsync();
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The hard way
|
|
|
|
|
foreach (var u in g.Users)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(ei.Name, u.Username, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(ei.Name, u.Nickname, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2017-07-22 02:50:28 +00:00
|
|
|
|
return await u.GetOrCreateDMChannelAsync();
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|