2021-05-26 20:27:52 +00:00
|
|
|
using Autofac;
|
|
|
|
|
2022-11-28 22:45:26 +00:00
|
|
|
using Serilog;
|
|
|
|
|
2021-05-26 20:27:52 +00:00
|
|
|
using Myriad.Gateway;
|
|
|
|
using Myriad.Types;
|
2023-05-15 15:17:34 +00:00
|
|
|
using System.Buffers;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
2021-05-26 20:27:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class InteractionCreated: IEventHandler<InteractionCreateEvent>
|
2021-05-26 20:27:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly InteractionDispatchService _interactionDispatch;
|
2023-05-15 15:17:34 +00:00
|
|
|
private readonly ApplicationCommandTree _commandTree;
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ILifetimeScope _services;
|
2022-11-28 22:45:26 +00:00
|
|
|
private readonly ILogger _logger;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
public InteractionCreated(InteractionDispatchService interactionDispatch, ApplicationCommandTree commandTree,
|
|
|
|
ILifetimeScope services, ILogger logger)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
_interactionDispatch = interactionDispatch;
|
2023-05-15 15:17:34 +00:00
|
|
|
_commandTree = commandTree;
|
2021-11-27 02:10:56 +00:00
|
|
|
_services = services;
|
2022-11-28 22:45:26 +00:00
|
|
|
_logger = logger;
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-05-26 20:27:52 +00:00
|
|
|
|
2022-01-14 23:39:03 +00:00
|
|
|
public async Task Handle(int shardId, InteractionCreateEvent evt)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2023-05-15 15:17:34 +00:00
|
|
|
var system = await _services.Resolve<ModelRepository>().GetSystemByAccount(evt.Member?.User.Id ?? evt.User!.Id);
|
|
|
|
var ctx = new InteractionContext(_services, evt, system);
|
2022-06-14 23:05:15 +00:00
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
switch (evt.Type)
|
|
|
|
{
|
|
|
|
case Interaction.InteractionType.MessageComponent:
|
|
|
|
_logger.Information("Discord debug: got interaction with ID {id} from custom ID {custom_id}", evt.Id, evt.Data?.CustomId);
|
|
|
|
var customId = evt.Data?.CustomId;
|
|
|
|
if (customId == null) return;
|
|
|
|
|
|
|
|
if (customId.Contains("help-menu"))
|
|
|
|
await Help.ButtonClick(ctx);
|
|
|
|
else
|
|
|
|
await _interactionDispatch.Dispatch(customId, ctx);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Interaction.InteractionType.ApplicationCommand:
|
|
|
|
var res = _commandTree.TryHandleCommand(ctx);
|
|
|
|
if (res != null)
|
|
|
|
{
|
|
|
|
await res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// got some unhandled command, log and ignore
|
|
|
|
_logger.Warning(@"Unhandled ApplicationCommand interaction: {EventId} {CommandName}", evt.Id, evt.Data?.Name);
|
|
|
|
break;
|
|
|
|
};
|
2021-05-26 20:27:52 +00:00
|
|
|
}
|
|
|
|
}
|