mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 13:06:50 +00:00
feat: send events from gateway to bot over http
This commit is contained in:
parent
a72afb35a0
commit
2578eb0e3c
8 changed files with 134 additions and 15 deletions
|
|
@ -32,13 +32,14 @@ public class Bot
|
|||
private readonly DiscordApiClient _rest;
|
||||
private readonly RedisService _redis;
|
||||
private readonly ILifetimeScope _services;
|
||||
private readonly RuntimeConfigService _runtimeConfig;
|
||||
|
||||
private Timer _periodicTask; // Never read, just kept here for GC reasons
|
||||
|
||||
public Bot(ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics,
|
||||
BotConfig config, RedisService redis,
|
||||
ErrorMessageService errorMessageService, CommandMessageService commandMessageService,
|
||||
Cluster cluster, DiscordApiClient rest, IDiscordCache cache)
|
||||
Cluster cluster, DiscordApiClient rest, IDiscordCache cache, RuntimeConfigService runtimeConfig)
|
||||
{
|
||||
_logger = logger.ForContext<Bot>();
|
||||
_services = services;
|
||||
|
|
@ -51,6 +52,7 @@ public class Bot
|
|||
_rest = rest;
|
||||
_redis = redis;
|
||||
_cache = cache;
|
||||
_runtimeConfig = runtimeConfig;
|
||||
}
|
||||
|
||||
private string BotStatus => $"{(_config.Prefixes ?? BotConfig.DefaultPrefixes)[0]}help"
|
||||
|
|
@ -97,13 +99,15 @@ public class Bot
|
|||
|
||||
private async Task OnEventReceived(int shardId, IGatewayEvent evt)
|
||||
{
|
||||
if (_runtimeConfig.Exists("disable_events")) return;
|
||||
|
||||
// we HandleGatewayEvent **before** getting the own user, because the own user is set in HandleGatewayEvent for ReadyEvent
|
||||
await _cache.HandleGatewayEvent(evt);
|
||||
await _cache.TryUpdateSelfMember(_config.ClientId, evt);
|
||||
await OnEventReceivedInner(shardId, evt);
|
||||
}
|
||||
|
||||
private async Task OnEventReceivedInner(int shardId, IGatewayEvent evt)
|
||||
public async Task OnEventReceivedInner(int shardId, IGatewayEvent evt)
|
||||
{
|
||||
// HandleEvent takes a type parameter, automatically inferred by the event type
|
||||
// It will then look up an IEventHandler<TypeOfEvent> in the DI container and call that object's handler method
|
||||
|
|
|
|||
|
|
@ -1,21 +1,26 @@
|
|||
using Serilog;
|
||||
using System.Text.Json;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
||||
using WatsonWebserver.Lite;
|
||||
using WatsonWebserver.Core;
|
||||
|
||||
using Myriad.Gateway;
|
||||
using Myriad.Serialization;
|
||||
|
||||
namespace PluralKit.Bot;
|
||||
|
||||
public class HttpListenerService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly RuntimeConfigService _runtimeConfig;
|
||||
private readonly Bot _bot;
|
||||
|
||||
public HttpListenerService(ILogger logger, RuntimeConfigService runtimeConfig)
|
||||
public HttpListenerService(ILogger logger, RuntimeConfigService runtimeConfig, Bot bot)
|
||||
{
|
||||
_logger = logger.ForContext<HttpListenerService>();
|
||||
_runtimeConfig = runtimeConfig;
|
||||
_bot = bot;
|
||||
}
|
||||
|
||||
public void Start(string host)
|
||||
|
|
@ -26,6 +31,8 @@ public class HttpListenerService
|
|||
server.Routes.PreAuthentication.Parameter.Add(WatsonWebserver.Core.HttpMethod.POST, "/runtime_config/{key}", RuntimeConfigSet);
|
||||
server.Routes.PreAuthentication.Parameter.Add(WatsonWebserver.Core.HttpMethod.DELETE, "/runtime_config/{key}", RuntimeConfigDelete);
|
||||
|
||||
server.Routes.PreAuthentication.Parameter.Add(WatsonWebserver.Core.HttpMethod.POST, "/events/{shard_id}", GatewayEvent);
|
||||
|
||||
server.Start();
|
||||
}
|
||||
|
||||
|
|
@ -36,7 +43,7 @@ public class HttpListenerService
|
|||
{
|
||||
var config = _runtimeConfig.GetAll();
|
||||
ctx.Response.Headers.Add("content-type", "application/json");
|
||||
await ctx.Response.Send(JsonConvert.SerializeObject(config));
|
||||
await ctx.Response.Send(JsonSerializer.Serialize(config));
|
||||
}
|
||||
|
||||
private async Task RuntimeConfigSet(HttpContextBase ctx)
|
||||
|
|
@ -53,4 +60,43 @@ public class HttpListenerService
|
|||
await _runtimeConfig.Delete(key);
|
||||
await RuntimeConfigGet(ctx);
|
||||
}
|
||||
|
||||
private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad();
|
||||
|
||||
private async Task GatewayEvent(HttpContextBase ctx)
|
||||
{
|
||||
var shardIdString = ctx.Request.Url.Parameters["shard_id"];
|
||||
if (!int.TryParse(shardIdString, out var shardId)) return;
|
||||
|
||||
var packet = JsonSerializer.Deserialize<GatewayPacket>(ctx.Request.DataAsString, _jsonSerializerOptions);
|
||||
var evt = DeserializeEvent(shardId, packet.EventType!, (JsonElement)packet.Payload!);
|
||||
if (evt != null)
|
||||
{
|
||||
await _bot.OnEventReceivedInner(shardId, evt);
|
||||
}
|
||||
await ctx.Response.Send("a");
|
||||
}
|
||||
|
||||
private IGatewayEvent? DeserializeEvent(int shardId, string eventType, JsonElement payload)
|
||||
{
|
||||
if (!IGatewayEvent.EventTypes.TryGetValue(eventType, out var clrType))
|
||||
{
|
||||
_logger.Debug("Shard {ShardId}: Received unknown event type {EventType}", shardId, eventType);
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.Verbose("Shard {ShardId}: Deserializing {EventType} to {ClrType}", shardId, eventType,
|
||||
clrType);
|
||||
return JsonSerializer.Deserialize(payload.GetRawText(), clrType, _jsonSerializerOptions)
|
||||
as IGatewayEvent;
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
_logger.Error(e, "Shard {ShardId}: Error deserializing event {EventType} to {ClrType}", shardId,
|
||||
eventType, clrType);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue