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 <mctaylxrs@outlook.com>
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 @@
   <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
     <value>Nitro booster since</value>
   </data>
-  <data name="RandomMinGreaterThanMax" xml:space="preserve">
-    <value>The minimum number is greater than the maximum!</value>
+  <data name="RandomTitle" xml:space="preserve">
+      <value>Random number for {0} is:</value>
   </data>
-  <data name="RandomOutput" xml:space="preserve">
-    <value>Your random number is:</value>
+  <data name="RandomMinMaxSame" xml:space="preserve">
+      <value>Isn't it obvious?</value>
+  </data>
+  <data name="RandomMin" xml:space="preserve">
+      <value>Minimum number: {0}</value>
+  </data>
+  <data name="RandomMax" xml:space="preserve">
+      <value>Maximum number: {0}</value>
+  </data>
+  <data name="Default" xml:space="preserve">
+      <value>(default)</value>
   </data>
   <data name="TimestampTitle" xml:space="preserve">
       <value>Timestamp for {0}:</value>
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 @@
   <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
     <value>Начал бустить сервер</value>
   </data>
-  <data name="RandomMinGreaterThanMax" xml:space="preserve">
-    <value>Минимальное число больше максимального!</value>
+  <data name="RandomTitle" xml:space="preserve">
+      <value>Случайное число для {0}:</value>
   </data>
-  <data name="RandomOutput" xml:space="preserve">
-    <value>Ваше случайное число:</value>
+  <data name="RandomMinMaxSame" xml:space="preserve">
+      <value>Разве это не очевидно?</value>
+  </data>
+  <data name="RandomMax" xml:space="preserve">
+      <value>Максимальное число: {0}</value>
+  </data>
+  <data name="RandomMin" xml:space="preserve">
+      <value>Минимальное число: {0}</value>
+  </data>
+  <data name="Default" xml:space="preserve">
+      <value>(по умолчанию)</value>
   </data>
   <data name="TimestampTitle" xml:space="preserve">
       <value>Временная метка для {0}:</value>
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 @@
   <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
     <value>бустит сервер со времен</value>
   </data>
-  <data name="RandomMinGreaterThanMax" xml:space="preserve">
-    <value>почему минимальное &gt; максимальное</value>
+  <data name="RandomTitle" xml:space="preserve">
+      <value>рандомное число {0}:</value>
   </data>
-  <data name="RandomOutput" xml:space="preserve">
-    <value>ваше рандомное число:</value>
+  <data name="RandomMinMaxSame" xml:space="preserve">
+      <value>ну чувак...</value>
+  </data>
+  <data name="RandomMax" xml:space="preserve">
+      <value>наибольшее: {0}</value>
+  </data>
+  <data name="RandomMin" xml:space="preserve">
+      <value>наименьшее: {0}</value>
+  </data>
+  <data name="Default" xml:space="preserve">
+      <value>(дефолт)</value>
   </data>
   <data name="TimestampTitle" xml:space="preserve">
       <value>таймштамп для {0}:</value>
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
     /// <summary>
     ///     A slash command that generates a random number using maximum and minimum numbers.
     /// </summary>
-    /// <param name="max">The maximum number for randomization.</param>
-    /// <param name="min">The minimum number for randomization. Default value: 1</param>
+    /// <param name="first">The first number used for randomization.</param>
+    /// <param name="second">The second number used for randomization. Default value: 0</param>
     /// <returns>
     ///     A feedback sending result which may or may not have succeeded.
     /// </returns>
@@ -242,21 +242,15 @@ public class ToolsCommandGroup : CommandGroup
     [Description("Generates a random number")]
     [UsedImplicitly]
     public async Task<Result> 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<Result> SendRandomNumberAsync(int max, int min, IUser user, IUser currentUser, CancellationToken ct)
+    private async Task<Result> 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);
             }
         }