From 777dbc6eec9662df05eb5e9b8019a7ce213cbf5c Mon Sep 17 00:00:00 2001 From: Macintosh II <95250141+mctaylors@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:32:54 +0300 Subject: [PATCH] Update /random (#138) Updates in /random: - Set default minimum number to 0. - Show maximum & minimum numbers. - Recolor & display a message when user tries to use exact same number in first and second fields for some reason. - Mention user in small title. - Automatically detect max & min numbers. - Add `long` support. - Show what default number is. --------- Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com> Signed-off-by: Apceniy <53149450+Apceniy@users.noreply.github.com> Signed-off-by: Macintosh II Co-authored-by: Apceniy <53149450+Apceniy@users.noreply.github.com> --- locale/Messages.resx | 17 +++++++-- locale/Messages.ru.resx | 17 +++++++-- locale/Messages.tt-ru.resx | 17 +++++++-- src/Commands/ToolsCommandGroup.cs | 62 +++++++++++++++++++------------ src/Messages.Designer.cs | 29 +++++++++++++-- 5 files changed, 103 insertions(+), 39 deletions(-) diff --git a/locale/Messages.resx b/locale/Messages.resx index b03c9f1..7f7e209 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -525,11 +525,20 @@ Nitro booster since - - The minimum number is greater than the maximum! + + Random number for {0} is: - - Your random number is: + + Isn't it obvious? + + + Minimum number: {0} + + + Maximum number: {0} + + + (default) Timestamp for {0}: diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index 7e0d6cd..5f3dcf5 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -525,11 +525,20 @@ Начал бустить сервер - - Минимальное число больше максимального! + + Случайное число для {0}: - - Ваше случайное число: + + Разве это не очевидно? + + + Максимальное число: {0} + + + Минимальное число: {0} + + + (по умолчанию) Временная метка для {0}: diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index b27104b..ce0ef61 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -525,11 +525,20 @@ бустит сервер со времен - - почему минимальное > максимальное + + рандомное число {0}: - - ваше рандомное число: + + ну чувак... + + + наибольшее: {0} + + + наименьшее: {0} + + + (дефолт) таймштамп для {0}: diff --git a/src/Commands/ToolsCommandGroup.cs b/src/Commands/ToolsCommandGroup.cs index 6abb918..445140d 100644 --- a/src/Commands/ToolsCommandGroup.cs +++ b/src/Commands/ToolsCommandGroup.cs @@ -232,8 +232,8 @@ public class ToolsCommandGroup : CommandGroup /// /// A slash command that generates a random number using maximum and minimum numbers. /// - /// The maximum number for randomization. - /// The minimum number for randomization. Default value: 1 + /// The first number used for randomization. + /// The second number used for randomization. Default value: 0 /// /// A feedback sending result which may or may not have succeeded. /// @@ -242,21 +242,15 @@ public class ToolsCommandGroup : CommandGroup [Description("Generates a random number")] [UsedImplicitly] public async Task ExecuteRandomAsync( - [Description("Maximum number")] int max, - [Description("Minumum number (Default: 1)")] - int min = 1) + [Description("First number")] long first, + [Description("Second number (Default: 0)")] + long? second = null) { if (!_context.TryGetContextIDs(out var guildId, out _, out var userId)) { return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); } - var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken); - if (!currentUserResult.IsDefined(out var currentUser)) - { - return Result.FromError(currentUserResult); - } - var userResult = await _userApi.GetUserAsync(userId, CancellationToken); if (!userResult.IsDefined(out var user)) { @@ -266,25 +260,47 @@ public class ToolsCommandGroup : CommandGroup var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); - return await SendRandomNumberAsync(max, min, user, currentUser, CancellationToken); + return await SendRandomNumberAsync(first, second, user, CancellationToken); } - private async Task SendRandomNumberAsync(int max, int min, IUser user, IUser currentUser, CancellationToken ct) + private async Task SendRandomNumberAsync(long first, long? secondNullable, + IUser user, CancellationToken ct) { - if (min > max) - { - var failedEmbed = new EmbedBuilder().WithSmallTitle( - Messages.RandomMinGreaterThanMax, currentUser) - .WithColour(ColorsList.Red).Build(); + const long secondDefault = 0; + var second = secondNullable ?? secondDefault; - return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); + var min = Math.Min(first, second); + var max = Math.Max(first, second); + + var i = Random.Shared.NextInt64(min, max + 1); + + var description = new StringBuilder().Append("# ").Append(i); + + description.AppendLine().Append("- ").Append(string.Format( + Messages.RandomMin, Markdown.InlineCode(min.ToString()))); + if (secondNullable is null && first >= secondDefault) + { + description.Append(' ').Append(Messages.Default); } - var i = Random.Shared.Next(min, max + 1); + description.AppendLine().Append("- ").Append(string.Format( + Messages.RandomMax, Markdown.InlineCode(max.ToString()))); + if (secondNullable is null && first < secondDefault) + { + description.Append(' ').Append(Messages.Default); + } - var embed = new EmbedBuilder().WithSmallTitle(Messages.RandomOutput, user) - .WithDescription($"# {i}\n({min}-{max})") - .WithColour(ColorsList.Blue) + var embedColor = ColorsList.Blue; + if (secondNullable is not null && min == max) + { + description.AppendLine().Append(Markdown.Italicise(Messages.RandomMinMaxSame)); + embedColor = ColorsList.Red; + } + + var embed = new EmbedBuilder().WithSmallTitle( + string.Format(Messages.RandomTitle, user.GetTag()), user) + .WithDescription(description.ToString()) + .WithColour(embedColor) .Build(); return await _feedback.SendContextualEmbedResultAsync(embed, ct); diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index aef4b7a..bcdc9cd 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -882,17 +882,38 @@ namespace Octobot { } } - internal static string RandomMinGreaterThanMax + internal static string RandomTitle { get { - return ResourceManager.GetString("RandomMinGreaterThanMax", resourceCulture); + return ResourceManager.GetString("RandomTitle", resourceCulture); } } - internal static string RandomOutput + internal static string RandomMinMaxSame { get { - return ResourceManager.GetString("RandomOutput", resourceCulture); + return ResourceManager.GetString("RandomMinMaxSame", resourceCulture); + } + } + + internal static string RandomMax + { + get { + return ResourceManager.GetString("RandomMax", resourceCulture); + } + } + + internal static string RandomMin + { + get { + return ResourceManager.GetString("RandomMin", resourceCulture); + } + } + + internal static string Default + { + get { + return ResourceManager.GetString("Default", resourceCulture); } }