2023-05-15 15:17:34 +00:00
|
|
|
using App.Metrics;
|
|
|
|
|
2021-05-26 20:27:52 +00:00
|
|
|
using Autofac;
|
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
using Myriad.Cache;
|
2021-05-26 20:27:52 +00:00
|
|
|
using Myriad.Gateway;
|
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Types;
|
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class InteractionContext
|
2021-05-26 20:27:52 +00:00
|
|
|
{
|
2023-05-15 15:17:34 +00:00
|
|
|
private readonly ILifetimeScope _provider;
|
|
|
|
private readonly IMetrics _metrics;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
public InteractionContext(ILifetimeScope provider, InteractionCreateEvent evt, PKSystem system)
|
2021-05-26 20:27:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
Event = evt;
|
2023-05-15 15:17:34 +00:00
|
|
|
System = system;
|
|
|
|
Cache = provider.Resolve<IDiscordCache>();
|
|
|
|
Rest = provider.Resolve<DiscordApiClient>();
|
|
|
|
Repository = provider.Resolve<ModelRepository>();
|
|
|
|
_metrics = provider.Resolve<IMetrics>();
|
|
|
|
_provider = provider;
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
internal readonly IDiscordCache Cache;
|
|
|
|
internal readonly DiscordApiClient Rest;
|
|
|
|
internal readonly ModelRepository Repository;
|
|
|
|
public readonly PKSystem System;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public InteractionCreateEvent Event { get; }
|
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
public ulong GuildId => Event.GuildId;
|
2021-11-27 02:10:56 +00:00
|
|
|
public ulong ChannelId => Event.ChannelId;
|
|
|
|
public ulong? MessageId => Event.Message?.Id;
|
|
|
|
public GuildMember? Member => Event.Member;
|
|
|
|
public User User => Event.Member?.User ?? Event.User;
|
|
|
|
public string Token => Event.Token;
|
|
|
|
public string? CustomId => Event.Data?.CustomId;
|
2023-05-15 15:17:34 +00:00
|
|
|
public IComponentContext Services => _provider;
|
|
|
|
|
|
|
|
public async Task Execute<T>(ApplicationCommand? command, Func<T, Task> handler)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (_metrics.Measure.Timer.Time(BotMetrics.ApplicationCommandTime, new MetricTags("Application command", command?.Name ?? "null")))
|
|
|
|
await handler(_provider.Resolve<T>());
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2023-05-15 15:17:34 +00:00
|
|
|
_metrics.Measure.Meter.Mark(BotMetrics.ApplicationCommandsRun);
|
|
|
|
}
|
|
|
|
catch (PKError e)
|
|
|
|
{
|
|
|
|
await Reply($"{Emojis.Error} {e.Message}");
|
|
|
|
}
|
|
|
|
catch (TimeoutException)
|
|
|
|
{
|
|
|
|
// Got a complaint the old error was a bit too patronizing. Hopefully this is better?
|
|
|
|
await Reply($"{Emojis.Error} Operation timed out, sorry. Try again, perhaps?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Reply(string content = null, Embed[]? embeds = null)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
await Respond(InteractionResponse.ResponseType.ChannelMessageWithSource,
|
2023-05-15 15:17:34 +00:00
|
|
|
new InteractionApplicationCommandCallbackData
|
|
|
|
{
|
|
|
|
Content = content,
|
|
|
|
Embeds = embeds,
|
|
|
|
Flags = Message.MessageFlags.Ephemeral
|
|
|
|
});
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Ignore()
|
|
|
|
{
|
|
|
|
await Respond(InteractionResponse.ResponseType.DeferredUpdateMessage,
|
|
|
|
new InteractionApplicationCommandCallbackData
|
2021-05-30 14:45:29 +00:00
|
|
|
{
|
2022-06-14 23:05:15 +00:00
|
|
|
Components = Event.Message.Components
|
2021-05-30 14:45:29 +00:00
|
|
|
});
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-05-30 14:45:29 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Acknowledge()
|
|
|
|
{
|
|
|
|
await Respond(InteractionResponse.ResponseType.UpdateMessage,
|
|
|
|
new InteractionApplicationCommandCallbackData { Components = Array.Empty<MessageComponent>() });
|
|
|
|
}
|
2021-05-30 14:45:29 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Respond(InteractionResponse.ResponseType type,
|
|
|
|
InteractionApplicationCommandCallbackData? data)
|
|
|
|
{
|
2023-05-15 15:17:34 +00:00
|
|
|
await Rest.CreateInteractionResponse(Event.Id, Event.Token,
|
2021-11-27 02:10:56 +00:00
|
|
|
new InteractionResponse { Type = type, Data = data });
|
2021-05-26 20:27:52 +00:00
|
|
|
}
|
|
|
|
}
|