Fixed BanCommand not accepting IDs of non-guild users

Also slightly improved the equivalent code for KickCommand
This commit is contained in:
Noikoio 2017-08-11 09:29:52 -07:00
parent 1761980275
commit 2358a8eec4
3 changed files with 30 additions and 26 deletions

View file

@ -3,7 +3,7 @@ using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Linq; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.ModTools namespace Noikoio.RegexBot.Feature.ModTools
@ -56,18 +56,19 @@ namespace Noikoio.RegexBot.Feature.ModTools
} }
} }
// Getting SocketGuildUser kick target (ensuring that it's the parameter) // Getting SocketGuildUser target
SocketGuildUser targetobj = null; Match m = UserMention.Match(targetstr);
if (UserMention.IsMatch(targetstr)) if (m.Success) targetstr = m.Groups["snowflake"].Value;
{
targetobj = msg.MentionedUsers.ElementAt(0) as SocketGuildUser;
}
else if (ulong.TryParse(targetstr, out var snowflake))
{
targetobj = g.GetUser(snowflake);
}
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.**"); await SendUsageMessage(msg, ":x: **Unable to determine the target user.**");
return; return;
@ -76,8 +77,8 @@ namespace Noikoio.RegexBot.Feature.ModTools
try try
{ {
if (reason != null) reason = Uri.EscapeDataString(reason); // TODO remove when fixed in library if (reason != null) reason = Uri.EscapeDataString(reason); // TODO remove when fixed in library
await g.AddBanAsync(targetobj, _purgeDays, reason); await g.AddBanAsync(targetuid, _purgeDays, reason);
await msg.Channel.SendMessageAsync($":white_check_mark: Banned user **{targetobj.ToString()}**."); await msg.Channel.SendMessageAsync($":white_check_mark: Banned user **{targetdisp}**.");
} }
catch (Discord.Net.HttpException ex) catch (Discord.Net.HttpException ex)
{ {

View file

@ -38,22 +38,25 @@ namespace Noikoio.RegexBot.Feature.ModTools
return; 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; SocketGuildUser targetobj = null;
if (UserMention.IsMatch(targetstr)) if (ulong.TryParse(targetstr, out var snowflake))
{
targetobj = msg.MentionedUsers.ElementAt(0) as SocketGuildUser;
}
else if (ulong.TryParse(targetstr, out var snowflake))
{ {
targetobj = g.GetUser(snowflake); targetobj = g.GetUser(snowflake);
}
if (targetobj == null) if (targetobj == null)
{ {
await SendUsageMessage(msg, ":x: **Unable to determine the target user.**"); await SendUsageMessage(msg, ":x: **Unable to determine the target user.**");
return; return;
} }
}
else
{
await SendUsageMessage(msg, ":x: **The given target is not valid.**");
return;
}
try try
{ {

View file

@ -4,7 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Noikoio.RegexBot</RootNamespace> <RootNamespace>Noikoio.RegexBot</RootNamespace>
<AssemblyVersion>1.1.2.0</AssemblyVersion> <AssemblyVersion>1.1.3.0</AssemblyVersion>
<Description>Highly configurable Discord moderation bot</Description> <Description>Highly configurable Discord moderation bot</Description>
<Authors>Noikoio</Authors> <Authors>Noikoio</Authors>
<Company /> <Company />