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>
This commit is contained in:
Macintxsh 2023-10-04 15:32:54 +03:00 committed by GitHub
parent 7cf200d8de
commit 777dbc6eec
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 39 deletions

View file

@ -525,11 +525,20 @@
<data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve"> <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
<value>Nitro booster since</value> <value>Nitro booster since</value>
</data> </data>
<data name="RandomMinGreaterThanMax" xml:space="preserve"> <data name="RandomTitle" xml:space="preserve">
<value>The minimum number is greater than the maximum!</value> <value>Random number for {0} is:</value>
</data> </data>
<data name="RandomOutput" xml:space="preserve"> <data name="RandomMinMaxSame" xml:space="preserve">
<value>Your random number is:</value> <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>
<data name="TimestampTitle" xml:space="preserve"> <data name="TimestampTitle" xml:space="preserve">
<value>Timestamp for {0}:</value> <value>Timestamp for {0}:</value>

View file

@ -525,11 +525,20 @@
<data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve"> <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
<value>Начал бустить сервер</value> <value>Начал бустить сервер</value>
</data> </data>
<data name="RandomMinGreaterThanMax" xml:space="preserve"> <data name="RandomTitle" xml:space="preserve">
<value>Минимальное число больше максимального!</value> <value>Случайное число для {0}:</value>
</data> </data>
<data name="RandomOutput" xml:space="preserve"> <data name="RandomMinMaxSame" xml:space="preserve">
<value>Ваше случайное число:</value> <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>
<data name="TimestampTitle" xml:space="preserve"> <data name="TimestampTitle" xml:space="preserve">
<value>Временная метка для {0}:</value> <value>Временная метка для {0}:</value>

View file

@ -525,11 +525,20 @@
<data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve"> <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
<value>бустит сервер со времен</value> <value>бустит сервер со времен</value>
</data> </data>
<data name="RandomMinGreaterThanMax" xml:space="preserve"> <data name="RandomTitle" xml:space="preserve">
<value>почему минимальное &gt; максимальное</value> <value>рандомное число {0}:</value>
</data> </data>
<data name="RandomOutput" xml:space="preserve"> <data name="RandomMinMaxSame" xml:space="preserve">
<value>ваше рандомное число:</value> <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>
<data name="TimestampTitle" xml:space="preserve"> <data name="TimestampTitle" xml:space="preserve">
<value>таймштамп для {0}:</value> <value>таймштамп для {0}:</value>

View file

@ -232,8 +232,8 @@ public class ToolsCommandGroup : CommandGroup
/// <summary> /// <summary>
/// A slash command that generates a random number using maximum and minimum numbers. /// A slash command that generates a random number using maximum and minimum numbers.
/// </summary> /// </summary>
/// <param name="max">The maximum number for randomization.</param> /// <param name="first">The first number used for randomization.</param>
/// <param name="min">The minimum number for randomization. Default value: 1</param> /// <param name="second">The second number used for randomization. Default value: 0</param>
/// <returns> /// <returns>
/// A feedback sending result which may or may not have succeeded. /// A feedback sending result which may or may not have succeeded.
/// </returns> /// </returns>
@ -242,21 +242,15 @@ public class ToolsCommandGroup : CommandGroup
[Description("Generates a random number")] [Description("Generates a random number")]
[UsedImplicitly] [UsedImplicitly]
public async Task<Result> ExecuteRandomAsync( public async Task<Result> ExecuteRandomAsync(
[Description("Maximum number")] int max, [Description("First number")] long first,
[Description("Minumum number (Default: 1)")] [Description("Second number (Default: 0)")]
int min = 1) long? second = null)
{ {
if (!_context.TryGetContextIDs(out var guildId, out _, out var userId)) if (!_context.TryGetContextIDs(out var guildId, out _, out var userId))
{ {
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); 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); var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
if (!userResult.IsDefined(out var user)) if (!userResult.IsDefined(out var user))
{ {
@ -266,25 +260,47 @@ public class ToolsCommandGroup : CommandGroup
var data = await _guildData.GetData(guildId, CancellationToken); var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings); 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) const long secondDefault = 0;
{ var second = secondNullable ?? secondDefault;
var failedEmbed = new EmbedBuilder().WithSmallTitle(
Messages.RandomMinGreaterThanMax, currentUser)
.WithColour(ColorsList.Red).Build();
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) var embedColor = ColorsList.Blue;
.WithDescription($"# {i}\n({min}-{max})") if (secondNullable is not null && min == max)
.WithColour(ColorsList.Blue) {
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(); .Build();
return await _feedback.SendContextualEmbedResultAsync(embed, ct); return await _feedback.SendContextualEmbedResultAsync(embed, ct);

View file

@ -882,17 +882,38 @@ namespace Octobot {
} }
} }
internal static string RandomMinGreaterThanMax internal static string RandomTitle
{ {
get { get {
return ResourceManager.GetString("RandomMinGreaterThanMax", resourceCulture); return ResourceManager.GetString("RandomTitle", resourceCulture);
} }
} }
internal static string RandomOutput internal static string RandomMinMaxSame
{ {
get { 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);
} }
} }