Added EntityList.ExistsInList

Replaces redundant code in both features.
This commit is contained in:
Noikoio 2017-08-08 12:48:30 -07:00
parent 9818d4af89
commit 7f8d6465ef
3 changed files with 69 additions and 130 deletions

View file

@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq; using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -63,6 +64,67 @@ namespace Noikoio.RegexBot.ConfigItem
+ $"{Users.Count()} user(s)"; + $"{Users.Count()} user(s)";
} }
/// <summary>
/// Checks if the parameters of the given <see cref="SocketMessage"/> match with the entities
/// specified in this list.
/// </summary>
/// <param name="msg">An incoming message.</param>
/// <returns>
/// True if the <see cref="SocketMessage"/> occurred within a channel specified in this list,
/// or if the message author belongs to one or more roles in this list, or if the user itself
/// is defined within this list.
/// </returns>
public bool ExistsInList(SocketMessage msg)
{
var guildauthor = msg.Author as SocketGuildUser;
foreach (var item in this.Users)
{
if (!item.Id.HasValue)
{
if (guildauthor != null &&
string.Equals(item.Name, guildauthor.Nickname, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(item.Name, msg.Author.Username, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
else
{
if (item.Id.Value == msg.Author.Id) return true;
}
}
if (guildauthor != null)
{
foreach (var guildrole in guildauthor.Roles)
{
if (this.Roles.Any(listrole =>
{
if (listrole.Id.HasValue) return listrole.Id == guildrole.Id;
else return string.Equals(listrole.Name, guildrole.Name, StringComparison.OrdinalIgnoreCase);
}))
{
return true;
}
}
foreach (var listchannel in this.Channels)
{
if (listchannel.Id.HasValue && listchannel.Id == msg.Channel.Id ||
string.Equals(listchannel.Name, msg.Channel.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
// No match.
return false;
}
/// <summary> /// <summary>
/// Helper method for reading whitelist and blacklist filtering lists /// Helper method for reading whitelist and blacklist filtering lists

View file

@ -35,8 +35,7 @@ namespace Noikoio.RegexBot.Feature.ModTools
if (sc == null) return; if (sc == null) return;
// Disregard if not a bot moderator // Disregard if not a bot moderator
// TODO have this and RegexResponder call the same relevant code if (!sc.Moderators.ExistsInList(arg)) return;
if (!IsInList(sc.Moderators, arg)) return;
// Disregard if the message contains a newline character // Disregard if the message contains a newline character
if (arg.Content.Contains("\n")) return; if (arg.Content.Contains("\n")) return;
@ -109,69 +108,5 @@ namespace Noikoio.RegexBot.Feature.ModTools
} }
public new Task Log(string text) => base.Log(text); public new Task Log(string text) => base.Log(text);
private bool IsInList(EntityList ignorelist, SocketMessage m)
{
if (ignorelist == null)
{
// This happens when getting a message from a server not defined in config.
return false;
}
var author = m.Author as SocketGuildUser;
foreach (var item in ignorelist.Users)
{
if (!item.Id.HasValue)
{
// Attempt to update ID if given nick matches
if (string.Equals(item.Name, author.Nickname, StringComparison.OrdinalIgnoreCase)
|| string.Equals(item.Name, author.Username, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
else
{
if (item.Id.Value == author.Id) return true;
}
}
foreach (var item in ignorelist.Roles)
{
if (!item.Id.HasValue)
{
// Try to update ID if none exists
foreach (var role in author.Roles)
{
if (string.Equals(item.Name, role.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
else
{
if (author.Roles.Any(r => r.Id == item.Id)) return true;
}
}
foreach (var item in ignorelist.Channels)
{
if (!item.Id.HasValue)
{
// Try get ID
if (string.Equals(item.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
else
{
if (item.Id == m.Channel.Id) return true;
}
}
return false;
}
} }
} }

View file

@ -1,13 +1,12 @@
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Noikoio.RegexBot.Feature.RegexResponder namespace Noikoio.RegexBot.Feature.RegexResponder
{ {
@ -127,7 +126,7 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
if (rule.MaxLength.HasValue && msgcontent.Length >= rule.MaxLength.Value) return; if (rule.MaxLength.HasValue && msgcontent.Length >= rule.MaxLength.Value) return;
// Moderator bypass check // Moderator bypass check
if (rule.AllowModBypass == true && IsInList(srv.Moderators, msg)) return; if (rule.AllowModBypass == true && srv.Moderators.ExistsInList(msg)) return;
// Individual rule filtering check // Individual rule filtering check
if (IsFiltered(rule, msg)) return; if (IsFiltered(rule, msg)) return;
@ -226,79 +225,22 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
{ {
if (r.FilterMode == FilterType.None) return false; if (r.FilterMode == FilterType.None) return false;
bool inFilter = IsInList(r.FilterList, m); bool inFilter = r.FilterList.ExistsInList(m);
if (r.FilterMode == FilterType.Whitelist) if (r.FilterMode == FilterType.Whitelist)
{ {
if (!inFilter) return true; if (!inFilter) return true;
return IsInList(r.FilterExemptions, m); return r.FilterExemptions.ExistsInList(m);
} }
else if (r.FilterMode == FilterType.Blacklist) else if (r.FilterMode == FilterType.Blacklist)
{ {
if (!inFilter) return false; if (!inFilter) return false;
return !IsInList(r.FilterExemptions, m); return !r.FilterExemptions.ExistsInList(m);
} }
return false; // this shouldn't happen™ return false; // this shouldn't happen™
} }
private bool IsInList(EntityList ignorelist, SocketMessage m)
{
if (ignorelist == null)
{
// This happens when getting a message from a server not defined in config.
return false;
}
var guildauthor = m.Author as SocketGuildUser;
foreach (var item in ignorelist.Users)
{
if (!item.Id.HasValue)
{
if (guildauthor != null &&
string.Equals(item.Name, guildauthor.Nickname, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(item.Name, m.Author.Username, StringComparison.OrdinalIgnoreCase))
{
return true;
}
} else
{
if (item.Id.Value == m.Author.Id) return true;
}
}
if (guildauthor != null)
{
foreach (var guildrole in guildauthor.Roles)
{
if (ignorelist.Roles.Any(listrole =>
{
if (listrole.Id.HasValue) return listrole.Id == guildrole.Id;
else return string.Equals(listrole.Name, guildrole.Name, StringComparison.OrdinalIgnoreCase);
}))
{
return true;
}
}
foreach (var listchannel in ignorelist.Channels)
{
if (listchannel.Id.HasValue && listchannel.Id == m.Channel.Id ||
string.Equals(listchannel.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
// No match.
return false;
}
private string[] SplitParams(string cmd, int? limit = null) private string[] SplitParams(string cmd, int? limit = null)
{ {
if (limit.HasValue) if (limit.HasValue)