1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-30 11:09:54 +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

@ -19,7 +19,7 @@ using Remora.Results;
namespace Boyfriend.Commands;
/// <summary>
/// Handles commands related to tools: /showinfo.
/// Handles tool commands: /showinfo, /random.
/// </summary>
[UsedImplicitly]
public class ToolsCommandGroup : CommandGroup
@ -228,4 +228,65 @@ public class ToolsCommandGroup : CommandGroup
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);
}
}