1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Fix durations blocking command handler thread

Add "This punishment will expire in" text to ban/mute notifications
This commit is contained in:
l1ttleO 2022-02-12 23:54:45 +05:00
parent 04facc3de2
commit e41a459f6f
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
6 changed files with 51 additions and 28 deletions

View file

@ -28,7 +28,12 @@ public class BanCommand : Command {
var authorMention = author.Mention; var authorMention = author.Mention;
var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}"; var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}";
var memberToBan = await guild.GetUserAsync(toBan.Id); 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); await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
if (memberToBan != null) if (memberToBan != null)
@ -44,14 +49,16 @@ public class BanCommand : Command {
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification); await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification); await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
async void UnbanWhenExpires() { if (duration.TotalSeconds > 0) {
var task = new Task(async () => {
await Task.Delay(duration);
try { try {
await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan, await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan,
Messages.PunishmentExpired); Messages.PunishmentExpired);
} catch (ApplicationException) {} } catch (ApplicationException) {}
});
task.Start();
} }
await Utils.StartDelayed(new Task(UnbanWhenExpires), duration);
} }
public override List<string> GetAliases() { public override List<string> GetAliases() {

View file

@ -55,6 +55,12 @@ public class MuteCommand : Command {
var config = Boyfriend.GetGuildConfig(guild); var config = Boyfriend.GetGuildConfig(guild);
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}"); var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
var role = Utils.GetMuteRole(guild); 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 (role != null) {
if (config.RemoveRolesOnMute.GetValueOrDefault(false)) { if (config.RemoveRolesOnMute.GetValueOrDefault(false)) {
@ -75,21 +81,21 @@ public class MuteCommand : Command {
await toMute.AddRoleAsync(role, requestOptions); await toMute.AddRoleAsync(role, requestOptions);
} else } else
await toMute.SetTimeOutAsync(duration, requestOptions); 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, await Utils.SilentSendAsync(channel, string.Format(Messages.MuteResponse, toMute.Mention,
Utils.WrapInline(reason))); Utils.WrapInline(reason)));
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification); await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification); await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
async void UnmuteWhenExpires() { if (role != null && duration.TotalSeconds > 0) {
var task = new Task(async () => {
await Task.Delay(duration);
try { try {
await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute, await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute,
Messages.PunishmentExpired); Messages.PunishmentExpired);
} catch (ApplicationException) {} } catch (ApplicationException) {}
});
task.Start();
} }
if (role != null)
await Utils.StartDelayed(new Task(UnmuteWhenExpires), duration);
} }
public override List<string> GetAliases() { public override List<string> GetAliases() {

View file

@ -475,7 +475,7 @@ namespace Boyfriend {
} }
/// <summary> /// <summary>
/// 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}.
/// </summary> /// </summary>
internal static string MemberMuted { internal static string MemberMuted {
get { get {
@ -573,6 +573,15 @@ namespace Boyfriend {
} }
} }
/// <summary>
/// Looks up a localized string similar to {0}This punishment will expire &lt;t:{1}:R&gt;.
/// </summary>
internal static string PunishmentExpiresIn {
get {
return ResourceManager.GetString("PunishmentExpiresIn", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to {0}I&apos;m ready! (C#). /// Looks up a localized string similar to {0}I&apos;m ready! (C#).
/// </summary> /// </summary>
@ -673,7 +682,7 @@ namespace Boyfriend {
} }
/// <summary> /// <summary>
/// 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}.
/// </summary> /// </summary>
internal static string UserBanned { internal static string UserBanned {
get { get {

View file

@ -109,7 +109,7 @@
<value>You were banned by {0} in guild {1} for {2}</value> <value>You were banned by {0} in guild {1} for {2}</value>
</data> </data>
<data name="UserBanned" xml:space="preserve"> <data name="UserBanned" xml:space="preserve">
<value>{0} banned {1} for {2}</value> <value>{0} banned {1} for {2}{3}</value>
</data> </data>
<data name="PunishmentExpired" xml:space="preserve"> <data name="PunishmentExpired" xml:space="preserve">
<value>Punishment expired</value> <value>Punishment expired</value>
@ -133,7 +133,7 @@
<value>{0} kicked {1} for {2}</value> <value>{0} kicked {1} for {2}</value>
</data> </data>
<data name="MemberMuted" xml:space="preserve"> <data name="MemberMuted" xml:space="preserve">
<value>{0} muted {1} for {2}</value> <value>{0} muted {1} for {2}{3}</value>
</data> </data>
<data name="Milliseconds" xml:space="preserve"> <data name="Milliseconds" xml:space="preserve">
<value>ms</value> <value>ms</value>
@ -249,4 +249,7 @@
<data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve"> <data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve">
<value>Receive startup messages (`receiveStartupMessages`): {0}{1}</value> <value>Receive startup messages (`receiveStartupMessages`): {0}{1}</value>
</data> </data>
<data name="PunishmentExpiresIn" xml:space="preserve">
<value>{0}This punishment will expire &lt;t:{1}:R&gt;</value>
</data>
</root> </root>

View file

@ -100,7 +100,7 @@
<value>Тебя забанил {0} на сервере {1} за {2}</value> <value>Тебя забанил {0} на сервере {1} за {2}</value>
</data> </data>
<data name="UserBanned" xml:space="preserve"> <data name="UserBanned" xml:space="preserve">
<value>{0} банит {1} за {2}</value> <value>{0} банит {1} за {2}{3}</value>
</data> </data>
<data name="PunishmentExpired" xml:space="preserve"> <data name="PunishmentExpired" xml:space="preserve">
<value>Время наказания истекло</value> <value>Время наказания истекло</value>
@ -124,7 +124,7 @@
<value>{0} выгоняет {1} за {2}</value> <value>{0} выгоняет {1} за {2}</value>
</data> </data>
<data name="MemberMuted" xml:space="preserve"> <data name="MemberMuted" xml:space="preserve">
<value>{0} глушит {1} за {2}</value> <value>{0} глушит {1} за {2}{3}</value>
</data> </data>
<data name="Milliseconds" xml:space="preserve"> <data name="Milliseconds" xml:space="preserve">
<value>мс</value> <value>мс</value>
@ -240,4 +240,7 @@
<data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve"> <data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve">
<value>Получать сообщения о запуске (`receiveStartupMessages`): {0}{1}</value> <value>Получать сообщения о запуске (`receiveStartupMessages`): {0}{1}</value>
</data> </data>
<data name="PunishmentExpiresIn" xml:space="preserve">
<value>{0}Это наказание истечёт &lt;t:{1}:R&gt;</value>
</data>
</root> </root>

View file

@ -39,11 +39,6 @@ public static class Utils {
return $"<#{id}>"; return $"<#{id}>";
} }
public static async Task StartDelayed(Task toRun, TimeSpan delay) {
await Task.Delay(delay);
toRun.Start();
}
private static ulong ParseMention(string mention) { private static ulong ParseMention(string mention) {
return Convert.ToUInt64(Regex.Replace(mention, "[^0-9]", "")); return Convert.ToUInt64(Regex.Replace(mention, "[^0-9]", ""));
} }