2017-08-06 20:05:44 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
2018-01-19 08:23:54 +00:00
|
|
|
|
using System.Linq;
|
2017-08-11 16:29:52 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
namespace Noikoio.RegexBot.Module.ModTools.Commands
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2017-10-09 18:54:12 +00:00
|
|
|
|
class BanKick : CommandBase
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2017-10-09 18:54:12 +00:00
|
|
|
|
// Ban and kick commands are highly similar in implementation, and thus are handled in a single class.
|
|
|
|
|
protected enum CommandMode { Ban, Kick }
|
|
|
|
|
private readonly CommandMode _mode;
|
|
|
|
|
|
2017-08-06 20:05:44 +00:00
|
|
|
|
private readonly bool _forceReason;
|
|
|
|
|
private readonly int _purgeDays;
|
2017-10-09 18:54:12 +00:00
|
|
|
|
private readonly string _successMsg;
|
2017-11-29 23:20:45 +00:00
|
|
|
|
private readonly string _notifyMsg;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
|
|
|
|
// Configuration:
|
|
|
|
|
// "forcereason" - boolean; Force a reason to be given. Defaults to false.
|
2017-10-09 18:54:12 +00:00
|
|
|
|
// "purgedays" - integer; Number of days of target's post history to delete, if banning.
|
|
|
|
|
// Must be between 0-7 inclusive. Defaults to 0.
|
|
|
|
|
// "successmsg" - Message to display on command success. Overrides default.
|
2017-11-29 23:20:45 +00:00
|
|
|
|
// "notifymsg" - Message to send to the target user being acted upon. Default message is used
|
|
|
|
|
// if the value is not specified. If a blank value is given, the feature is disabled.
|
|
|
|
|
// Takes the special values $s for server name and $r for reason text.
|
2017-10-09 18:54:12 +00:00
|
|
|
|
protected BanKick(ModTools l, string label, JObject conf, CommandMode mode) : base(l, label, conf)
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2017-10-09 18:54:12 +00:00
|
|
|
|
_mode = mode;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
_forceReason = conf["forcereason"]?.Value<bool>() ?? false;
|
|
|
|
|
_purgeDays = conf["purgedays"]?.Value<int>() ?? 0;
|
2017-10-09 18:54:12 +00:00
|
|
|
|
if (_mode == CommandMode.Ban && (_purgeDays > 7 || _purgeDays < 0))
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
|
|
|
|
throw new RuleImportException("The value of 'purgedays' must be between 0 and 7.");
|
|
|
|
|
}
|
2017-10-09 18:54:12 +00:00
|
|
|
|
_successMsg = conf["successmsg"]?.Value<string>();
|
2017-11-29 23:20:45 +00:00
|
|
|
|
if (conf["notifymsg"] == null)
|
|
|
|
|
{
|
|
|
|
|
// Message not specified - use default
|
2018-01-19 08:57:36 +00:00
|
|
|
|
_notifyMsg = string.Format(NotifyDefault, mode == CommandMode.Ban ? "banned" : "kicked");
|
2017-11-29 23:20:45 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string val = conf["notifymsg"].Value<string>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(val)) _notifyMsg = null; // empty value - disable message
|
|
|
|
|
else _notifyMsg = val;
|
|
|
|
|
}
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 08:57:36 +00:00
|
|
|
|
#region Strings
|
|
|
|
|
const string FailPrefix = ":x: **Failed to {0} user:** ";
|
|
|
|
|
const string Fail403 = "I do not have the required permissions to perform that action.";
|
|
|
|
|
const string Fail404 = "The target user is no longer available.";
|
|
|
|
|
const string FailDefault = "An unknown error occurred. Notify the bot operator.";
|
|
|
|
|
const string NotifyDefault = "You have been {0} from $s for the following reason:\n$r";
|
|
|
|
|
const string NotifyReasonNone = "No reason specified.";
|
|
|
|
|
const string NotifyFailed = "\n(User was unable to receive notification message.)";
|
|
|
|
|
const string ReasonRequired = ":x: **You must specify a reason.**";
|
|
|
|
|
const string TargetNotFound = ":x: **Unable to determine the target user.**";
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-08-06 20:05:44 +00:00
|
|
|
|
// Usage: (command) (mention) (reason)
|
|
|
|
|
public override async Task Invoke(SocketGuild g, SocketMessage msg)
|
|
|
|
|
{
|
|
|
|
|
string[] line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
string targetstr;
|
2017-11-29 23:20:45 +00:00
|
|
|
|
string reason;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
if (line.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
await SendUsageMessage(msg, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
targetstr = line[1];
|
|
|
|
|
|
|
|
|
|
if (line.Length == 3)
|
|
|
|
|
{
|
2017-11-29 23:20:45 +00:00
|
|
|
|
// Reason given - keep it
|
|
|
|
|
reason = line[2];
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// No reason given
|
|
|
|
|
if (_forceReason)
|
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await SendUsageMessage(msg, ReasonRequired);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-29 23:20:45 +00:00
|
|
|
|
reason = null;
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 16:29:52 +00:00
|
|
|
|
// Getting SocketGuildUser target
|
2018-01-19 08:23:54 +00:00
|
|
|
|
SocketGuildUser targetobj = null;
|
|
|
|
|
|
|
|
|
|
// Extract snowflake value from mention (if a mention was given)
|
2017-08-11 16:29:52 +00:00
|
|
|
|
Match m = UserMention.Match(targetstr);
|
|
|
|
|
if (m.Success) targetstr = m.Groups["snowflake"].Value;
|
|
|
|
|
|
2018-03-05 02:04:10 +00:00
|
|
|
|
EntityCache.CacheUser qres;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
qres = (await EntityCache.EntityCache.QueryAsync(g.Id, targetstr)).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
catch (Npgsql.NpgsqlException ex)
|
|
|
|
|
{
|
|
|
|
|
await Log("A database error occurred during user lookup: " + ex.Message);
|
|
|
|
|
await msg.Channel.SendMessageAsync(FailPrefix + FailDefault);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-19 08:23:54 +00:00
|
|
|
|
if (qres == null)
|
2017-08-06 20:05:44 +00:00
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await SendUsageMessage(msg, TargetNotFound);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-19 08:23:54 +00:00
|
|
|
|
ulong targetuid = qres.UserId;
|
|
|
|
|
targetobj = g.GetUser(targetuid);
|
|
|
|
|
string targetdisp = targetobj?.ToString() ?? $"ID {targetuid}";
|
2017-08-06 20:05:44 +00:00
|
|
|
|
|
2017-10-09 18:54:12 +00:00
|
|
|
|
if (_mode == CommandMode.Kick && targetobj == null)
|
|
|
|
|
{
|
|
|
|
|
// Can't kick without obtaining the user object
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await SendUsageMessage(msg, TargetNotFound);
|
2017-10-09 18:54:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:20:45 +00:00
|
|
|
|
// Send out message
|
2018-01-19 08:57:36 +00:00
|
|
|
|
var notifyTask = SendNotificationMessage(targetobj, reason);
|
2017-11-29 23:20:45 +00:00
|
|
|
|
|
|
|
|
|
// Do the action
|
2017-08-06 20:05:44 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-29 23:20:45 +00:00
|
|
|
|
string reasonlog = $"Invoked by {msg.Author.ToString()}.";
|
|
|
|
|
if (reason != null) reasonlog += $" Reason: {reason}";
|
|
|
|
|
reasonlog = Uri.EscapeDataString(reasonlog);
|
2017-10-21 19:05:46 +00:00
|
|
|
|
#warning Remove EscapeDataString call on next Discord.Net update
|
2017-12-08 18:29:32 +00:00
|
|
|
|
#if !DEBUG
|
2017-11-29 23:20:45 +00:00
|
|
|
|
if (_mode == CommandMode.Ban) await g.AddBanAsync(targetuid, _purgeDays, reasonlog);
|
2017-10-09 18:54:12 +00:00
|
|
|
|
else await targetobj.KickAsync(reason);
|
2017-12-08 18:29:32 +00:00
|
|
|
|
#else
|
|
|
|
|
#warning "Actual kick/ban action is DISABLED during debug."
|
|
|
|
|
#endif
|
2017-10-09 18:54:12 +00:00
|
|
|
|
string resultmsg = BuildSuccessMessage(targetdisp);
|
2018-01-19 08:57:36 +00:00
|
|
|
|
if (await notifyTask == false) resultmsg += NotifyFailed;
|
2017-10-09 18:54:12 +00:00
|
|
|
|
await msg.Channel.SendMessageAsync(resultmsg);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Discord.Net.HttpException ex)
|
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
string err = string.Format(FailPrefix, (_mode == CommandMode.Ban ? "ban" : "kick"));
|
2017-08-06 20:05:44 +00:00
|
|
|
|
if (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
|
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await msg.Channel.SendMessageAsync(err + Fail403);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
else if (ex.HttpCode == System.Net.HttpStatusCode.NotFound)
|
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await msg.Channel.SendMessageAsync(err + Fail404);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-01-19 08:57:36 +00:00
|
|
|
|
await msg.Channel.SendMessageAsync(err + FailDefault);
|
2017-08-06 20:05:44 +00:00
|
|
|
|
await Log(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 08:57:36 +00:00
|
|
|
|
// Returns true on message send success
|
|
|
|
|
private async Task<bool> SendNotificationMessage(SocketGuildUser target, string reason)
|
|
|
|
|
{
|
|
|
|
|
if (_notifyMsg == null) return true;
|
|
|
|
|
if (target == null) return false;
|
|
|
|
|
|
|
|
|
|
var ch = await target.GetOrCreateDMChannelAsync();
|
|
|
|
|
string outresult = _notifyMsg;
|
2018-02-07 20:47:51 +00:00
|
|
|
|
outresult = outresult.Replace("$s", target.Guild.Name);
|
2018-01-19 08:57:36 +00:00
|
|
|
|
outresult = outresult.Replace("$r", reason ?? NotifyReasonNone);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ch.SendMessageAsync(outresult);
|
|
|
|
|
}
|
|
|
|
|
catch (Discord.Net.HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
await Log("Failed to send out notification to target over DM: "
|
|
|
|
|
+ Enum.GetName(typeof(System.Net.HttpStatusCode), ex.HttpCode));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 20:05:44 +00:00
|
|
|
|
private async Task SendUsageMessage(SocketMessage m, string message)
|
|
|
|
|
{
|
|
|
|
|
string desc = $"{this.Command} [user or user ID] " + (_forceReason ? "[reason]" : "*[reason]*") + "\n";
|
|
|
|
|
desc += "Removes the given user from this server and prevents the user from rejoining. ";
|
|
|
|
|
desc += (_forceReason ? "L" : "Optionally l") + "ogs the reason for the ban to the Audit Log.";
|
|
|
|
|
if (_purgeDays > 0)
|
|
|
|
|
desc += $"\nAdditionally removes the user's post history for the last {_purgeDays} day(s).";
|
|
|
|
|
|
|
|
|
|
var usageEmbed = new EmbedBuilder()
|
|
|
|
|
{
|
|
|
|
|
Title = "Usage",
|
|
|
|
|
Description = desc
|
|
|
|
|
};
|
|
|
|
|
await m.Channel.SendMessageAsync(message ?? "", embed: usageEmbed);
|
|
|
|
|
}
|
2017-10-09 18:54:12 +00:00
|
|
|
|
|
|
|
|
|
private string BuildSuccessMessage(string targetstr)
|
|
|
|
|
{
|
|
|
|
|
const string defaultmsgBan = ":white_check_mark: Banned user **$target**.";
|
|
|
|
|
const string defaultmsgKick = ":white_check_mark: Kicked user **$target**.";
|
|
|
|
|
|
|
|
|
|
string msg = _successMsg ?? (_mode == CommandMode.Ban ? defaultmsgBan : defaultmsgKick);
|
|
|
|
|
|
|
|
|
|
return msg.Replace("$target", targetstr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Ban : BanKick
|
|
|
|
|
{
|
|
|
|
|
public Ban(ModTools l, string label, JObject conf)
|
|
|
|
|
: base(l, label, conf, CommandMode.Ban) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Kick : BanKick
|
|
|
|
|
{
|
|
|
|
|
public Kick(ModTools l, string label, JObject conf)
|
|
|
|
|
: base(l, label, conf, CommandMode.Kick) { }
|
2017-08-06 20:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|