PluralKit/PluralKit.Bot/Interactive/BaseInteractive.cs

133 lines
3.9 KiB
C#
Raw Normal View History

2021-05-30 14:45:29 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Myriad.Rest.Types;
2021-05-30 14:45:29 +00:00
using Myriad.Rest.Types.Requests;
using Myriad.Types;
using NodaTime;
namespace PluralKit.Bot.Interactive
{
public abstract class BaseInteractive
{
protected readonly Context _ctx;
protected readonly List<Button> _buttons = new();
protected readonly TaskCompletionSource _tcs = new();
protected Message _message { get; private set; }
protected bool _running;
2021-08-27 15:03:47 +00:00
2021-05-30 14:45:29 +00:00
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);
2021-08-27 15:03:47 +00:00
2021-05-30 14:45:29 +00:00
var button = new Button
{
Label = label,
Style = style,
Disabled = disabled,
CustomId = customId
};
_buttons.Add(button);
return button;
}
protected async Task Update(InteractionContext ctx)
2021-05-30 14:45:29 +00:00
{
await ctx.Respond(InteractionResponse.ResponseType.UpdateMessage,
new InteractionApplicationCommandCallbackData
{
Components = GetComponents()
});
}
protected async Task Finish(InteractionContext? ctx = null)
2021-05-30 14:45:29 +00:00
{
2021-08-27 15:03:47 +00:00
foreach (var button in _buttons)
2021-05-30 14:45:29 +00:00
button.Disabled = true;
if (ctx != null)
await Update(ctx);
2021-08-27 15:03:47 +00:00
else
await _ctx.Rest.EditMessage(_message.ChannelId, _message.Id, new MessageEditRequest
{
Components = GetComponents()
});
2021-08-27 15:03:47 +00:00
2021-05-30 14:45:29 +00:00
_tcs.TrySetResult();
}
protected async Task Send(string? content = null, Embed? embed = null, AllowedMentions? mentions = null)
2021-05-30 14:45:29 +00:00
{
_message = await _ctx.Rest.CreateMessage(_ctx.Channel.Id, new MessageRequest
2021-05-30 14:45:29 +00:00
{
Content = content,
Embed = embed,
AllowedMentions = mentions,
2021-05-30 14:45:29 +00:00
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>();
2021-08-27 15:03:47 +00:00
foreach (var button in _buttons)
2021-05-30 14:45:29 +00:00
button.CustomId = dispatch.Register(button.Handler, Timeout);
}
public abstract Task Start();
2021-08-27 15:03:47 +00:00
2021-05-30 14:45:29 +00:00
public async Task Run()
{
if (_running)
throw new InvalidOperationException("Action is already running");
_running = true;
await Start();
2021-08-27 15:03:47 +00:00
2021-05-30 14:45:29 +00:00
var cts = new CancellationTokenSource(Timeout.ToTimeSpan());
cts.Token.Register(() => _tcs.TrySetException(new TimeoutException("Action timed out")));
try
{
await _tcs.Task;
}
finally
{
Cleanup();
}
}
protected void Cleanup()
2021-05-30 14:45:29 +00:00
{
var dispatch = _ctx.Services.Resolve<InteractionDispatchService>();
2021-08-27 15:03:47 +00:00
foreach (var button in _buttons)
2021-05-30 14:45:29 +00:00
dispatch.Unregister(button.CustomId!);
}
}
}