forked from TeamInklings/Octobot
Fix durations blocking command handler thread
Add "This punishment will expire in" text to ban/mute notifications
This commit is contained in:
parent
04facc3de2
commit
e41a459f6f
6 changed files with 51 additions and 28 deletions
|
@ -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() {
|
||||
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<string> GetAliases() {
|
||||
|
|
|
@ -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() {
|
||||
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<string> GetAliases() {
|
||||
|
|
13
Boyfriend/Messages.Designer.cs
generated
13
Boyfriend/Messages.Designer.cs
generated
|
@ -475,7 +475,7 @@ namespace Boyfriend {
|
|||
}
|
||||
|
||||
/// <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>
|
||||
internal static string MemberMuted {
|
||||
get {
|
||||
|
@ -573,6 +573,15 @@ namespace Boyfriend {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to {0}This punishment will expire <t:{1}:R>.
|
||||
/// </summary>
|
||||
internal static string PunishmentExpiresIn {
|
||||
get {
|
||||
return ResourceManager.GetString("PunishmentExpiresIn", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to {0}I'm ready! (C#).
|
||||
/// </summary>
|
||||
|
@ -673,7 +682,7 @@ namespace Boyfriend {
|
|||
}
|
||||
|
||||
/// <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>
|
||||
internal static string UserBanned {
|
||||
get {
|
||||
|
|
|
@ -109,7 +109,7 @@
|
|||
<value>You were banned by {0} in guild {1} for {2}</value>
|
||||
</data>
|
||||
<data name="UserBanned" xml:space="preserve">
|
||||
<value>{0} banned {1} for {2}</value>
|
||||
<value>{0} banned {1} for {2}{3}</value>
|
||||
</data>
|
||||
<data name="PunishmentExpired" xml:space="preserve">
|
||||
<value>Punishment expired</value>
|
||||
|
@ -133,7 +133,7 @@
|
|||
<value>{0} kicked {1} for {2}</value>
|
||||
</data>
|
||||
<data name="MemberMuted" xml:space="preserve">
|
||||
<value>{0} muted {1} for {2}</value>
|
||||
<value>{0} muted {1} for {2}{3}</value>
|
||||
</data>
|
||||
<data name="Milliseconds" xml:space="preserve">
|
||||
<value>ms</value>
|
||||
|
@ -249,4 +249,7 @@
|
|||
<data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve">
|
||||
<value>Receive startup messages (`receiveStartupMessages`): {0}{1}</value>
|
||||
</data>
|
||||
<data name="PunishmentExpiresIn" xml:space="preserve">
|
||||
<value>{0}This punishment will expire <t:{1}:R></value>
|
||||
</data>
|
||||
</root>
|
|
@ -100,7 +100,7 @@
|
|||
<value>Тебя забанил {0} на сервере {1} за {2}</value>
|
||||
</data>
|
||||
<data name="UserBanned" xml:space="preserve">
|
||||
<value>{0} банит {1} за {2}</value>
|
||||
<value>{0} банит {1} за {2}{3}</value>
|
||||
</data>
|
||||
<data name="PunishmentExpired" xml:space="preserve">
|
||||
<value>Время наказания истекло</value>
|
||||
|
@ -124,7 +124,7 @@
|
|||
<value>{0} выгоняет {1} за {2}</value>
|
||||
</data>
|
||||
<data name="MemberMuted" xml:space="preserve">
|
||||
<value>{0} глушит {1} за {2}</value>
|
||||
<value>{0} глушит {1} за {2}{3}</value>
|
||||
</data>
|
||||
<data name="Milliseconds" xml:space="preserve">
|
||||
<value>мс</value>
|
||||
|
@ -240,4 +240,7 @@
|
|||
<data name="CurrentSettingsReceiveStartupMessages" xml:space="preserve">
|
||||
<value>Получать сообщения о запуске (`receiveStartupMessages`): {0}{1}</value>
|
||||
</data>
|
||||
<data name="PunishmentExpiresIn" xml:space="preserve">
|
||||
<value>{0}Это наказание истечёт <t:{1}:R></value>
|
||||
</data>
|
||||
</root>
|
|
@ -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]", ""));
|
||||
}
|
||||
|
|
Reference in a new issue