feat(bot): add support for Discord message context commands (#513)
This commit is contained in:
@@ -1,34 +1,77 @@
|
||||
using App.Metrics;
|
||||
|
||||
using Autofac;
|
||||
|
||||
using Myriad.Cache;
|
||||
using Myriad.Gateway;
|
||||
using Myriad.Rest;
|
||||
using Myriad.Types;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Bot;
|
||||
|
||||
public class InteractionContext
|
||||
{
|
||||
private readonly ILifetimeScope _services;
|
||||
private readonly ILifetimeScope _provider;
|
||||
private readonly IMetrics _metrics;
|
||||
|
||||
public InteractionContext(InteractionCreateEvent evt, ILifetimeScope services)
|
||||
public InteractionContext(ILifetimeScope provider, InteractionCreateEvent evt, PKSystem system)
|
||||
{
|
||||
Event = evt;
|
||||
_services = services;
|
||||
System = system;
|
||||
Cache = provider.Resolve<IDiscordCache>();
|
||||
Rest = provider.Resolve<DiscordApiClient>();
|
||||
Repository = provider.Resolve<ModelRepository>();
|
||||
_metrics = provider.Resolve<IMetrics>();
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
internal readonly IDiscordCache Cache;
|
||||
internal readonly DiscordApiClient Rest;
|
||||
internal readonly ModelRepository Repository;
|
||||
public readonly PKSystem System;
|
||||
|
||||
public InteractionCreateEvent Event { get; }
|
||||
|
||||
public ulong GuildId => Event.GuildId;
|
||||
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;
|
||||
public IComponentContext Services => _provider;
|
||||
|
||||
public async Task Reply(string content)
|
||||
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>());
|
||||
|
||||
_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)
|
||||
{
|
||||
await Respond(InteractionResponse.ResponseType.ChannelMessageWithSource,
|
||||
new InteractionApplicationCommandCallbackData { Content = content, Flags = Message.MessageFlags.Ephemeral });
|
||||
new InteractionApplicationCommandCallbackData
|
||||
{
|
||||
Content = content,
|
||||
Embeds = embeds,
|
||||
Flags = Message.MessageFlags.Ephemeral
|
||||
});
|
||||
}
|
||||
|
||||
public async Task Ignore()
|
||||
@@ -49,8 +92,7 @@ public class InteractionContext
|
||||
public async Task Respond(InteractionResponse.ResponseType type,
|
||||
InteractionApplicationCommandCallbackData? data)
|
||||
{
|
||||
var rest = _services.Resolve<DiscordApiClient>();
|
||||
await rest.CreateInteractionResponse(Event.Id, Event.Token,
|
||||
await Rest.CreateInteractionResponse(Event.Id, Event.Token,
|
||||
new InteractionResponse { Type = type, Data = data });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user