Fix ping-like query failing; other small changes

This commit is contained in:
Noi 2022-07-12 23:45:18 -07:00
parent 9e09800bff
commit dc61e93a7f
4 changed files with 24 additions and 14 deletions

View file

@ -5,7 +5,7 @@ class ModuleConfig {
public EntityName Role { get; } public EntityName Role { get; }
public ModuleConfig(JObject conf) { public ModuleConfig(JObject conf) {
var cfgRole = conf["Role"]?.Value<string>(); var cfgRole = conf[nameof(Role)]?.Value<string>();
if (string.IsNullOrWhiteSpace(cfgRole)) if (string.IsNullOrWhiteSpace(cfgRole))
throw new ModuleLoadException("Role was not specified."); throw new ModuleLoadException("Role was not specified.");
Role = new EntityName(cfgRole); Role = new EntityName(cfgRole);

View file

@ -141,7 +141,7 @@ class ResponseExecutor {
result = await _bot.KickAsync(_guild, $"Rule '{_rule.Label}'", _user.Id, result = await _bot.KickAsync(_guild, $"Rule '{_rule.Label}'", _user.Id,
parameter, _rule.NotifyUserOfRemoval); parameter, _rule.NotifyUserOfRemoval);
} }
if (result.ErrorForbidden) return FromError(ForbiddenGenericError); if (result.ErrorForbidden) return FromError(Strings.ForbiddenGenericError);
if (result.ErrorNotFound) return FromError("The target user is no longer in the server."); if (result.ErrorNotFound) return FromError("The target user is no longer in the server.");
if (_rule.NotifyChannelOfRemoval) await _msg.Channel.SendMessageAsync(result.GetResultString(_bot)); if (_rule.NotifyChannelOfRemoval) await _msg.Channel.SendMessageAsync(result.GetResultString(_bot));
return FromSuccess(result.MessageSendSuccess ? null : "Unable to send notification DM."); return FromSuccess(result.MessageSendSuccess ? null : "Unable to send notification DM.");

View file

@ -2,12 +2,18 @@
using Discord.WebSocket; using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace RegexBot.Common; namespace RegexBot.Common;
/// <summary> /// <summary>
/// Miscellaneous utility methods useful for the bot and modules. /// Miscellaneous utility methods useful for the bot and modules.
/// </summary> /// </summary>
public static class Utilities { public static class Utilities {
public static Regex ChannelMention { get; } = new(@"<#(?<snowflake>\d+)>", RegexOptions.Compiled);
public static Regex CustomEmoji { get; } = new(@"<:(?<name>[A-Za-z0-9_]{2,}):(?<ID>\d+)>", RegexOptions.Compiled);
public static Regex DiscriminatorSearch { get; } = new(@"(.+)#(\d{4}(?!\d))", RegexOptions.Compiled);
public static Regex UserMention { get; } = new(@"<@!?(?<snowflake>\d+)>", RegexOptions.Compiled);
/// <summary> /// <summary>
/// Performs common checks on the specified message to see if it fits all the criteria of a /// Performs common checks on the specified message to see if it fits all the criteria of a
/// typical, ordinary message sent by an ordinary guild user. /// typical, ordinary message sent by an ordinary guild user.

View file

@ -1,6 +1,6 @@
using Discord.WebSocket; using Discord.WebSocket;
using RegexBot.Common;
using RegexBot.Data; using RegexBot.Data;
using System.Text.RegularExpressions;
namespace RegexBot.Services.EntityCache; namespace RegexBot.Services.EntityCache;
/// <summary> /// <summary>
@ -10,8 +10,6 @@ namespace RegexBot.Services.EntityCache;
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
class UserCachingSubservice { class UserCachingSubservice {
private static Regex DiscriminatorSearch { get; } = new(@"(.+)#(\d{4}(?!\d))", RegexOptions.Compiled);
internal UserCachingSubservice(RegexbotClient bot) { internal UserCachingSubservice(RegexbotClient bot) {
bot.DiscordClient.GuildMembersDownloaded += DiscordClient_GuildMembersDownloaded; bot.DiscordClient.GuildMembersDownloaded += DiscordClient_GuildMembersDownloaded;
bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated; bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated;
@ -84,15 +82,18 @@ class UserCachingSubservice {
return query.SingleOrDefault(); return query.SingleOrDefault();
} }
// Is search just a number? Assume ID, pass it on to the correct place. // Is search actually a ping? Extract ID.
if (ulong.TryParse(search, out var searchid)) { var m = Utilities.UserMention.Match(search);
if (m.Success) search = m.Groups["snowflake"].Value;
// Is search a number? Assume ID, proceed to query.
if (ulong.TryParse(search, out var searchid)) {
var idres = innerQuery(searchid, null); var idres = innerQuery(searchid, null);
if (idres != null) return idres; if (idres != null) return idres;
} }
// If the above fails, assume the number may be a string to search. // All of the above failed. Assume the number may be a string to search.
var namesplit = SplitNameAndDiscriminator(search); var namesplit = SplitNameAndDiscriminator(search);
return innerQuery(null, namesplit); return innerQuery(null, namesplit);
} }
@ -114,22 +115,25 @@ class UserCachingSubservice {
return query.SingleOrDefault(); return query.SingleOrDefault();
} }
// Is search just a number? Assume ID, pass it on to the correct place. // Is search actually a ping? Extract ID.
var m = Utilities.UserMention.Match(search);
if (m.Success) search = m.Groups["snowflake"].Value;
// Is search a number? Assume ID, proceed to query.
if (ulong.TryParse(search, out var searchid)) { if (ulong.TryParse(search, out var searchid)) {
var idres = innerQuery(guildId, searchid, null); var idres = innerQuery(guildId, searchid, null);
if (idres != null) return idres; if (idres != null) return idres;
} }
// If the above fails, assume the number may be a string to search. // All of the above failed. Assume the number may be a string to search.
var namesplit = SplitNameAndDiscriminator(search); var namesplit = SplitNameAndDiscriminator(search);
return innerQuery(guildId, null, namesplit); return innerQuery(guildId, null, namesplit);
} }
private static (string, string?) SplitNameAndDiscriminator(string input) { private static (string, string?) SplitNameAndDiscriminator(string input) {
string name; string name;
string? disc = null; string? disc = null;
var split = DiscriminatorSearch.Match(input); var split = Utilities.DiscriminatorSearch.Match(input);
if (split.Success) { if (split.Success) {
name = split.Groups[1].Value; name = split.Groups[1].Value;
disc = split.Groups[2].Value; disc = split.Groups[2].Value;