From c25808716b6b2a831c4572ceb08da5e517ac3ea9 Mon Sep 17 00:00:00 2001 From: Noi Date: Fri, 30 Sep 2022 21:03:45 -0700 Subject: [PATCH] Add command for removng timeouts --- Modules/ModCommands/Commands/Untimeout.cs | 51 +++++++++++++++++++++++ Modules/ModCommands/ModuleConfig.cs | 1 + 2 files changed, 52 insertions(+) create mode 100644 Modules/ModCommands/Commands/Untimeout.cs diff --git a/Modules/ModCommands/Commands/Untimeout.cs b/Modules/ModCommands/Commands/Untimeout.cs new file mode 100644 index 0000000..87903a6 --- /dev/null +++ b/Modules/ModCommands/Commands/Untimeout.cs @@ -0,0 +1,51 @@ +using RegexBot.Common; + +namespace RegexBot.Modules.ModCommands.Commands; +class Untimeout : 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 Untimeout(ModCommands module, JObject config) : base(module, config) { + _usage = $"{Command} `user or user ID`\n" + + "Unsets a timeout from a given user."; + } + + // Usage: (command) (user query) + public override async Task Invoke(SocketGuild g, SocketMessage msg) { + var line = msg.Content.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries); + string targetstr; + if (line.Length < 2) { + await SendUsageMessageAsync(msg.Channel, null); + return; + } + targetstr = line[1]; + + SocketGuildUser? target = null; + var query = Module.Bot.EcQueryUser(targetstr); + if (query != null) { + target = g.GetUser((ulong)query.UserId); + } + if (target == null) { + await SendUsageMessageAsync(msg.Channel, TargetNotFound); + return; + } + + // Check if timed out, respond accordingly + if (target.TimedOutUntil.HasValue && target.TimedOutUntil.Value <= DateTimeOffset.UtcNow) { + await msg.Channel.SendMessageAsync($":x: **{target}** is not timed out."); + return; + } + + // Do the action + try { + await target.RemoveTimeOutAsync(); + } catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) { + const string FailPrefix = ":x: **Could not remove timeout:** "; + await msg.Channel.SendMessageAsync(FailPrefix + Messages.ForbiddenGenericError); + } + } +} \ No newline at end of file diff --git a/Modules/ModCommands/ModuleConfig.cs b/Modules/ModCommands/ModuleConfig.cs index 651e710..c30b0a8 100644 --- a/Modules/ModCommands/ModuleConfig.cs +++ b/Modules/ModCommands/ModuleConfig.cs @@ -37,6 +37,7 @@ class ModuleConfig { { "addnote", typeof(Note) }, { "warn", typeof(Warn) }, { "timeout", typeof(Commands.Timeout) }, + { "untimeout", typeof(Untimeout)}, { "addrole", typeof(RoleAdd) }, { "roleadd", typeof(RoleAdd) }, { "delrole", typeof(RoleDel) },