From e41a459f6f551d790dc9879aa09a446ae3250223 Mon Sep 17 00:00:00 2001 From: l1ttleO Date: Sat, 12 Feb 2022 23:54:45 +0500 Subject: [PATCH] Fix durations blocking command handler thread Add "This punishment will expire in" text to ban/mute notifications --- Boyfriend/Commands/BanCommand.cs | 23 +++++++++++++++-------- Boyfriend/Commands/MuteCommand.cs | 24 +++++++++++++++--------- Boyfriend/Messages.Designer.cs | 13 +++++++++++-- Boyfriend/Messages.resx | 7 +++++-- Boyfriend/Messages.ru.resx | 7 +++++-- Boyfriend/Utils.cs | 5 ----- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/Boyfriend/Commands/BanCommand.cs b/Boyfriend/Commands/BanCommand.cs index 145316a..05a18f0 100644 --- a/Boyfriend/Commands/BanCommand.cs +++ b/Boyfriend/Commands/BanCommand.cs @@ -28,7 +28,12 @@ public class BanCommand : Command { var authorMention = author.Mention; var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}"; var memberToBan = await guild.GetUserAsync(toBan.Id); - var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason)); + var expiresIn = duration.TotalSeconds > 0 + ? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine, + DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds) + : ""; + var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason), + expiresIn); await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers); if (memberToBan != null) @@ -44,14 +49,16 @@ public class BanCommand : Command { await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification); await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification); - async void UnbanWhenExpires() { - try { - await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan, - Messages.PunishmentExpired); - } catch (ApplicationException) {} + if (duration.TotalSeconds > 0) { + var task = new Task(async () => { + await Task.Delay(duration); + try { + await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan, + Messages.PunishmentExpired); + } catch (ApplicationException) {} + }); + task.Start(); } - - await Utils.StartDelayed(new Task(UnbanWhenExpires), duration); } public override List GetAliases() { diff --git a/Boyfriend/Commands/MuteCommand.cs b/Boyfriend/Commands/MuteCommand.cs index aece45d..931384e 100644 --- a/Boyfriend/Commands/MuteCommand.cs +++ b/Boyfriend/Commands/MuteCommand.cs @@ -55,6 +55,12 @@ public class MuteCommand : Command { var config = Boyfriend.GetGuildConfig(guild); var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}"); var role = Utils.GetMuteRole(guild); + var expiresIn = duration.TotalSeconds > 0 + ? string.Format(Messages.PunishmentExpiresIn, Environment.NewLine, + DateTimeOffset.Now.ToUnixTimeSeconds() + duration.TotalSeconds) + : ""; + var notification = string.Format(Messages.MemberMuted, authorMention, toMute.Mention, Utils.WrapInline(reason), + expiresIn); if (role != null) { if (config.RemoveRolesOnMute.GetValueOrDefault(false)) { @@ -75,21 +81,21 @@ public class MuteCommand : Command { await toMute.AddRoleAsync(role, requestOptions); } else await toMute.SetTimeOutAsync(duration, requestOptions); - var notification = string.Format(Messages.MemberMuted, authorMention, toMute.Mention, Utils.WrapInline(reason)); await Utils.SilentSendAsync(channel, string.Format(Messages.MuteResponse, toMute.Mention, Utils.WrapInline(reason))); await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification); await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification); - async void UnmuteWhenExpires() { - try { - await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute, - Messages.PunishmentExpired); - } catch (ApplicationException) {} + if (role != null && duration.TotalSeconds > 0) { + var task = new Task(async () => { + await Task.Delay(duration); + try { + await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute, + Messages.PunishmentExpired); + } catch (ApplicationException) {} + }); + task.Start(); } - - if (role != null) - await Utils.StartDelayed(new Task(UnmuteWhenExpires), duration); } public override List GetAliases() { diff --git a/Boyfriend/Messages.Designer.cs b/Boyfriend/Messages.Designer.cs index 5e7fd44..1214e00 100644 --- a/Boyfriend/Messages.Designer.cs +++ b/Boyfriend/Messages.Designer.cs @@ -475,7 +475,7 @@ namespace Boyfriend { } /// - /// Looks up a localized string similar to {0} muted {1} for {2}. + /// Looks up a localized string similar to {0} muted {1} for {2}{3}. /// internal static string MemberMuted { get { @@ -573,6 +573,15 @@ namespace Boyfriend { } } + /// + /// Looks up a localized string similar to {0}This punishment will expire <t:{1}:R>. + /// + internal static string PunishmentExpiresIn { + get { + return ResourceManager.GetString("PunishmentExpiresIn", resourceCulture); + } + } + /// /// Looks up a localized string similar to {0}I'm ready! (C#). /// @@ -673,7 +682,7 @@ namespace Boyfriend { } /// - /// Looks up a localized string similar to {0} banned {1} for {2}. + /// Looks up a localized string similar to {0} banned {1} for {2}{3}. /// internal static string UserBanned { get { diff --git a/Boyfriend/Messages.resx b/Boyfriend/Messages.resx index 75ee336..1714f55 100644 --- a/Boyfriend/Messages.resx +++ b/Boyfriend/Messages.resx @@ -109,7 +109,7 @@ You were banned by {0} in guild {1} for {2} - {0} banned {1} for {2} + {0} banned {1} for {2}{3} Punishment expired @@ -133,7 +133,7 @@ {0} kicked {1} for {2} - {0} muted {1} for {2} + {0} muted {1} for {2}{3} ms @@ -249,4 +249,7 @@ Receive startup messages (`receiveStartupMessages`): {0}{1} + + {0}This punishment will expire <t:{1}:R> + \ No newline at end of file diff --git a/Boyfriend/Messages.ru.resx b/Boyfriend/Messages.ru.resx index 2790d0a..7ff5cd2 100644 --- a/Boyfriend/Messages.ru.resx +++ b/Boyfriend/Messages.ru.resx @@ -100,7 +100,7 @@ Тебя забанил {0} на сервере {1} за {2} - {0} банит {1} за {2} + {0} банит {1} за {2}{3} Время наказания истекло @@ -124,7 +124,7 @@ {0} выгоняет {1} за {2} - {0} глушит {1} за {2} + {0} глушит {1} за {2}{3} мс @@ -240,4 +240,7 @@ Получать сообщения о запуске (`receiveStartupMessages`): {0}{1} + + {0}Это наказание истечёт <t:{1}:R> + \ No newline at end of file diff --git a/Boyfriend/Utils.cs b/Boyfriend/Utils.cs index e653b86..bfc3756 100644 --- a/Boyfriend/Utils.cs +++ b/Boyfriend/Utils.cs @@ -39,11 +39,6 @@ public static class Utils { return $"<#{id}>"; } - public static async Task StartDelayed(Task toRun, TimeSpan delay) { - await Task.Delay(delay); - toRun.Start(); - } - private static ulong ParseMention(string mention) { return Convert.ToUInt64(Regex.Replace(mention, "[^0-9]", "")); }