Skip to content

Commit

Permalink
Extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Nov 5, 2024
1 parent b842c77 commit 610e51c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/Commands/Common/ConfirmCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@

namespace OoLunar.Tomoe.Commands.Common
{
/// <summary>
/// Ping! Pong!
/// </summary>
public class ConfirmCommand
public class InteractivityCommands
{
private readonly Procrastinator _procrastinator;

public ConfirmCommand(Procrastinator procrastinator) => _procrastinator = procrastinator;

[Command("confirm")]
public async ValueTask ExecuteAsync(CommandContext context)
{
bool? result = await _procrastinator.ConfirmAsync(context, "Are you sure you want to confirm?");
bool? result = await context.ConfirmAsync("Are you sure you want to confirm?");
if (result is null)
{
await context.RespondAsync("Timed out!");
Expand All @@ -36,7 +29,7 @@ public async ValueTask ExecuteAsync(CommandContext context)
[Command("prompt")]
public async ValueTask PromptAsync(CommandContext context)
{
string? result = await _procrastinator.PromptAsync(context, "What is your favorite color?");
string? result = await context.PromptAsync("What is your favorite color?");
if (result is null)
{
await context.RespondAsync("Timed out!");
Expand All @@ -50,7 +43,7 @@ public async ValueTask PromptAsync(CommandContext context)
[Command("pick")]
public async ValueTask ChooseAsync(CommandContext context)
{
string? result = await _procrastinator.ChooseAsync(context, "Choose your favorite color", ["Red", "Green", "Blue"]);
string? result = await context.ChooseAsync("Choose your favorite color", ["Red", "Green", "Blue"]);
if (result is null)
{
await context.RespondAsync("Timed out!");
Expand All @@ -64,8 +57,8 @@ public async ValueTask ChooseAsync(CommandContext context)
[Command("multipick")]
public async ValueTask ChooseMultipleAsync(CommandContext context)
{
IReadOnlyList<string>? result = await _procrastinator.ChooseMultipleAsync(context, "Choose your favorite colors", ["Red", "Green", "Blue"]);
if (result is null)
IReadOnlyList<string> result = await context.ChooseMultipleAsync("Choose your favorite colors", ["Red", "Green", "Blue"]);
if (result.Count == 0)
{
await context.RespondAsync("Timed out!");
}
Expand Down
11 changes: 11 additions & 0 deletions src/Interactivity/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DSharpPlus.Commands;
using DSharpPlus.Entities;
using Microsoft.Extensions.DependencyInjection;

namespace OoLunar.Tomoe.Interactivity
{
public static class ExtensionMethods
{
public static async ValueTask<string?> PromptAsync(this CommandContext context, string question) => await context.ServiceProvider.GetRequiredService<Procrastinator>().PromptAsync(context, question);

public static async ValueTask<string?> ChooseAsync(this CommandContext context, string question, IReadOnlyList<string> options) => await context.ServiceProvider.GetRequiredService<Procrastinator>().ChooseAsync(context, question, options);

public static async ValueTask<IReadOnlyList<string>> ChooseMultipleAsync(this CommandContext context, string question, IReadOnlyList<string> options) => await context.ServiceProvider.GetRequiredService<Procrastinator>().ChooseMultipleAsync(context, question, options);

public static async ValueTask<bool?> ConfirmAsync(this CommandContext context, string question) => await context.ServiceProvider.GetRequiredService<Procrastinator>().ConfirmAsync(context, question);

public static IEnumerable<DiscordComponent> Mutate<T>(this IEnumerable<DiscordComponent> source, Predicate<T> filter, Func<T, T> mutation) where T : DiscordComponent
{
foreach (DiscordComponent component in source)
Expand Down

0 comments on commit 610e51c

Please sign in to comment.