RegexBot/Modules/ModCommands/Commands/Unban.cs

54 lines
2.1 KiB
C#
Raw Permalink Normal View History

using RegexBot.Common;
namespace RegexBot.Modules.ModCommands.Commands;
2022-07-18 23:44:31 +00:00
class Unban : CommandConfig {
private readonly string _usage;
protected override string DefaultUsageMsg => _usage;
// No configuration.
// TODO bring in some options from BanKick. Particularly custom success msg.
// TODO when ModLogs fully implemented, add a reason?
public Unban(ModCommands module, JObject config) : base(module, config) {
_usage = $"{Command} `user or user ID`\n"
+ "Unbans the given user, allowing them to rejoin the server.";
}
// Usage: (command) (user query)
public override async Task Invoke(SocketGuild g, SocketMessage msg) {
var line = SplitToParams(msg, 3);
2022-07-18 23:44:31 +00:00
string targetstr;
if (line.Length < 2) {
await SendUsageMessageAsync(msg.Channel, null);
return;
}
targetstr = line[1];
ulong targetId;
string targetDisplay;
var query = Module.Bot.EcQueryUser(targetstr);
if (query != null) {
targetId = (ulong)query.UserId;
targetDisplay = $"**{query.GetDisplayableUsername()}**";
2022-07-18 23:44:31 +00:00
} else {
if (!ulong.TryParse(targetstr, out targetId)) {
await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return;
}
targetDisplay = $"with ID **{targetId}**";
2022-07-18 23:44:31 +00:00
}
// Do the action
try {
await g.RemoveBanAsync(targetId);
await msg.Channel.SendMessageAsync($":white_check_mark: Unbanned user **{targetDisplay}**.");
} catch (Discord.Net.HttpException ex) {
const string FailPrefix = ":x: **Could not unban:** ";
if (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
await msg.Channel.SendMessageAsync(FailPrefix + Messages.ForbiddenGenericError);
2022-07-18 23:44:31 +00:00
else if (ex.HttpCode == System.Net.HttpStatusCode.NotFound)
await msg.Channel.SendMessageAsync(FailPrefix + "The specified user does not exist or is not in the ban list.");
else throw;
}
}
}