diff --git a/locale/Messages.resx b/locale/Messages.resx index 282e678..4d84198 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -648,4 +648,10 @@ Nitro booster since + + The minimum number is greater than the maximum! + + + Your random number is: + diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index 77e3838..4900e53 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -648,4 +648,10 @@ Начал бустить сервер + + Минимальное число больше максимального! + + + Ваше случайное число: + diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index b0335a8..5a08755 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -648,4 +648,10 @@ бустит сервер со времен - \ No newline at end of file + + почему минимальное > максимальное + + + ваше рандомное число: + + diff --git a/src/Commands/ToolsCommandGroup.cs b/src/Commands/ToolsCommandGroup.cs index 3ac8b70..fa0f015 100644 --- a/src/Commands/ToolsCommandGroup.cs +++ b/src/Commands/ToolsCommandGroup.cs @@ -19,7 +19,7 @@ using Remora.Results; namespace Boyfriend.Commands; /// -/// Handles commands related to tools: /showinfo. +/// Handles tool commands: /showinfo, /random. /// [UsedImplicitly] public class ToolsCommandGroup : CommandGroup @@ -228,4 +228,65 @@ public class ToolsCommandGroup : CommandGroup Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value))); } } + + /// + /// 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 + /// + /// A feedback sending result which may or may not have succeeded. + /// + [Command("random")] + [DiscordDefaultDMPermission(false)] + [Description("Generates a random number")] + [UsedImplicitly] + public async Task 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 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); + } } diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index 8784b90..ae712df 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -1121,5 +1121,19 @@ namespace Boyfriend { 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); + } + } } }