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.Collections.Generic;
using System.Diagnostics;
@ -63,6 +64,67 @@ namespace Noikoio.RegexBot.ConfigItem
+ $"{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>
/// Helper method for reading whitelist and blacklist filtering lists

View file

@ -35,8 +35,7 @@ namespace Noikoio.RegexBot.Feature.ModTools
if (sc == null) return;
// Disregard if not a bot moderator
// TODO have this and RegexResponder call the same relevant code
if (!IsInList(sc.Moderators, arg)) return;
if (!sc.Moderators.ExistsInList(arg)) return;
// Disregard if the message contains a newline character
if (arg.Content.Contains("\n")) return;
@ -109,69 +108,5 @@ namespace Noikoio.RegexBot.Feature.ModTools
}
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.WebSocket;
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Noikoio.RegexBot.Feature.RegexResponder
{
@ -127,7 +126,7 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
if (rule.MaxLength.HasValue && msgcontent.Length >= rule.MaxLength.Value) return;
// 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
if (IsFiltered(rule, msg)) return;
@ -226,79 +225,22 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
{
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 (!inFilter) return true;
return IsInList(r.FilterExemptions, m);
return r.FilterExemptions.ExistsInList(m);
}
else if (r.FilterMode == FilterType.Blacklist)
{
if (!inFilter) return false;
return !IsInList(r.FilterExemptions, m);
return !r.FilterExemptions.ExistsInList(m);
}
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)
{
if (limit.HasValue)