Port the choose command to / commands

This commit is contained in:
Daan Boerlage 2021-11-14 23:44:02 +01:00
parent 17cb5951ee
commit d03525d363
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 52 additions and 4 deletions

View file

@ -1,6 +1,4 @@
using System; using Discord.Commands;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.ErrorHandling; using Geekbot.Core.ErrorHandling;
using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.GuildSettingsManager;
@ -15,7 +13,7 @@ namespace Geekbot.Bot.Commands.Utils
} }
[Command("choose", RunMode = RunMode.Async)] [Command("choose", RunMode = RunMode.Async)]
[Summary("Let the bot choose for you, seperate options with a semicolon.")] [Summary("Let the bot choose for you, separate options with a semicolon.")]
public async Task Command([Remainder] [Summary("option1;option2")] public async Task Command([Remainder] [Summary("option1;option2")]
string choices) string choices)
{ {

View file

@ -0,0 +1,50 @@
using Geekbot.Interactions;
using Geekbot.Interactions.ApplicationCommand;
using Geekbot.Interactions.Request;
using Geekbot.Interactions.Response;
using Localization = Geekbot.Core.Localization;
namespace Geekbot.Web.Commands;
public class Choose : InteractionBase
{
private struct Options
{
public const string InputOptions = "options";
public const string Separator = "separator";
}
public override Command GetCommandInfo() => new ()
{
Name = "choose",
Description = "Let the bot choose for you.",
Type = CommandType.ChatInput,
Options = new List<Option>()
{
new ()
{
Name = Options.InputOptions,
Description = "The options, by default separated with a semicolon.",
Type = OptionType.String,
Required = true
},
new ()
{
Name = Options.Separator,
Description = "The separator, by default a semicolon.",
Type = OptionType.String,
Required = false
}
}
};
public override Task<InteractionResponse> Exec(Interaction interaction)
{
var options = interaction.Data.Options.Find(o => o.Name == Options.InputOptions)?.Value.GetString();
var separator = interaction.Data.Options.Find(o => o.Name == Options.Separator)?.Value.GetString() ?? ";";
var choicesArray = options.Split(separator);
var choice = new Random().Next(choicesArray.Length);
return Task.FromResult(SimpleResponse(string.Format(Localization.Choose.Choice, choicesArray[choice].Trim())));
}
}