Add basic interactivity framework
This commit is contained in:
125
PluralKit.Bot/Interactive/BaseInteractive.cs
Normal file
125
PluralKit.Bot/Interactive/BaseInteractive.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Autofac;
|
||||
|
||||
using Myriad.Rest.Types.Requests;
|
||||
using Myriad.Types;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Bot.Interactive
|
||||
{
|
||||
public abstract class BaseInteractive
|
||||
{
|
||||
private readonly Context _ctx;
|
||||
private readonly List<Button> _buttons = new();
|
||||
private readonly TaskCompletionSource _tcs = new();
|
||||
private bool _running;
|
||||
|
||||
protected BaseInteractive(Context ctx)
|
||||
{
|
||||
_ctx = ctx;
|
||||
}
|
||||
|
||||
public Duration Timeout { get; set; } = Duration.FromMinutes(5);
|
||||
|
||||
protected Button AddButton(Func<InteractionContext, Task> handler, string? label = null, ButtonStyle style = ButtonStyle.Secondary, bool disabled = false)
|
||||
{
|
||||
var dispatch = _ctx.Services.Resolve<InteractionDispatchService>();
|
||||
var customId = dispatch.Register(handler, Timeout);
|
||||
|
||||
var button = new Button
|
||||
{
|
||||
Label = label,
|
||||
Style = style,
|
||||
Disabled = disabled,
|
||||
CustomId = customId
|
||||
};
|
||||
_buttons.Add(button);
|
||||
return button;
|
||||
}
|
||||
|
||||
protected async Task Update(InteractionContext ctx, string? content = null, Embed? embed = null)
|
||||
{
|
||||
await ctx.Respond(InteractionResponse.ResponseType.UpdateMessage,
|
||||
new InteractionApplicationCommandCallbackData
|
||||
{
|
||||
Content = content,
|
||||
Embeds = embed != null ? new[] { embed } : null,
|
||||
Components = GetComponents()
|
||||
});
|
||||
}
|
||||
|
||||
protected async Task Finish(InteractionContext ctx)
|
||||
{
|
||||
foreach (var button in _buttons)
|
||||
button.Disabled = true;
|
||||
await Update(ctx);
|
||||
|
||||
_tcs.TrySetResult();
|
||||
}
|
||||
|
||||
protected async Task<Message> Send(string? content = null, Embed? embed = null)
|
||||
{
|
||||
return await _ctx.Rest.CreateMessage(_ctx.Channel.Id, new MessageRequest
|
||||
{
|
||||
Content = content,
|
||||
Embed = embed,
|
||||
Components = GetComponents()
|
||||
});
|
||||
}
|
||||
|
||||
public MessageComponent[] GetComponents()
|
||||
{
|
||||
return new MessageComponent[]
|
||||
{
|
||||
new()
|
||||
{
|
||||
Type = ComponentType.ActionRow,
|
||||
Components = _buttons.Select(b => b.ToMessageComponent()).ToArray()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void Setup(Context ctx)
|
||||
{
|
||||
var dispatch = ctx.Services.Resolve<InteractionDispatchService>();
|
||||
foreach (var button in _buttons)
|
||||
button.CustomId = dispatch.Register(button.Handler, Timeout);
|
||||
}
|
||||
|
||||
public abstract Task Start();
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
if (_running)
|
||||
throw new InvalidOperationException("Action is already running");
|
||||
_running = true;
|
||||
|
||||
await Start();
|
||||
|
||||
var cts = new CancellationTokenSource(Timeout.ToTimeSpan());
|
||||
cts.Token.Register(() => _tcs.TrySetException(new TimeoutException("Action timed out")));
|
||||
|
||||
try
|
||||
{
|
||||
await _tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
private void Cleanup()
|
||||
{
|
||||
var dispatch = _ctx.Services.Resolve<InteractionDispatchService>();
|
||||
foreach (var button in _buttons)
|
||||
dispatch.Unregister(button.CustomId!);
|
||||
}
|
||||
}
|
||||
}
|
25
PluralKit.Bot/Interactive/Button.cs
Normal file
25
PluralKit.Bot/Interactive/Button.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Types;
|
||||
|
||||
namespace PluralKit.Bot.Interactive
|
||||
{
|
||||
public class Button
|
||||
{
|
||||
public string? Label { get; set; }
|
||||
public ButtonStyle Style { get; set; } = ButtonStyle.Secondary;
|
||||
public string? CustomId { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
public Func<InteractionContext, Task> Handler { get; init; }
|
||||
|
||||
public MessageComponent ToMessageComponent() => new()
|
||||
{
|
||||
Type = ComponentType.Button,
|
||||
Label = Label,
|
||||
Style = Style,
|
||||
CustomId = CustomId,
|
||||
Disabled = Disabled
|
||||
};
|
||||
}
|
||||
}
|
43
PluralKit.Bot/Interactive/YesNoPrompt.cs
Normal file
43
PluralKit.Bot/Interactive/YesNoPrompt.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Types;
|
||||
|
||||
namespace PluralKit.Bot.Interactive
|
||||
{
|
||||
public class YesNoPrompt: BaseInteractive
|
||||
{
|
||||
public bool? Result { get; private set; }
|
||||
public ulong? User { get; set; }
|
||||
public string Message { get; set; } = "Are you sure?";
|
||||
|
||||
public string AcceptLabel { get; set; } = "OK";
|
||||
public ButtonStyle AcceptStyle { get; set; } = ButtonStyle.Primary;
|
||||
|
||||
public string CancelLabel { get; set; } = "Cancel";
|
||||
public ButtonStyle CancelStyle { get; set; } = ButtonStyle.Secondary;
|
||||
|
||||
public override async Task Start()
|
||||
{
|
||||
AddButton(ctx => OnButtonClick(ctx, true), AcceptLabel, AcceptStyle);
|
||||
AddButton(ctx => OnButtonClick(ctx, false), CancelLabel, CancelStyle);
|
||||
await Send(Message);
|
||||
}
|
||||
|
||||
private async Task OnButtonClick(InteractionContext ctx, bool result)
|
||||
{
|
||||
if (ctx.User.Id != User)
|
||||
{
|
||||
await Update(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
Result = result;
|
||||
await Finish(ctx);
|
||||
}
|
||||
|
||||
public YesNoPrompt(Context ctx): base(ctx)
|
||||
{
|
||||
User = ctx.Author.Id;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user