Custom duration parser + bugfixes

This commit is contained in:
Octol1ttle 2022-08-22 19:48:51 +05:00
parent c57b845217
commit 51c24c1e23
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
5 changed files with 786 additions and 478 deletions

View file

@ -17,11 +17,12 @@ public class MuteCommand : Command {
if (duration.TotalSeconds < 0) {
Warn(Messages.DurationParseFailed);
reason = Utils.JoinString(ref args, 1);
}
if (reason is "") {
Error(Messages.ReasonRequired, false);
return;
}
if (reason is "") {
Error(Messages.ReasonRequired, false);
return;
}
if (toMute == null) {
@ -65,10 +66,6 @@ public class MuteCommand : Command {
}
await MuteMember(guild, author, toMute, duration, reason);
Success(
string.Format(Messages.FeedbackMemberMuted, toMute.Mention, Utils.GetHumanizedTimeOffset(ref duration),
Utils.Wrap(reason)), author.Mention, true);
}
private static async Task MuteMember(SocketGuild guild, SocketUser author, SocketGuildUser toMute,
@ -86,8 +83,7 @@ public class MuteCommand : Command {
if (userRole == guild.EveryoneRole || userRole == role) continue;
await toMute.RemoveRoleAsync(role);
rolesRemoved.Add(userRole.Id);
}
catch (HttpException e) {
} catch (HttpException e) {
Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)));
}
@ -105,9 +101,8 @@ public class MuteCommand : Command {
}
await toMute.AddRoleAsync(role, requestOptions);
}
else {
if (!hasDuration) {
} else {
if (!hasDuration || duration.TotalDays > 28) {
Error(Messages.DurationRequiredForTimeOuts, false);
return;
}
@ -119,5 +114,11 @@ public class MuteCommand : Command {
await toMute.SetTimeOutAsync(duration, requestOptions);
}
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
Utils.GetHumanizedTimeOffset(ref duration),
Utils.Wrap(reason));
Success(feedback, author.Mention, false, false);
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
}
}

File diff suppressed because it is too large Load diff

View file

@ -199,7 +199,7 @@
<value>I couldn't remove role {0} because of an error! {1}</value>
</data>
<data name="DurationRequiredForTimeOuts" xml:space="preserve">
<value>I cannot mute someone forever using timeouts! Either specify a proper duration, or set a mute role in settings</value>
<value>I cannot mute someone for more than 28 days using timeouts! Either specify a duration shorter than 28 days, or set a mute role in settings</value>
</data>
<data name="CannotTimeOutBot" xml:space="preserve">
<value>I cannot use time-outs on other bots! Try to set a mute role in settings</value>

View file

@ -187,7 +187,7 @@
<value>Я не смог забрать роль {0} в связи с ошибкой! {1}</value>
</data>
<data name="DurationRequiredForTimeOuts" xml:space="preserve">
<value>Я не могу заглушить кого-то навсегда, используя тайм-ауты! Или укажи правильную продолжительность, или установи роль мута в настройках</value>
<value>Я не могу заглушить кого-то на более чем 28 дней, используя тайм-ауты! Или укажи продолжительность менее 28 дней, или установи роль мута в настройках</value>
</data>
<data name="CannotTimeOutBot" xml:space="preserve">
<value>Я не могу использовать тайм-ауты на других ботах! Попробуй указать роль мута в настройках</value>

View file

@ -10,14 +10,6 @@ using Humanizer.Localisation;
namespace Boyfriend;
public static class Utils {
private static readonly string[] Formats = {
"%d'd'%h'h'%m'm'%s's'", "%d'd'%h'h'%m'm'", "%d'd'%h'h'%s's'", "%d'd'%h'h'", "%d'd'%m'm'%s's'", "%d'd'%m'm'",
"%d'd'%s's'", "%d'd'", "%h'h'%m'm'%s's'", "%h'h'%m'm'", "%h'h'%s's'", "%h'h'", "%m'm'%s's'", "%m'm'", "%s's'",
"%d'д'%h'ч'%m'м'%s'с'", "%d'д'%h'ч'%m'м'", "%d'д'%h'ч'%s'с'", "%d'д'%h'ч'", "%d'д'%m'м'%s'с'", "%d'д'%m'м'",
"%d'д'%s'с'", "%d'д'", "%h'ч'%m'м'%s'с'", "%h'ч'%m'м'", "%h'ч'%s'с'", "%h'ч'", "%m'м'%s'с'", "%m'м'", "%s'с'"
};
public static readonly Random Random = new();
private static readonly Dictionary<string, string> ReflectionMessageCache = new();
@ -99,9 +91,34 @@ public static class Utils {
}
public static TimeSpan? GetTimeSpan(ref string from) {
if (TimeSpan.TryParseExact(from.ToLowerInvariant(), Formats, CultureInfo.InvariantCulture, out var timeSpan))
return timeSpan;
return null;
var chars = from.AsSpan();
var numberBuilder = Boyfriend.StringBuilder;
int days = 0, hours = 0, minutes = 0, seconds = 0;
foreach (var c in chars)
if (char.IsDigit(c)) { numberBuilder.Append(c); } else {
if (numberBuilder.Length == 0) return null;
switch (c) {
case 'd' or 'D' or 'д' or 'Д':
days += int.Parse(numberBuilder.ToString());
numberBuilder.Clear();
break;
case 'h' or 'H' or 'ч' or 'Ч':
hours += int.Parse(numberBuilder.ToString());
numberBuilder.Clear();
break;
case 'm' or 'M' or 'м' or 'М':
minutes += int.Parse(numberBuilder.ToString());
numberBuilder.Clear();
break;
case 's' or 'S' or 'с' or 'С':
seconds += int.Parse(numberBuilder.ToString());
numberBuilder.Clear();
break;
default: return null;
}
}
return new TimeSpan(days, hours, minutes, seconds);
}
public static string JoinString(ref string[] args, int startIndex) {
@ -144,7 +161,7 @@ public static class Utils {
public static string GetHumanizedTimeOffset(ref TimeSpan span) {
return span.TotalSeconds > 0
? $" {span.Humanize(minUnit: TimeUnit.Second, culture: Messages.Culture)}"
? $" {span.Humanize(2, minUnit: TimeUnit.Second, maxUnit: TimeUnit.Month, culture: Messages.Culture)}"
: Messages.Ever;
}