From e87a985067704d64b55dc5d768d5804d8af07a43 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Sun, 16 Jun 2019 21:59:17 -0700 Subject: [PATCH] Finish adding entity finder helper methods --- Kerobot/Common/EntityName.cs | 137 ++++++++++++++--------------------- 1 file changed, 55 insertions(+), 82 deletions(-) diff --git a/Kerobot/Common/EntityName.cs b/Kerobot/Common/EntityName.cs index 24edd92..d0af483 100644 --- a/Kerobot/Common/EntityName.cs +++ b/Kerobot/Common/EntityName.cs @@ -108,87 +108,6 @@ namespace Kerobot.Common if (!Id.HasValue) Id = id; } - #region Name to ID resolving - /// - /// Attempts to determine the corresponding ID if not already known. - /// Searches the specified guild and stores it into this instance if found. - /// Places the ID into when and if the result is known. - /// - /// The entity type to which this instance corresponds to. - /// If known, outputs the ID of the corresponding entity. - /// Specifies if the internal ID value should be stored if a match is found. - /// True if the ID is known. - [Obsolete] - public bool TryResolve(SocketGuild searchGuild, out ulong id, bool keepId, EntityType searchType) - { - if (Id.HasValue) - { - id = Id.Value; - return true; - } - if (searchType != EntityType.Unspecified && Type == EntityType.Unspecified) Type = searchType; - if (string.IsNullOrWhiteSpace(Name)) - { - id = default; - return false; - } - - Predicate resolver; - IEnumerable collection; - switch (Type) - { - case EntityType.Role: - collection = searchGuild.Roles; - resolver = ResolveTryRole; - break; - case EntityType.Channel: - collection = searchGuild.TextChannels; - resolver = ResolveTryChannel; - break; - case EntityType.User: - collection = searchGuild.Users; - resolver = ResolveTryUser; - break; - default: - id = default; - return false; - } - - foreach (var item in collection) - { - if (resolver.Invoke(item)) - { - if (keepId) Id = item.Id; - id = Id.Value; - return true; - } - } - - id = default; - return false; - } - - private bool ResolveTryRole(ISnowflakeEntity entity) - { - var r = (SocketRole)entity; - return string.Equals(r.Name, this.Name, StringComparison.InvariantCultureIgnoreCase); - } - - private bool ResolveTryChannel(ISnowflakeEntity entity) - { - var c = (SocketTextChannel)entity; - return string.Equals(c.Name, this.Name, StringComparison.InvariantCultureIgnoreCase); - } - - private bool ResolveTryUser(ISnowflakeEntity entity) - { - var u = (SocketGuildUser)entity; - // Check username first, then nickname - return string.Equals(u.Username, this.Name, StringComparison.InvariantCultureIgnoreCase) - || string.Equals(u.Nickname, this.Name, StringComparison.InvariantCultureIgnoreCase); - } - #endregion - /// /// Returns the appropriate prefix corresponding to an EntityType. /// @@ -233,7 +152,7 @@ namespace Kerobot.Common if (this.Type != EntityType.Role) throw new ArgumentException("This EntityName instance must correspond to a Role."); - bool dirty = false; // flag to update ID if possible regardless of updateMissingID setting + bool dirty = false; // flag for updating ID if possible regardless of updateMissingId setting if (this.Id.HasValue) { var role = guild.GetRole(Id.Value); @@ -246,6 +165,60 @@ namespace Kerobot.Common return r; } + + /// + /// Attempts to find the corresponding user within the given guild. + /// + /// The guild in which to search for the user. + /// + /// Specifies if this EntityName instance should keep the snowflake ID of the + /// corresponding user found in this guild, if it is not already known by this instance. + /// + public SocketGuildUser FindUserIn(SocketGuild guild, bool updateMissingID = false) + { + if (this.Type != EntityType.User) + throw new ArgumentException("This EntityName instance must correspond to a User."); + + bool dirty = false; // flag for updating ID if possible regardless of updateMissingId setting + if (this.Id.HasValue) + { + var user = guild.GetUser(Id.Value); + if (user != null) return user; + else dirty = true; // only set if ID already existed but is now invalid + } + + var u = guild.Users.FirstOrDefault(rq => string.Equals(rq.Username, this.Name, StringComparison.OrdinalIgnoreCase)); + if (u != null && (updateMissingID || dirty)) this.Id = u.Id; + + return u; + } + + /// + /// Attempts to find the corresponding channel within the given guild. + /// + /// The guild in which to search for the channel. + /// + /// Specifies if this EntityName instance should keep the snowflake ID of the + /// corresponding channel found in this guild, if it is not already known by this instance. + /// + public SocketTextChannel FindChannelIn(SocketGuild guild, bool updateMissingID = false) + { + if (this.Type != EntityType.Channel) + throw new ArgumentException("This EntityName instance must correspond to a Channel."); + + bool dirty = false; // flag for updating ID if possible regardless of updateMissingId setting + if (this.Id.HasValue) + { + var channel = guild.GetTextChannel(Id.Value); + if (channel != null) return channel; + else dirty = true; // only set if ID already existed but is now invalid + } + + var c = guild.TextChannels.FirstOrDefault(rq => string.Equals(rq.Name, this.Name, StringComparison.OrdinalIgnoreCase)); + if (c != null && (updateMissingID || dirty)) this.Id = c.Id; + + return c; + } #endregion } }