2022-07-21 01:55:08 +00:00
|
|
|
|
namespace RegexBot.Common;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper class that holds an entity's name, ID, or both.
|
|
|
|
|
/// Meant to be used during configuration processing in cases where the configuration expects
|
|
|
|
|
/// an entity name to be defined in a certain way which may or may not include its snowflake ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EntityName {
|
2018-06-13 22:55:02 +00:00
|
|
|
|
/// <summary>
|
2022-08-23 02:25:48 +00:00
|
|
|
|
/// The entity's type, as specified in configuration.
|
2018-06-13 22:55:02 +00:00
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public EntityType Type { get; private set; }
|
2018-06-13 22:55:02 +00:00
|
|
|
|
|
2022-08-23 02:25:48 +00:00
|
|
|
|
private ulong? _id;
|
2018-06-13 22:55:02 +00:00
|
|
|
|
/// <summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// Entity's unique ID value (snowflake). May be null if the value is not known.
|
2018-06-13 22:55:02 +00:00
|
|
|
|
/// </summary>
|
2022-08-23 02:25:48 +00:00
|
|
|
|
/// <remarks>
|
2022-08-24 03:12:39 +00:00
|
|
|
|
/// This property may be updated during runtime if instructed to update the ID for persistence.
|
2022-08-23 02:25:48 +00:00
|
|
|
|
/// </remarks>
|
|
|
|
|
public ulong? Id {
|
|
|
|
|
get => _id;
|
|
|
|
|
internal set => _id ??= value;
|
|
|
|
|
}
|
2018-06-13 22:55:02 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entity's name as specified in configuration. May be null if it was not specified.
|
|
|
|
|
/// </summary>
|
2022-08-23 02:25:48 +00:00
|
|
|
|
/// <remarks>This value is not updated during runtime.</remarks>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public string? Name { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new object instance from the given input string.
|
|
|
|
|
/// Documentation for the EntityName format can be found elsewhere in this project's documentation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Input string in EntityName format.</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException">Input string is null or blank.</exception>
|
|
|
|
|
/// <exception cref="ArgumentException">Input string cannot be resolved to an entity type.</exception>
|
|
|
|
|
public EntityName(string input) {
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
|
|
|
throw new ArgumentNullException(nameof(input), "Specified name is blank.");
|
2022-08-24 03:12:39 +00:00
|
|
|
|
if (input.Length < 2) throw new ArgumentException("Input is not in a valid entity name format.");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
2022-08-24 03:12:39 +00:00
|
|
|
|
if (input[0] == '&') Type = EntityType.Role;
|
|
|
|
|
else if (input[0] == '#') Type = EntityType.Channel;
|
|
|
|
|
else if (input[0] == '@') Type = EntityType.User;
|
|
|
|
|
else throw new ArgumentException("Entity type unable to be inferred by given input.");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
input = input[1..]; // Remove prefix
|
|
|
|
|
|
|
|
|
|
// Input contains ID/Label separator?
|
2022-06-18 06:23:25 +00:00
|
|
|
|
var separator = input.IndexOf("::");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (separator != -1) {
|
|
|
|
|
Name = input[(separator + 2)..];
|
|
|
|
|
if (ulong.TryParse(input.AsSpan(0, separator), out var parseOut)) {
|
|
|
|
|
// Got an ID.
|
|
|
|
|
Id = parseOut;
|
|
|
|
|
} else {
|
|
|
|
|
// It's not actually an ID. Assuming the entire string is a name.
|
|
|
|
|
Name = input;
|
|
|
|
|
Id = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// No separator. Input is either entirely an ID or entirely a Name.
|
|
|
|
|
if (ulong.TryParse(input, out var parseOut)) {
|
|
|
|
|
// ID without name.
|
|
|
|
|
Id = parseOut;
|
|
|
|
|
Name = null;
|
|
|
|
|
} else {
|
|
|
|
|
// Name without ID.
|
|
|
|
|
Name = input;
|
|
|
|
|
Id = null;
|
2018-06-13 22:55:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2018-06-13 22:55:02 +00:00
|
|
|
|
|
2022-08-23 02:25:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new object instance from the given input string.
|
|
|
|
|
/// Documentation for the EntityName format can be found elsewhere in this project's documentation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Input string in EntityName format.</param>
|
|
|
|
|
/// <param name="expectedType">The <see cref="EntityType"/> expected for this instance.</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException">Input string is null or blank.</exception>
|
|
|
|
|
/// <exception cref="ArgumentException">Input string cannot be resolved to an entity type.</exception>
|
|
|
|
|
/// <exception cref="FormatException">Input string was resolved to a type other than specified.</exception>
|
|
|
|
|
public EntityName(string input, EntityType expectedType) : this(input) {
|
|
|
|
|
if (Type != expectedType) throw new FormatException("Resolved EntityType does not match expected type.");
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
2019-02-17 23:59:02 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the appropriate prefix corresponding to an EntityType.
|
|
|
|
|
/// </summary>
|
2022-08-23 02:25:48 +00:00
|
|
|
|
public static char GetPrefix(EntityType t) => t switch {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
EntityType.Role => '&',
|
|
|
|
|
EntityType.Channel => '#',
|
|
|
|
|
EntityType.User => '@',
|
|
|
|
|
_ => '\0',
|
|
|
|
|
};
|
2019-02-17 23:59:02 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a string representation of this item in proper EntityName format.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override string ToString() {
|
2022-08-23 02:25:48 +00:00
|
|
|
|
var pf = GetPrefix(Type);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
if (Id.HasValue && Name != null)
|
|
|
|
|
return $"{pf}{Id.Value}::{Name}";
|
|
|
|
|
else if (Id.HasValue)
|
|
|
|
|
return $"{pf}{Id}";
|
|
|
|
|
else
|
|
|
|
|
return $"{pf}{Name}";
|
|
|
|
|
}
|
2019-02-17 23:59:02 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
#region Helper methods
|
2022-08-24 03:12:39 +00:00
|
|
|
|
// TODO convert all to extension methods
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find the corresponding role within the given guild.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guild">The guild in which to search for the role.</param>
|
|
|
|
|
/// <param name="updateMissingID">
|
2022-06-18 06:23:25 +00:00
|
|
|
|
/// Specifies if this EntityName instance should cache the snowflake ID of the
|
|
|
|
|
/// corresponding role found in this guild if it is not already known by this instance.
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// </param>
|
|
|
|
|
public SocketRole? FindRoleIn(SocketGuild guild, bool updateMissingID = false) {
|
|
|
|
|
if (Type != EntityType.Role)
|
|
|
|
|
throw new ArgumentException("This EntityName instance must correspond to a Role.");
|
|
|
|
|
|
2022-06-18 06:23:25 +00:00
|
|
|
|
var dirty = false; // flag for updating ID if possible regardless of updateMissingId setting
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (Id.HasValue) {
|
|
|
|
|
var role = guild.GetRole(Id.Value);
|
|
|
|
|
if (role != null) return role;
|
|
|
|
|
else dirty = true; // only set if ID already existed but is now invalid
|
2019-02-17 23:59:02 +00:00
|
|
|
|
}
|
2019-06-17 04:59:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var r = guild.Roles.FirstOrDefault(rq => string.Equals(rq.Name, Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
if (r != null && (updateMissingID || dirty)) Id = r.Id;
|
2019-06-17 04:59:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2019-06-17 04:59:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find the corresponding user within the given guild.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guild">The guild in which to search for the user.</param>
|
|
|
|
|
/// <param name="updateMissingID">
|
2022-06-18 06:23:25 +00:00
|
|
|
|
/// Specifies if this EntityName instance should cache the snowflake ID of the
|
|
|
|
|
/// corresponding user found in this guild if it is not already known by this instance.
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// </param>
|
|
|
|
|
public SocketGuildUser? FindUserIn(SocketGuild guild, bool updateMissingID = false) {
|
|
|
|
|
if (Type != EntityType.User)
|
|
|
|
|
throw new ArgumentException("This EntityName instance must correspond to a User.");
|
|
|
|
|
|
2022-06-18 06:23:25 +00:00
|
|
|
|
var dirty = false; // flag for updating ID if possible regardless of updateMissingId setting
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (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
|
2019-06-17 04:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
var u = guild.Users.FirstOrDefault(rq => string.Equals(rq.Username, Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
if (u != null && (updateMissingID || dirty)) Id = u.Id;
|
2019-06-17 04:59:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
return u;
|
|
|
|
|
}
|
2019-06-17 04:59:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find the corresponding channel within the given guild.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guild">The guild in which to search for the channel.</param>
|
|
|
|
|
/// <param name="updateMissingID">
|
2022-06-18 06:23:25 +00:00
|
|
|
|
/// Specifies if this EntityName instance should cache the snowflake ID of the
|
|
|
|
|
/// corresponding channel found in this guild if it is not already known by this instance.
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// </param>
|
|
|
|
|
public SocketTextChannel? FindChannelIn(SocketGuild guild, bool updateMissingID = false) {
|
|
|
|
|
if (Type != EntityType.Channel)
|
|
|
|
|
throw new ArgumentException("This EntityName instance must correspond to a Channel.");
|
|
|
|
|
|
2022-06-18 06:23:25 +00:00
|
|
|
|
var dirty = false; // flag for updating ID if possible regardless of updateMissingId setting
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (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
|
2019-06-17 04:59:17 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
var c = guild.TextChannels.FirstOrDefault(rq => string.Equals(rq.Name, Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
if (c != null && (updateMissingID || dirty)) Id = c.Id;
|
|
|
|
|
|
|
|
|
|
return c;
|
2018-06-13 22:55:02 +00:00
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
#endregion
|
2018-06-13 22:55:02 +00:00
|
|
|
|
}
|