start member add responder

This commit is contained in:
sam 2024-08-20 21:03:03 +02:00
parent 633ba8f600
commit f0cb5a9d03
3 changed files with 105 additions and 7 deletions

View file

@ -4,7 +4,6 @@ using System.Threading.RateLimiting;
using Humanizer;
using NodaTime;
using Polly;
using Remora.Rest.Json.Policies;
namespace Catalogger.Backend.Services;
@ -33,7 +32,7 @@ public class PluralkitApiService(ILogger logger)
_logger.Debug("Requesting {Path} from PluralKit API", path);
var resp = await _client.SendAsync(req, ct);
var resp = await _pipeline.ExecuteAsync(async ct2 => await _client.SendAsync(req, ct2), ct);
if (resp.StatusCode == HttpStatusCode.NotFound && allowNotFound)
{
_logger.Debug("PluralKit API path {Path} returned 404 but 404 response is valid", path);
@ -48,15 +47,15 @@ public class PluralkitApiService(ILogger logger)
}
return await resp.Content.ReadFromJsonAsync<T>(new JsonSerializerOptions
{ PropertyNamingPolicy = new SnakeCaseNamingPolicy() }, ct) ??
{ PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }, ct) ??
throw new CataloggerError("JSON response from PluralKit API was null");
}
public async Task<PkMessage?> GetPluralKitMessageAsync(ulong id, CancellationToken ct = default) =>
await DoRequestAsync<PkMessage>($"/messages/{id}", allowNotFound: true, ct);
public async Task<PkSystem> GetPluralKitSystemAsync(ulong id, CancellationToken ct = default) =>
(await DoRequestAsync<PkSystem>($"/systems/{id}", allowNotFound: false, ct))!;
public async Task<PkSystem?> GetPluralKitSystemAsync(ulong id, CancellationToken ct = default) =>
await DoRequestAsync<PkSystem>($"/systems/{id}", allowNotFound: true, ct);
public record PkMessage(
ulong Id,

View file

@ -5,6 +5,7 @@ using Catalogger.Backend.Extensions;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Rest.Core;
using Guild = Catalogger.Backend.Database.Models.Guild;
@ -54,6 +55,8 @@ public class WebhookExecutorService(
public async Task QueueLogAsync(ulong channelId, IEmbed embed)
{
if (channelId == 0) return;
var queue = _cache.GetOrAdd(channelId, []);
queue.Enqueue(embed);
_cache[channelId] = queue;
@ -88,15 +91,17 @@ public class WebhookExecutorService(
embeds: embeds, username: _selfUser!.Username, avatarUrl: _selfUser.AvatarUrl());
}
public async Task SendLogWithAttachmentsAsync(ulong channelId, IEmbed embed, IEnumerable<FileData> files)
public async Task SendLogWithAttachmentsAsync(ulong channelId, List<Embed> embeds, IEnumerable<FileData> files)
{
if (channelId == 0) return;
var attachments = files
.Select<FileData, OneOf.OneOf<FileData, IPartialAttachment>>(f => f)
.ToList();
var webhook = await webhookCache.GetOrFetchWebhookAsync(channelId, id => FetchWebhookAsync(id));
await webhookApi.ExecuteWebhookAsync(DiscordSnowflake.New(webhook.Id), webhook.Token, shouldWait: false,
embeds: new List<IEmbed>([embed]), attachments: attachments, username: _selfUser!.Username,
embeds: embeds, attachments: attachments, username: _selfUser!.Username,
avatarUrl: _selfUser.AvatarUrl());
}