Removed EntityItem.UpdateId

Will no longer determine and store snowflake IDs for each entity
defined in configuration. Instead, it will be strongly
recommended in future documentation that users make use of IDs to
define such things.
This commit is contained in:
Noikoio 2017-08-08 12:36:49 -07:00
parent e3d40a5f60
commit 9818d4af89
3 changed files with 28 additions and 64 deletions

View file

@ -1,6 +1,4 @@
using System; namespace Noikoio.RegexBot.ConfigItem
namespace Noikoio.RegexBot.ConfigItem
{ {
enum EntityType { Channel, Role, User } enum EntityType { Channel, Role, User }
@ -10,10 +8,10 @@ namespace Noikoio.RegexBot.ConfigItem
/// over the entity's string-based name, as it can change at any time. /// over the entity's string-based name, as it can change at any time.
/// In configuration, entities are fully specified with a prefix (if necessary), an ID, two colons, and a name. /// In configuration, entities are fully specified with a prefix (if necessary), an ID, two colons, and a name.
/// </summary> /// </summary>
internal class EntityName struct EntityName
{ {
private ulong? _id; private readonly ulong? _id;
private string _name; private readonly string _name;
private readonly EntityType _type; private readonly EntityType _type;
public ulong? Id => _id; public ulong? Id => _id;
@ -60,23 +58,6 @@ namespace Noikoio.RegexBot.ConfigItem
} }
} }
} }
/// <summary>
/// Updates this entity's ID value only if it was previously unknown.
/// Additionally logs a message suggesting to insert the ID into configuration.
/// </summary>
public void UpdateId(ulong id)
{
// TODO not in here, but references to this have a lot of boilerplate around it. how to fix?
if (_id.HasValue) return;
_id = id;
var log = Logger.GetLogger(Configuration.LogPrefix);
var thisstr = this.ToString();
log(String.Format(
"Suggestion: \"{0}\" may be written in configuration as \"{1}\"",
(Type == EntityType.Role ? "" : thisstr.Substring(0, 1)) + Name, thisstr));
}
public override string ToString() public override string ToString()
{ {

View file

@ -127,7 +127,6 @@ namespace Noikoio.RegexBot.Feature.ModTools
if (string.Equals(item.Name, author.Nickname, StringComparison.OrdinalIgnoreCase) if (string.Equals(item.Name, author.Nickname, StringComparison.OrdinalIgnoreCase)
|| string.Equals(item.Name, author.Username, StringComparison.OrdinalIgnoreCase)) || string.Equals(item.Name, author.Username, StringComparison.OrdinalIgnoreCase))
{ {
item.UpdateId(author.Id);
return true; return true;
} }
} }
@ -146,7 +145,6 @@ namespace Noikoio.RegexBot.Feature.ModTools
{ {
if (string.Equals(item.Name, role.Name, StringComparison.OrdinalIgnoreCase)) if (string.Equals(item.Name, role.Name, StringComparison.OrdinalIgnoreCase))
{ {
item.UpdateId(role.Id);
return true; return true;
} }
} }
@ -164,7 +162,6 @@ namespace Noikoio.RegexBot.Feature.ModTools
// Try get ID // Try get ID
if (string.Equals(item.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase)) if (string.Equals(item.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase))
{ {
item.UpdateId(m.Channel.Id);
return true; return true;
} }
} }

View file

@ -250,61 +250,52 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
return false; return false;
} }
var author = m.Author as SocketGuildUser; var guildauthor = m.Author as SocketGuildUser;
foreach (var item in ignorelist.Users) foreach (var item in ignorelist.Users)
{ {
if (!item.Id.HasValue) if (!item.Id.HasValue)
{ {
// Attempt to update ID if given nick matches if (guildauthor != null &&
if (string.Equals(item.Name, author.Nickname, StringComparison.OrdinalIgnoreCase) string.Equals(item.Name, guildauthor.Nickname, StringComparison.OrdinalIgnoreCase))
|| string.Equals(item.Name, author.Username, StringComparison.OrdinalIgnoreCase)) {
return true;
}
if (string.Equals(item.Name, m.Author.Username, StringComparison.OrdinalIgnoreCase))
{ {
item.UpdateId(author.Id);
return true; return true;
} }
} else } else
{ {
if (item.Id.Value == author.Id) return true; if (item.Id.Value == m.Author.Id) return true;
} }
} }
foreach (var item in ignorelist.Roles) if (guildauthor != null)
{ {
if (!item.Id.HasValue) foreach (var guildrole in guildauthor.Roles)
{ {
// Try to update ID if none exists if (ignorelist.Roles.Any(listrole =>
foreach (var role in author.Roles)
{ {
if (string.Equals(item.Name, role.Name, StringComparison.OrdinalIgnoreCase)) if (listrole.Id.HasValue) return listrole.Id == guildrole.Id;
{ else return string.Equals(listrole.Name, guildrole.Name, StringComparison.OrdinalIgnoreCase);
item.UpdateId(role.Id); }))
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))
{ {
item.UpdateId(m.Channel.Id);
return true; return true;
} }
} }
else
foreach (var listchannel in ignorelist.Channels)
{ {
if (item.Id == m.Channel.Id) return true; if (listchannel.Id.HasValue && listchannel.Id == m.Channel.Id ||
string.Equals(listchannel.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
} }
} }
// No match.
return false; return false;
} }
@ -367,11 +358,7 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
} }
else else
{ {
if (string.Equals(ei.Name, ch.Name, StringComparison.OrdinalIgnoreCase)) if (string.Equals(ei.Name, ch.Name, StringComparison.OrdinalIgnoreCase)) return ch;
{
ei.UpdateId(ch.Id); // Unnecessary, serves only to trigger the suggestion log message
return ch;
}
} }
} }
} }
@ -389,7 +376,6 @@ namespace Noikoio.RegexBot.Feature.RegexResponder
if (string.Equals(ei.Name, u.Username, StringComparison.OrdinalIgnoreCase) || if (string.Equals(ei.Name, u.Username, StringComparison.OrdinalIgnoreCase) ||
string.Equals(ei.Name, u.Nickname, StringComparison.OrdinalIgnoreCase)) string.Equals(ei.Name, u.Nickname, StringComparison.OrdinalIgnoreCase))
{ {
ei.UpdateId(u.Id); // As mentioned above, serves only to trigger the suggestion log
return await u.GetOrCreateDMChannelAsync(); return await u.GetOrCreateDMChannelAsync();
} }
} }