RegexBot/Module/ModCommands/Commands/Unban.cs

81 lines
2.9 KiB
C#
Raw Normal View History

using Discord.WebSocket;
2018-03-05 02:36:32 +00:00
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Module.ModCommands.Commands
2018-03-05 02:36:32 +00:00
{
class Unban : Command
2018-03-05 02:36:32 +00:00
{
// No configuration.
// TODO bring in some options from BanKick. Particularly custom success msg.
// TODO when ModLogs fully implemented, add a reason?
public Unban(CommandListener l, string label, JObject conf) : base(l, label, conf) {
DefaultUsageMsg = $"{this.Trigger} [user or user ID]\n"
+ "Unbans the given user, allowing them to rejoin the server.";
}
2018-03-05 02:36:32 +00:00
#region Strings
const string FailPrefix = ":x: **Unable to unban:** ";
protected const string Fail404 = "The specified user does not exist or is not in the ban list.";
2018-03-05 02:36:32 +00:00
const string TargetNotFound = ":x: **Unable to determine the target user.**";
const string Success = ":white_check_mark: Unbanned user **{0}**.";
#endregion
// Usage: (command) (user query)
public override async Task Invoke(SocketGuild g, SocketMessage msg)
{
string[] line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
string targetstr;
if (line.Length < 2)
{
await SendUsageMessageAsync(msg.Channel, null);
2018-03-05 02:36:32 +00:00
return;
}
targetstr = line[1];
// Retrieve target user
var (targetId, targetData) = await GetUserDataFromString(g.Id, targetstr);
if (targetId == 1)
2018-03-05 02:36:32 +00:00
{
await msg.Channel.SendMessageAsync(FailPrefix + FailDefault);
return;
}
if (targetId == 0)
2018-03-05 02:36:32 +00:00
{
await SendUsageMessageAsync(msg.Channel, TargetNotFound);
2018-03-05 02:36:32 +00:00
return;
}
string targetdisp;
if (targetData != null)
targetdisp = $"{targetData.Username}#{targetData.Discriminator}";
else
targetdisp = $"ID {targetId}";
2018-03-05 02:36:32 +00:00
// Do the action
try
{
await g.RemoveBanAsync(targetId);
2018-03-05 02:36:32 +00:00
await msg.Channel.SendMessageAsync(string.Format(Success, targetdisp));
}
catch (Discord.Net.HttpException ex)
{
if (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
{
await msg.Channel.SendMessageAsync(FailPrefix + Fail403);
}
else if (ex.HttpCode == System.Net.HttpStatusCode.NotFound)
{
await msg.Channel.SendMessageAsync(FailPrefix + Fail404);
}
else
{
await msg.Channel.SendMessageAsync(FailPrefix + FailDefault);
await Log(ex.ToString());
}
}
}
}
}