From 2358a8eec460dd5bac37efc91be7e3285398dc60 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Fri, 11 Aug 2017 09:29:52 -0700 Subject: [PATCH] Fixed BanCommand not accepting IDs of non-guild users Also slightly improved the equivalent code for KickCommand --- Feature/ModTools/BanCommand.cs | 29 +++++++++++++++-------------- Feature/ModTools/KickCommand.cs | 23 +++++++++++++---------- RegexBot.csproj | 4 ++-- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Feature/ModTools/BanCommand.cs b/Feature/ModTools/BanCommand.cs index e9afe50..11aafe9 100644 --- a/Feature/ModTools/BanCommand.cs +++ b/Feature/ModTools/BanCommand.cs @@ -3,7 +3,7 @@ using Discord.WebSocket; using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using System; -using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Noikoio.RegexBot.Feature.ModTools @@ -56,18 +56,19 @@ namespace Noikoio.RegexBot.Feature.ModTools } } - // Getting SocketGuildUser kick target (ensuring that it's the parameter) - SocketGuildUser targetobj = null; - if (UserMention.IsMatch(targetstr)) - { - targetobj = msg.MentionedUsers.ElementAt(0) as SocketGuildUser; - } - else if (ulong.TryParse(targetstr, out var snowflake)) - { - targetobj = g.GetUser(snowflake); - } + // Getting SocketGuildUser target + Match m = UserMention.Match(targetstr); + if (m.Success) targetstr = m.Groups["snowflake"].Value; - if (targetobj == null) + SocketGuildUser targetobj = null; + ulong targetuid; + string targetdisp; + if (ulong.TryParse(targetstr, out targetuid)) + { + targetobj = g.GetUser(targetuid); + targetdisp = (targetobj == null ? $"with ID {targetuid}" : targetobj.ToString()); + } + else { await SendUsageMessage(msg, ":x: **Unable to determine the target user.**"); return; @@ -76,8 +77,8 @@ namespace Noikoio.RegexBot.Feature.ModTools try { if (reason != null) reason = Uri.EscapeDataString(reason); // TODO remove when fixed in library - await g.AddBanAsync(targetobj, _purgeDays, reason); - await msg.Channel.SendMessageAsync($":white_check_mark: Banned user **{targetobj.ToString()}**."); + await g.AddBanAsync(targetuid, _purgeDays, reason); + await msg.Channel.SendMessageAsync($":white_check_mark: Banned user **{targetdisp}**."); } catch (Discord.Net.HttpException ex) { diff --git a/Feature/ModTools/KickCommand.cs b/Feature/ModTools/KickCommand.cs index e0d162e..9112391 100644 --- a/Feature/ModTools/KickCommand.cs +++ b/Feature/ModTools/KickCommand.cs @@ -38,23 +38,26 @@ namespace Noikoio.RegexBot.Feature.ModTools return; } - // Getting SocketGuildUser kick target (ensuring that it's the parameter) + // Getting SocketGuildUser target + Match m = UserMention.Match(targetstr); + if (m.Success) targetstr = m.Groups["snowflake"].Value; + SocketGuildUser targetobj = null; - if (UserMention.IsMatch(targetstr)) - { - targetobj = msg.MentionedUsers.ElementAt(0) as SocketGuildUser; - } - else if (ulong.TryParse(targetstr, out var snowflake)) + if (ulong.TryParse(targetstr, out var snowflake)) { targetobj = g.GetUser(snowflake); + if (targetobj == null) + { + await SendUsageMessage(msg, ":x: **Unable to determine the target user.**"); + return; + } } - - if (targetobj == null) + else { - await SendUsageMessage(msg, ":x: **Unable to determine the target user.**"); + await SendUsageMessage(msg, ":x: **The given target is not valid.**"); return; } - + try { if (reason != null) reason = Uri.EscapeDataString(reason); // TODO remove when fixed in library diff --git a/RegexBot.csproj b/RegexBot.csproj index 68d052a..ff1966e 100644 --- a/RegexBot.csproj +++ b/RegexBot.csproj @@ -4,7 +4,7 @@ Exe netcoreapp1.1 Noikoio.RegexBot - 1.1.2.0 + 1.1.3.0 Highly configurable Discord moderation bot Noikoio @@ -26,4 +26,4 @@ - \ No newline at end of file +