From 9818d4af89fb6d76f38aed6b874786b3b0d24db3 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Tue, 8 Aug 2017 12:36:49 -0700 Subject: [PATCH] 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. --- ConfigItem/EntityName.cs | 27 ++--------- Feature/ModTools/CommandListener.cs | 3 -- Feature/RegexResponder/EventProcessor.cs | 62 +++++++++--------------- 3 files changed, 28 insertions(+), 64 deletions(-) diff --git a/ConfigItem/EntityName.cs b/ConfigItem/EntityName.cs index 3ce6528..046ded7 100644 --- a/ConfigItem/EntityName.cs +++ b/ConfigItem/EntityName.cs @@ -1,6 +1,4 @@ -using System; - -namespace Noikoio.RegexBot.ConfigItem +namespace Noikoio.RegexBot.ConfigItem { 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. /// In configuration, entities are fully specified with a prefix (if necessary), an ID, two colons, and a name. /// - internal class EntityName + struct EntityName { - private ulong? _id; - private string _name; + private readonly ulong? _id; + private readonly string _name; private readonly EntityType _type; public ulong? Id => _id; @@ -60,23 +58,6 @@ namespace Noikoio.RegexBot.ConfigItem } } } - - /// - /// Updates this entity's ID value only if it was previously unknown. - /// Additionally logs a message suggesting to insert the ID into configuration. - /// - 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() { diff --git a/Feature/ModTools/CommandListener.cs b/Feature/ModTools/CommandListener.cs index 9397b78..d0aa9af 100644 --- a/Feature/ModTools/CommandListener.cs +++ b/Feature/ModTools/CommandListener.cs @@ -127,7 +127,6 @@ namespace Noikoio.RegexBot.Feature.ModTools if (string.Equals(item.Name, author.Nickname, StringComparison.OrdinalIgnoreCase) || string.Equals(item.Name, author.Username, StringComparison.OrdinalIgnoreCase)) { - item.UpdateId(author.Id); return true; } } @@ -146,7 +145,6 @@ namespace Noikoio.RegexBot.Feature.ModTools { if (string.Equals(item.Name, role.Name, StringComparison.OrdinalIgnoreCase)) { - item.UpdateId(role.Id); return true; } } @@ -164,7 +162,6 @@ namespace Noikoio.RegexBot.Feature.ModTools // Try get ID if (string.Equals(item.Name, m.Channel.Name, StringComparison.OrdinalIgnoreCase)) { - item.UpdateId(m.Channel.Id); return true; } } diff --git a/Feature/RegexResponder/EventProcessor.cs b/Feature/RegexResponder/EventProcessor.cs index 2d4d8c0..7e498ea 100644 --- a/Feature/RegexResponder/EventProcessor.cs +++ b/Feature/RegexResponder/EventProcessor.cs @@ -250,61 +250,52 @@ namespace Noikoio.RegexBot.Feature.RegexResponder return false; } - var author = m.Author as SocketGuildUser; + var guildauthor = 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)) + if (guildauthor != null && + string.Equals(item.Name, guildauthor.Nickname, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + if (string.Equals(item.Name, m.Author.Username, StringComparison.OrdinalIgnoreCase)) { - item.UpdateId(author.Id); return true; } } 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 - foreach (var role in author.Roles) + if (ignorelist.Roles.Any(listrole => { - if (string.Equals(item.Name, role.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)) + if (listrole.Id.HasValue) return listrole.Id == guildrole.Id; + else return string.Equals(listrole.Name, guildrole.Name, StringComparison.OrdinalIgnoreCase); + })) { - item.UpdateId(m.Channel.Id); 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; } @@ -367,11 +358,7 @@ namespace Noikoio.RegexBot.Feature.RegexResponder } else { - if (string.Equals(ei.Name, ch.Name, StringComparison.OrdinalIgnoreCase)) - { - ei.UpdateId(ch.Id); // Unnecessary, serves only to trigger the suggestion log message - return ch; - } + if (string.Equals(ei.Name, ch.Name, StringComparison.OrdinalIgnoreCase)) return ch; } } } @@ -389,7 +376,6 @@ namespace Noikoio.RegexBot.Feature.RegexResponder if (string.Equals(ei.Name, u.Username, 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(); } }