elliebot/src/EllieBot/_common/Interaction/EllieInteractionService.cs
Toastie 547aa8b34d
Added common files
This took way too long
2024-06-18 23:44:07 +12:00

77 lines
No EOL
2.4 KiB
C#

namespace EllieBot;
public class EllieInteractionService : IEllieInteractionService, IEService
{
private readonly DiscordSocketClient _client;
public EllieInteractionService(DiscordSocketClient client)
{
_client = client;
}
public EllieInteractionBase Create(
ulong userId,
ButtonBuilder button,
Func<SocketMessageComponent, Task> onTrigger,
bool singleUse = true)
=> new EllieButtonInteractionHandler(_client,
userId,
button,
onTrigger,
onlyAuthor: true,
singleUse: singleUse);
public EllieInteractionBase Create<T>(
ulong userId,
ButtonBuilder button,
Func<SocketMessageComponent, T, Task> onTrigger,
in T state,
bool singleUse = true)
=> Create(userId,
button,
((Func<T, Func<SocketMessageComponent, Task>>)((data)
=> smc => onTrigger(smc, data)))(state),
singleUse);
public EllieInteractionBase Create(
ulong userId,
SelectMenuBuilder menu,
Func<SocketMessageComponent, Task> onTrigger,
bool singleUse = true)
=> new EllieButtonSelectInteractionHandler(_client,
userId,
menu,
onTrigger,
onlyAuthor: true,
singleUse: singleUse);
/// <summary>
/// Create an interaction which opens a modal
/// </summary>
/// <param name="userId">Id of the author</param>
/// <param name="button">Button builder for the button that will open the modal</param>
/// <param name="modal">Modal</param>
/// <param name="onTrigger">The function that will be called when the modal is submitted</param>
/// <param name="singleUse">Whether the button is single use</param>
/// <returns></returns>
public EllieInteractionBase Create(
ulong userId,
ButtonBuilder button,
ModalBuilder modal,
Func<SocketModal, Task> onTrigger,
bool singleUse = true)
=> Create(userId,
button,
async (smc) =>
{
await smc.RespondWithModalAsync(modal.Build());
var modalHandler = new EllieModalSubmitHandler(_client,
userId,
modal.CustomId,
onTrigger,
true);
await modalHandler.RunAsync(smc.Message);
},
singleUse: singleUse);
}