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

Add /random (#127)

It could have been a milestone PR, but of course I made a mistake
_somewhere_.

---------

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
Macintxsh 2023-09-29 19:22:44 +03:00 committed by GitHub
parent 04897cab20
commit 2e2f50908e
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 2 deletions

View file

@ -648,4 +648,10 @@
<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">
<value>The minimum number is greater than the maximum!</value>
</data>
<data name="RandomOutput" xml:space="preserve">
<value>Your random number is:</value>
</data>
</root> </root>

View file

@ -648,4 +648,10 @@
<data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve"> <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
<value>Начал бустить сервер</value> <value>Начал бустить сервер</value>
</data> </data>
<data name="RandomMinGreaterThanMax" xml:space="preserve">
<value>Минимальное число больше максимального!</value>
</data>
<data name="RandomOutput" xml:space="preserve">
<value>Ваше случайное число:</value>
</data>
</root> </root>

View file

@ -648,4 +648,10 @@
<data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve"> <data name="ShowInfoGuildMemberPremiumSince" xml:space="preserve">
<value>бустит сервер со времен</value> <value>бустит сервер со времен</value>
</data> </data>
</root> <data name="RandomMinGreaterThanMax" xml:space="preserve">
<value>почему минимальное &gt; максимальное</value>
</data>
<data name="RandomOutput" xml:space="preserve">
<value>ваше рандомное число:</value>
</data>
</root>

View file

@ -19,7 +19,7 @@ using Remora.Results;
namespace Boyfriend.Commands; namespace Boyfriend.Commands;
/// <summary> /// <summary>
/// Handles commands related to tools: /showinfo. /// Handles tool commands: /showinfo, /random.
/// </summary> /// </summary>
[UsedImplicitly] [UsedImplicitly]
public class ToolsCommandGroup : CommandGroup public class ToolsCommandGroup : CommandGroup
@ -228,4 +228,65 @@ public class ToolsCommandGroup : CommandGroup
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value))); Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value)));
} }
} }
/// <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>
/// <returns>
/// A feedback sending result which may or may not have succeeded.
/// </returns>
[Command("random")]
[DiscordDefaultDMPermission(false)]
[Description("Generates a random number")]
[UsedImplicitly]
public async Task<Result> ExecuteRandomAsync(
[Description("Maximum number")] int max,
[Description("Minumum number (Default: 1)")]
int min = 1)
{
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))
{
return Result.FromError(userResult);
}
var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings);
return await SendRandomNumberAsync(max, min, user, currentUser, CancellationToken);
}
private async Task<Result> SendRandomNumberAsync(int max, int min, IUser user, IUser currentUser, CancellationToken ct)
{
if (min > max)
{
var failedEmbed = new EmbedBuilder().WithSmallTitle(
Messages.RandomMinGreaterThanMax, currentUser)
.WithColour(ColorsList.Red).Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
}
var i = Random.Shared.Next(min, max + 1);
var embed = new EmbedBuilder().WithSmallTitle(Messages.RandomOutput, user)
.WithDescription($"# {i}\n({min}-{max})")
.WithColour(ColorsList.Blue)
.Build();
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
}
} }

View file

@ -1121,5 +1121,19 @@ namespace Boyfriend {
return ResourceManager.GetString("ShowInfoGuildMemberPremiumSince", resourceCulture); return ResourceManager.GetString("ShowInfoGuildMemberPremiumSince", resourceCulture);
} }
} }
internal static string RandomMinGreaterThanMax
{
get {
return ResourceManager.GetString("RandomMinGreaterThanMax", resourceCulture);
}
}
internal static string RandomOutput
{
get {
return ResourceManager.GetString("RandomOutput", resourceCulture);
}
}
} }
} }