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

Do not allow setting reminders without specifying a valid TimeSpan

Fixes #19

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-02-02 21:37:12 +05:00
parent 8f9c71edf1
commit a97341f9a9
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
7 changed files with 39 additions and 15 deletions

View file

@ -150,12 +150,13 @@ public static class Boyfriend {
mentions.Clear(); mentions.Clear();
} }
_ = ulong.TryParse(config["StarterRole"], out var starterRoleId);
foreach (var mData in data.MemberData.Values) { foreach (var mData in data.MemberData.Values) {
var user = guild.GetUser(mData.Id); var user = guild.GetUser(mData.Id);
if (now >= mData.BannedUntil) _ = guild.RemoveBanAsync(mData.Id); if (now >= mData.BannedUntil) _ = guild.RemoveBanAsync(mData.Id);
if (!mData.IsInGuild) continue; if (!mData.IsInGuild) continue;
if (!mData.Roles.Contains(starterRoleId) && mData.MutedUntil is null) _ = user.AddRoleAsync(starterRoleId); if (mData.MutedUntil is null
&& ulong.TryParse(config["StarterRole"], out var starterRoleId)
&& !mData.Roles.Contains(starterRoleId)) _ = user.AddRoleAsync(starterRoleId);
if (now >= mData.MutedUntil) { if (now >= mData.MutedUntil) {
await Utils.UnmuteMemberAsync( await Utils.UnmuteMemberAsync(

View file

@ -9,6 +9,7 @@ namespace Boyfriend;
public sealed class CommandProcessor { public sealed class CommandProcessor {
private static readonly string Mention = $"<@{Boyfriend.Client.CurrentUser.Id}>"; private static readonly string Mention = $"<@{Boyfriend.Client.CurrentUser.Id}>";
private static readonly TimeSpan Infinity = TimeSpan.FromMilliseconds(-1);
public static readonly ICommand[] Commands = { public static readonly ICommand[] Commands = {
new BanCommand(), new ClearCommand(), new HelpCommand(), new BanCommand(), new ClearCommand(), new HelpCommand(),
@ -246,14 +247,13 @@ public sealed class CommandProcessor {
} }
public static TimeSpan GetTimeSpan(string[] args, int index) { public static TimeSpan GetTimeSpan(string[] args, int index) {
var infinity = TimeSpan.FromMilliseconds(-1); if (index >= args.Length) return Infinity;
if (index >= args.Length) return infinity;
var chars = args[index].AsSpan(); var chars = args[index].AsSpan();
var numberBuilder = Boyfriend.StringBuilder; var numberBuilder = Boyfriend.StringBuilder;
int days = 0, hours = 0, minutes = 0, seconds = 0; int days = 0, hours = 0, minutes = 0, seconds = 0;
foreach (var c in chars) foreach (var c in chars)
if (char.IsDigit(c)) { numberBuilder.Append(c); } else { if (char.IsDigit(c)) { numberBuilder.Append(c); } else {
if (numberBuilder.Length is 0) return infinity; if (numberBuilder.Length is 0) return Infinity;
switch (c) { switch (c) {
case 'd' or 'D' or 'д' or 'Д': case 'd' or 'D' or 'д' or 'Д':
days += int.Parse(numberBuilder.ToString()); days += int.Parse(numberBuilder.ToString());
@ -271,7 +271,7 @@ public sealed class CommandProcessor {
seconds += int.Parse(numberBuilder.ToString()); seconds += int.Parse(numberBuilder.ToString());
numberBuilder.Clear(); numberBuilder.Clear();
break; break;
default: return infinity; default: return Infinity;
} }
} }

View file

@ -8,9 +8,14 @@ public sealed class RemindCommand : ICommand {
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) { public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
// TODO: actually make this good // TODO: actually make this good
var remindIn = CommandProcessor.GetTimeSpan(args, 0); var remindIn = CommandProcessor.GetTimeSpan(args, 0);
if (remindIn.TotalSeconds < 1) {
cmd.Reply(Messages.InvalidRemindIn, ReplyEmojis.InvalidArgument);
return Task.CompletedTask;
}
var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText"); var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText");
var reminderOffset = DateTimeOffset.Now.Add(remindIn);
if (reminderText is not null) { if (reminderText is not null) {
var reminderOffset = DateTimeOffset.Now.Add(remindIn);
GuildData.Get(cmd.Context.Guild).MemberData[cmd.Context.User.Id].Reminders.Add( GuildData.Get(cmd.Context.Guild).MemberData[cmd.Context.User.Id].Reminders.Add(
new Reminder { new Reminder {
RemindAt = reminderOffset, RemindAt = reminderOffset,

9
Messages.Designer.cs generated
View file

@ -1111,5 +1111,14 @@ namespace Boyfriend {
return ResourceManager.GetString("FeedbackReminderAdded", resourceCulture); return ResourceManager.GetString("FeedbackReminderAdded", resourceCulture);
} }
} }
/// <summary>
/// Looks up a localized string similar to You need to specify when I should send you the reminder!.
/// </summary>
internal static string InvalidRemindIn {
get {
return ResourceManager.GetString("InvalidRemindIn", resourceCulture);
}
}
} }
} }

View file

@ -328,7 +328,7 @@
<value>You need to specify a guild member!</value> <value>You need to specify a guild member!</value>
</data> </data>
<data name="InvalidMember" xml:space="preserve"> <data name="InvalidMember" xml:space="preserve">
<value>You did not specify a member of this guild!</value> <value>You need to specify a member of this guild!</value>
</data> </data>
<data name="UserCannotBanMembers" xml:space="preserve"> <data name="UserCannotBanMembers" xml:space="preserve">
<value>You cannot ban users from this guild!</value> <value>You cannot ban users from this guild!</value>
@ -468,4 +468,7 @@
<data name="FeedbackReminderAdded" xml:space="preserve"> <data name="FeedbackReminderAdded" xml:space="preserve">
<value>OK, I'll mention you on &lt;t:{0}:f&gt;</value> <value>OK, I'll mention you on &lt;t:{0}:f&gt;</value>
</data> </data>
<data name="InvalidRemindIn" xml:space="preserve">
<value>You need to specify when I should send you the reminder!</value>
</data>
</root> </root>

View file

@ -328,7 +328,7 @@
<value>Надо указать участника сервера!</value> <value>Надо указать участника сервера!</value>
</data> </data>
<data name="InvalidMember" xml:space="preserve"> <data name="InvalidMember" xml:space="preserve">
<value>Тебе надо указать участника этого сервера!</value> <value>Надо указать участника этого сервера!</value>
</data> </data>
<data name="UserCannotBanMembers" xml:space="preserve"> <data name="UserCannotBanMembers" xml:space="preserve">
<value>Ты не можешь банить пользователей на этом сервере!</value> <value>Ты не можешь банить пользователей на этом сервере!</value>
@ -459,13 +459,16 @@
<data name="SettingsReturnRolesOnRejoin" xml:space="preserve"> <data name="SettingsReturnRolesOnRejoin" xml:space="preserve">
<value>Возвращать роли при перезаходе</value> <value>Возвращать роли при перезаходе</value>
</data> </data>
<data name="SettingsAutoStartEvents" xml:space="preserve"> <data name="SettingsAutoStartEvents" xml:space="preserve">
<value>Автоматически начинать события</value> <value>Автоматически начинать события</value>
</data> </data>
<data name="MissingReminderText" xml:space="preserve"> <data name="MissingReminderText" xml:space="preserve">
<value>Тебе нужно указать текст напоминания!</value> <value>Тебе нужно указать текст напоминания!</value>
</data> </data>
<data name="FeedbackReminderAdded" xml:space="preserve"> <data name="FeedbackReminderAdded" xml:space="preserve">
<value>Хорошо, я упомяну тебя &lt;t:{0}:f&gt;</value> <value>Хорошо, я упомяну тебя &lt;t:{0}:f&gt;</value>
</data> </data>
<data name="InvalidRemindIn" xml:space="preserve">
<value>Нужно указать время, через которое придёт напоминание!</value>
</data>
</root> </root>

View file

@ -459,13 +459,16 @@
<data name="SettingsReturnRolesOnRejoin" xml:space="preserve"> <data name="SettingsReturnRolesOnRejoin" xml:space="preserve">
<value>вернуть звания при переподключении в дурку</value> <value>вернуть звания при переподключении в дурку</value>
</data> </data>
<data name="SettingsAutoStartEvents" xml:space="preserve"> <data name="SettingsAutoStartEvents" xml:space="preserve">
<value>автоматом стартить квесты</value> <value>автоматом стартить квесты</value>
</data> </data>
<data name="MissingReminderText" xml:space="preserve"> <data name="MissingReminderText" xml:space="preserve">
<value>для крафта напоминалки нужен текст</value> <value>для крафта напоминалки нужен текст</value>
</data> </data>
<data name="FeedbackReminderAdded" xml:space="preserve"> <data name="FeedbackReminderAdded" xml:space="preserve">
<value>вас понял, упоминание будет &lt;t:{0}:f&gt;</value> <value>вас понял, упоминание будет &lt;t:{0}:f&gt;</value>
</data> </data>
<data name="InvalidRemindIn" xml:space="preserve">
<value>шизоид у меня на часах такого нету</value>
</data>
</root> </root>