RegexBot/Feature/AutoMod/Responses/Ban.cs

51 lines
1.6 KiB
C#
Raw Normal View History

2017-08-26 17:24:37 +00:00
using Discord.WebSocket;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.AutoMod.Responses
{
/// <summary>
/// Bans the invoking user.
/// Parameters: ban [days = 0]
/// </summary>
2017-09-05 17:18:07 +00:00
class Ban : ResponseBase
2017-08-26 17:24:37 +00:00
{
readonly int _purgeDays;
2017-09-05 17:18:07 +00:00
public Ban(ConfigItem rule, string cmdline) : base(rule, cmdline)
2017-08-26 17:24:37 +00:00
{
var line = cmdline.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (line.Length == 1)
{
_purgeDays = 0;
}
else if (line.Length == 2)
{
if (int.TryParse(line[1], out _purgeDays))
{
if (_purgeDays < 0 || _purgeDays > 7)
{
throw new RuleImportException("Parameter must be an integer between 0 and 7.");
}
}
else
{
throw new RuleImportException("Parameter must be an integer between 0 and 7.");
}
}
else
{
throw new RuleImportException("Incorrect number of parameters.");
}
}
public override async Task Invoke(SocketMessage msg)
{
var target = (SocketGuildUser)msg.Author;
await target.Guild.AddBanAsync(target, _purgeDays, Uri.EscapeDataString($"Rule '{Rule.Label}'"));
// TODO remove string escaping when fixed in library
}
}
}