run dotnet format
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
@@ -11,12 +11,12 @@ namespace PluralKit.Bot
|
||||
public class CommandMessageService
|
||||
{
|
||||
private static readonly Duration CommandMessageRetention = Duration.FromHours(2);
|
||||
|
||||
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly IClock _clock;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
||||
public CommandMessageService(IDatabase db, ModelRepository repo, IClock clock, ILogger logger)
|
||||
{
|
||||
_db = db;
|
||||
@@ -42,7 +42,7 @@ namespace PluralKit.Bot
|
||||
var deleteThresholdSnowflake = DiscordUtils.InstantToSnowflake(deleteThresholdInstant);
|
||||
|
||||
var deletedRows = await _db.Execute(conn => _repo.DeleteCommandMessagesBefore(conn, deleteThresholdSnowflake));
|
||||
|
||||
|
||||
_logger.Information("Pruned {DeletedRows} command messages older than retention {Retention} (older than {DeleteThresholdInstant} / {DeleteThresholdSnowflake})",
|
||||
deletedRows, CommandMessageRetention, deleteThresholdInstant, deleteThresholdSnowflake);
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ namespace PluralKit.Bot
|
||||
public class CpuStatService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
||||
public double LastCpuMeasure { get; private set; }
|
||||
|
||||
public CpuStatService(ILogger logger)
|
||||
@@ -23,15 +23,15 @@ namespace PluralKit.Bot
|
||||
{
|
||||
// We get the current processor time, wait 5 seconds, then compare
|
||||
// https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
|
||||
|
||||
|
||||
_logger.Debug("Estimating CPU usage...");
|
||||
var stopwatch = new Stopwatch();
|
||||
|
||||
|
||||
stopwatch.Start();
|
||||
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
|
||||
|
||||
|
||||
await Task.Delay(5000);
|
||||
|
||||
|
||||
stopwatch.Stop();
|
||||
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
|
||||
|
||||
|
@@ -16,7 +16,8 @@ using NodaTime;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Bot {
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class EmbedService
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
@@ -42,11 +43,11 @@ namespace PluralKit.Bot {
|
||||
|
||||
return Task.WhenAll(ids.Select(Inner));
|
||||
}
|
||||
|
||||
|
||||
public async Task<Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
|
||||
// Fetch/render info for all accounts simultaneously
|
||||
var accounts = await _repo.GetSystemAccounts(conn, system.Id);
|
||||
var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})");
|
||||
@@ -81,13 +82,13 @@ namespace PluralKit.Bot {
|
||||
eb.Field(new("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))));
|
||||
}
|
||||
|
||||
if (system.Tag != null)
|
||||
if (system.Tag != null)
|
||||
eb.Field(new("Tag", system.Tag.EscapeMarkdown(), true));
|
||||
|
||||
if (cctx.Guild != null)
|
||||
{
|
||||
var guildSettings = await _repo.GetSystemGuild(conn, cctx.Guild.Id, system.Id);
|
||||
|
||||
|
||||
if (guildSettings.Tag != null && guildSettings.TagEnabled)
|
||||
eb.Field(new($"Tag (in server '{cctx.Guild.Name}')", guildSettings.Tag
|
||||
.EscapeMarkdown(), true));
|
||||
@@ -114,7 +115,8 @@ namespace PluralKit.Bot {
|
||||
return eb.Build();
|
||||
}
|
||||
|
||||
public Embed CreateLoggedMessageEmbed(Message triggerMessage, Message proxiedMessage, string systemHid, PKMember member, string channelName, string oldContent = null) {
|
||||
public Embed CreateLoggedMessageEmbed(Message triggerMessage, Message proxiedMessage, string systemHid, PKMember member, string channelName, string oldContent = null)
|
||||
{
|
||||
// TODO: pronouns in ?-reacted response using this card
|
||||
var timestamp = DiscordUtils.SnowflakeToInstant(proxiedMessage.Id);
|
||||
var name = proxiedMessage.Author.Username;
|
||||
@@ -129,7 +131,7 @@ namespace PluralKit.Bot {
|
||||
|
||||
if (oldContent != null)
|
||||
embed.Field(new("Old message", oldContent?.NormalizeLineEndSpacing().Truncate(1000)));
|
||||
|
||||
|
||||
return embed.Build();
|
||||
}
|
||||
|
||||
@@ -155,7 +157,7 @@ namespace PluralKit.Bot {
|
||||
}
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
|
||||
var guildSettings = guild != null ? await _repo.GetMemberGuild(conn, guild.Id, member.Id) : null;
|
||||
var guildDisplayName = guildSettings?.DisplayName;
|
||||
var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);
|
||||
@@ -179,19 +181,19 @@ namespace PluralKit.Bot {
|
||||
var description = "";
|
||||
if (member.MemberVisibility == PrivacyLevel.Private) description += "*(this member is hidden)*\n";
|
||||
if (guildSettings?.AvatarUrl != null)
|
||||
if (member.AvatarFor(ctx) != null)
|
||||
if (member.AvatarFor(ctx) != null)
|
||||
description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl.TryGetCleanCdnUrl()}) to see the global avatar)*\n";
|
||||
else
|
||||
description += "*(this member has a server-specific avatar set)*\n";
|
||||
if (description != "") eb.Description(description);
|
||||
|
||||
|
||||
if (avatar != null) eb.Thumbnail(new(avatar.TryGetCleanCdnUrl()));
|
||||
|
||||
if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx)) eb.Field(new("Display Name", member.DisplayName.Truncate(1024), true));
|
||||
if (guild != null && guildDisplayName != null) eb.Field(new($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true));
|
||||
if (member.BirthdayFor(ctx) != null) eb.Field(new("Birthdate", member.BirthdayString, true));
|
||||
if (member.PronounsFor(ctx) is {} pronouns && !string.IsNullOrWhiteSpace(pronouns)) eb.Field(new("Pronouns", pronouns.Truncate(1024), true));
|
||||
if (member.MessageCountFor(ctx) is {} count && count > 0) eb.Field(new("Message Count", member.MessageCount.ToString(), true));
|
||||
if (member.PronounsFor(ctx) is { } pronouns && !string.IsNullOrWhiteSpace(pronouns)) eb.Field(new("Pronouns", pronouns.Truncate(1024), true));
|
||||
if (member.MessageCountFor(ctx) is { } count && count > 0) eb.Field(new("Message Count", member.MessageCount.ToString(), true));
|
||||
if (member.HasProxyTags) eb.Field(new("Proxy Tags", member.ProxyTagsString("\n").Truncate(1024), true));
|
||||
// --- For when this gets added to the member object itself or however they get added
|
||||
// if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
|
||||
@@ -208,7 +210,7 @@ namespace PluralKit.Bot {
|
||||
eb.Field(new($"Groups ({groups.Count})", content.Truncate(1000)));
|
||||
}
|
||||
|
||||
if (member.DescriptionFor(ctx) is {} desc)
|
||||
if (member.DescriptionFor(ctx) is { } desc)
|
||||
eb.Field(new("Description", member.Description.NormalizeLineEndSpacing(), false));
|
||||
|
||||
return eb.Build();
|
||||
@@ -217,7 +219,7 @@ namespace PluralKit.Bot {
|
||||
public async Task<Embed> CreateGroupEmbed(Context ctx, PKSystem system, PKGroup target)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
|
||||
var pctx = ctx.LookupContextFor(system);
|
||||
var memberCount = ctx.MatchPrivateFlag(pctx) ? await _repo.GetGroupMemberCount(conn, target.Id, PrivacyLevel.Public) : await _repo.GetGroupMemberCount(conn, target.Id);
|
||||
|
||||
@@ -246,7 +248,7 @@ namespace PluralKit.Bot {
|
||||
|
||||
if (target.DisplayName != null)
|
||||
eb.Field(new("Display Name", target.DisplayName, true));
|
||||
|
||||
|
||||
if (!target.Color.EmptyOrNull()) eb.Field(new("Color", $"#{target.Color}", true));
|
||||
|
||||
if (target.ListPrivacy.CanAccess(pctx))
|
||||
@@ -261,7 +263,7 @@ namespace PluralKit.Bot {
|
||||
if (target.DescriptionFor(pctx) is { } desc)
|
||||
eb.Field(new("Description", desc));
|
||||
|
||||
if (target.IconFor(pctx) is {} icon)
|
||||
if (target.IconFor(pctx) is { } icon)
|
||||
eb.Thumbnail(new(icon.TryGetCleanCdnUrl()));
|
||||
|
||||
return eb.Build();
|
||||
@@ -349,14 +351,14 @@ namespace PluralKit.Bot {
|
||||
.Select(role => role.Name));
|
||||
eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));
|
||||
}
|
||||
|
||||
|
||||
return eb.Build();
|
||||
}
|
||||
|
||||
public Task<Embed> CreateFrontPercentEmbed(FrontBreakdown breakdown, PKSystem system, PKGroup group, DateTimeZone tz, LookupContext ctx, string embedTitle, bool ignoreNoFronters, bool showFlat)
|
||||
{
|
||||
string color = system.Color;
|
||||
if (group != null)
|
||||
if (group != null)
|
||||
{
|
||||
color = group.Color;
|
||||
}
|
||||
@@ -408,7 +410,7 @@ namespace PluralKit.Bot {
|
||||
foreach (var pair in membersOrdered)
|
||||
{
|
||||
var frac = pair.Value / period;
|
||||
eb.Field(new(pair.Key?.NameFor(ctx) ?? "*(no fronter)*", $"{frac*100:F0}% ({pair.Value.FormatDuration()})"));
|
||||
eb.Field(new(pair.Key?.NameFor(ctx) ?? "*(no fronter)*", $"{frac * 100:F0}% ({pair.Value.FormatDuration()})"));
|
||||
}
|
||||
|
||||
if (membersOrdered.Count > maxEntriesToDisplay)
|
||||
@@ -422,4 +424,4 @@ namespace PluralKit.Bot {
|
||||
return Task.FromResult(eb.Build());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -17,11 +17,11 @@ namespace PluralKit.Bot
|
||||
{
|
||||
private static readonly Duration MinErrorInterval = Duration.FromSeconds(10);
|
||||
private readonly ConcurrentDictionary<ulong, Instant> _lastErrorInChannel = new ConcurrentDictionary<ulong, Instant>();
|
||||
|
||||
|
||||
private readonly IMetrics _metrics;
|
||||
private readonly ILogger _logger;
|
||||
private readonly DiscordApiClient _rest;
|
||||
|
||||
|
||||
public ErrorMessageService(IMetrics metrics, ILogger logger, DiscordApiClient rest)
|
||||
{
|
||||
_metrics = metrics;
|
||||
@@ -53,7 +53,7 @@ namespace PluralKit.Bot
|
||||
Content = $"> **Error code:** `{errorId}`",
|
||||
Embed = embed.Build()
|
||||
});
|
||||
|
||||
|
||||
_logger.Information("Sent error message to {ChannelId} with error code {ErrorId}", channelId, errorId);
|
||||
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "sent");
|
||||
}
|
||||
|
@@ -12,14 +12,14 @@ namespace PluralKit.Bot
|
||||
private class Response
|
||||
{
|
||||
private readonly Groups.AddRemoveOperation _op;
|
||||
|
||||
|
||||
private readonly string _actionStr;
|
||||
private readonly string _containStr;
|
||||
private readonly string _emojiStr;
|
||||
|
||||
private readonly bool _memberPlural;
|
||||
private readonly bool _groupPlural;
|
||||
|
||||
|
||||
private readonly int _actionedOn;
|
||||
private readonly int _notActionedOn;
|
||||
|
||||
@@ -27,18 +27,18 @@ namespace PluralKit.Bot
|
||||
int notActionedOn)
|
||||
{
|
||||
_op = action;
|
||||
|
||||
|
||||
_actionStr = action == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
|
||||
_containStr = action == Groups.AddRemoveOperation.Add ? "in" : "not in";
|
||||
_emojiStr = actionedOn > 0 ? Emojis.Success : Emojis.Error;
|
||||
|
||||
_memberPlural = memberCount > 1;
|
||||
_groupPlural = groupCount > 1;
|
||||
|
||||
|
||||
// sanity checking: we can't add multiple groups to multiple members (at least for now)
|
||||
if (_memberPlural && _groupPlural)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
|
||||
// sanity checking: we can't act/not act on a different number of entities than we have
|
||||
if (_memberPlural && (actionedOn + notActionedOn) != memberCount)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
@@ -48,7 +48,7 @@ namespace PluralKit.Bot
|
||||
_actionedOn = actionedOn;
|
||||
_notActionedOn = notActionedOn;
|
||||
}
|
||||
|
||||
|
||||
// name generators
|
||||
private string MemberString(bool capitalize = false)
|
||||
=> capitalize
|
||||
@@ -59,14 +59,14 @@ namespace PluralKit.Bot
|
||||
=> capitalize
|
||||
? (count == 1 ? "Member" : "Members")
|
||||
: (count == 1 ? "member" : "members");
|
||||
|
||||
|
||||
private string GroupString() => _groupPlural ? "groups" : "group";
|
||||
|
||||
private string GroupString(int count)
|
||||
=> count == 1 ? "group" : "groups";
|
||||
|
||||
// string generators
|
||||
|
||||
|
||||
private string ResponseString()
|
||||
{
|
||||
if (_actionedOn > 0 && _notActionedOn > 0 && _memberPlural)
|
||||
@@ -100,9 +100,9 @@ namespace PluralKit.Bot
|
||||
|
||||
return $" ({msg})";
|
||||
}
|
||||
|
||||
|
||||
public string ToString() => $"{_emojiStr} {ResponseString()}{InfoMessage()}.";
|
||||
|
||||
|
||||
// |
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ namespace PluralKit.Bot
|
||||
private readonly IClock _clock;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Task _cleanupWorker;
|
||||
|
||||
|
||||
public InteractionDispatchService(IClock clock, ILogger logger)
|
||||
{
|
||||
_clock = clock;
|
||||
@@ -31,7 +31,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
if (!Guid.TryParse(customId, out var customIdGuid))
|
||||
return false;
|
||||
|
||||
|
||||
if (!_handlers.TryGetValue(customIdGuid, out var handler))
|
||||
return false;
|
||||
|
||||
@@ -52,10 +52,10 @@ namespace PluralKit.Bot
|
||||
var key = Guid.NewGuid();
|
||||
var handler = new RegisteredInteraction
|
||||
{
|
||||
Callback = callback,
|
||||
Callback = callback,
|
||||
Expiry = _clock.GetCurrentInstant() + (expiry ?? DefaultExpiry)
|
||||
};
|
||||
|
||||
|
||||
_handlers[key] = handler;
|
||||
return key.ToString();
|
||||
}
|
||||
@@ -93,7 +93,7 @@ namespace PluralKit.Bot
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts.Cancel();
|
||||
_cts.Cancel();
|
||||
_cts.Dispose();
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace PluralKit.Bot
|
||||
_cache[msg.ChannelId] = new(current, previous?.Current);
|
||||
}
|
||||
|
||||
private CachedMessage ToCachedMessage(Message msg) =>
|
||||
private CachedMessage ToCachedMessage(Message msg) =>
|
||||
new(msg.Id, msg.ReferencedMessage.Value?.Id, msg.Author.Username);
|
||||
|
||||
public CacheEntry? GetLastMessage(ulong channel)
|
||||
@@ -73,4 +73,4 @@ namespace PluralKit.Bot
|
||||
public record CacheEntry(CachedMessage Current, CachedMessage? Previous);
|
||||
|
||||
public record CachedMessage(ulong Id, ulong? ReferencedMessage, string AuthorUsername);
|
||||
}
|
||||
}
|
@@ -12,8 +12,10 @@ using PluralKit.Core;
|
||||
|
||||
using Serilog;
|
||||
|
||||
namespace PluralKit.Bot {
|
||||
public class LogChannelService {
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class LogChannelService
|
||||
{
|
||||
private readonly EmbedService _embed;
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
@@ -40,7 +42,7 @@ namespace PluralKit.Bot {
|
||||
return;
|
||||
|
||||
var triggerChannel = _cache.GetChannel(proxiedMessage.Channel);
|
||||
|
||||
|
||||
await using var conn = await _db.Obtain();
|
||||
var system = await _repo.GetSystem(conn, ctx.SystemId.Value);
|
||||
var member = await _repo.GetMember(conn, proxiedMessage.Member);
|
||||
@@ -48,7 +50,7 @@ namespace PluralKit.Bot {
|
||||
// Send embed!
|
||||
var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name, oldContent);
|
||||
var url = $"https://discord.com/channels/{proxiedMessage.Guild.Value}/{proxiedMessage.Channel}/{proxiedMessage.Mid}";
|
||||
await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed});
|
||||
await _rest.CreateMessage(logChannel.Id, new() { Content = url, Embed = embed });
|
||||
}
|
||||
|
||||
private async Task<Channel?> GetAndCheckLogChannel(MessageContext ctx, Message trigger, PKMessage proxiedMessage)
|
||||
@@ -71,17 +73,17 @@ namespace PluralKit.Bot {
|
||||
}
|
||||
|
||||
if (ctx.SystemId == null || logChannelId == null || isBlacklisted) return null;
|
||||
|
||||
|
||||
// Find log channel and check if valid
|
||||
var logChannel = await FindLogChannel(guildId, logChannelId.Value);
|
||||
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return null;
|
||||
|
||||
|
||||
// Check bot permissions
|
||||
var perms = _bot.PermissionsIn(logChannel.Id);
|
||||
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
|
||||
{
|
||||
_logger.Information(
|
||||
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
|
||||
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
|
||||
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
|
||||
return null;
|
||||
}
|
||||
@@ -94,12 +96,12 @@ namespace PluralKit.Bot {
|
||||
// TODO: fetch it directly on cache miss?
|
||||
if (_cache.TryGetChannel(channelId, out var channel))
|
||||
return channel;
|
||||
|
||||
|
||||
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too
|
||||
_logger.Warning("Attempted to fetch missing log channel {LogChannel} for guild {Guild}, removing from database", channelId, guildId);
|
||||
await using var conn = await _db.Obtain();
|
||||
await conn.ExecuteAsync("update servers set log_channel = null where id = @Guild",
|
||||
new {Guild = guildId});
|
||||
new { Guild = guildId });
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -48,15 +48,15 @@ namespace PluralKit.Bot
|
||||
|
||||
// There are two "Logger"s. They seem to be entirely unrelated. Don't ask.
|
||||
new LoggerBot("Logger#6088", 298822483060981760 , ExtractLoggerA, webhookName: "Logger"),
|
||||
new LoggerBot("Logger#6278", 327424261180620801, ExtractLoggerB),
|
||||
|
||||
new LoggerBot("Logger#6278", 327424261180620801, ExtractLoggerB),
|
||||
|
||||
new LoggerBot("Dyno", 155149108183695360, ExtractDyno, webhookName: "Dyno"),
|
||||
new LoggerBot("Auttaja", 242730576195354624, ExtractAuttaja, webhookName: "Auttaja"),
|
||||
new LoggerBot("GenericBot", 295329346590343168, ExtractGenericBot),
|
||||
new LoggerBot("blargbot", 134133271750639616, ExtractBlargBot),
|
||||
new LoggerBot("Mantaro", 213466096718708737, ExtractMantaro),
|
||||
new LoggerBot("UnbelievaBoat", 292953664492929025, ExtractUnbelievaBoat, webhookName: "UnbelievaBoat"),
|
||||
new LoggerBot("Vanessa", 310261055060443136, fuzzyExtractFunc: ExtractVanessa),
|
||||
new LoggerBot("GenericBot", 295329346590343168, ExtractGenericBot),
|
||||
new LoggerBot("blargbot", 134133271750639616, ExtractBlargBot),
|
||||
new LoggerBot("Mantaro", 213466096718708737, ExtractMantaro),
|
||||
new LoggerBot("UnbelievaBoat", 292953664492929025, ExtractUnbelievaBoat, webhookName: "UnbelievaBoat"),
|
||||
new LoggerBot("Vanessa", 310261055060443136, fuzzyExtractFunc: ExtractVanessa),
|
||||
new LoggerBot("SafetyAtLast", 401549924199694338, fuzzyExtractFunc: ExtractSAL),
|
||||
new LoggerBot("GearBot", 349977940198555660, fuzzyExtractFunc: ExtractGearBot),
|
||||
new LoggerBot("GiselleBot", 356831787445387285, ExtractGiselleBot),
|
||||
@@ -72,7 +72,7 @@ namespace PluralKit.Bot
|
||||
private readonly IDiscordCache _cache;
|
||||
private readonly Bot _bot; // todo: get rid of this nasty
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
||||
public LoggerCleanService(IDatabase db, DiscordApiClient client, IDiscordCache cache, Bot bot, ILogger logger)
|
||||
{
|
||||
_db = db;
|
||||
@@ -87,17 +87,17 @@ namespace PluralKit.Bot
|
||||
public async ValueTask HandleLoggerBotCleanup(Message msg)
|
||||
{
|
||||
var channel = _cache.GetChannel(msg.ChannelId);
|
||||
|
||||
|
||||
if (channel.Type != Channel.ChannelType.GuildText) return;
|
||||
if (!_bot.PermissionsIn(channel.Id).HasFlag(PermissionSet.ManageMessages)) return;
|
||||
|
||||
|
||||
// If this message is from a *webhook*, check if the name matches one of the bots we know
|
||||
// TODO: do we need to do a deeper webhook origin check, or would that be too hard on the rate limit?
|
||||
// If it's from a *bot*, check the bot ID to see if we know it.
|
||||
LoggerBot bot = null;
|
||||
if (msg.WebhookId != null) _botsByWebhookName.TryGetValue(msg.Author.Username, out bot);
|
||||
else if (msg.Author.Bot) _bots.TryGetValue(msg.Author.Id, out bot);
|
||||
|
||||
|
||||
// If we didn't find anything before, or what we found is an unsupported bot, bail
|
||||
if (bot == null) return;
|
||||
|
||||
@@ -113,10 +113,10 @@ namespace PluralKit.Bot
|
||||
// either way but shouldn't be too much, given it's constrained by user ID and guild.
|
||||
var fuzzy = bot.FuzzyExtractFunc(msg);
|
||||
if (fuzzy == null) return;
|
||||
|
||||
|
||||
_logger.Debug("Fuzzy logclean for {BotName} on {MessageId}: {@FuzzyExtractResult}",
|
||||
bot.Name, msg.Id, fuzzy);
|
||||
|
||||
|
||||
var mid = await _db.Execute(conn =>
|
||||
conn.QuerySingleOrDefaultAsync<ulong?>(
|
||||
"select mid from messages where sender = @User and mid > @ApproxID and guild = @Guild limit 1",
|
||||
@@ -127,11 +127,11 @@ namespace PluralKit.Bot
|
||||
ApproxId = DiscordUtils.InstantToSnowflake(
|
||||
fuzzy.Value.ApproxTimestamp - Duration.FromSeconds(3))
|
||||
}));
|
||||
|
||||
|
||||
// If we didn't find a corresponding message, bail
|
||||
if (mid == null)
|
||||
return;
|
||||
|
||||
if (mid == null)
|
||||
return;
|
||||
|
||||
// Otherwise, we can *reasonably assume* that this is a logged deletion, so delete the log message.
|
||||
await _client.DeleteMessage(msg.ChannelId, msg.Id);
|
||||
}
|
||||
@@ -140,12 +140,12 @@ namespace PluralKit.Bot
|
||||
// Other bots give us the message ID itself, and we can just extract that from the database directly.
|
||||
var extractedId = bot.ExtractFunc(msg);
|
||||
if (extractedId == null) return; // If we didn't find anything, bail.
|
||||
|
||||
|
||||
_logger.Debug("Pure logclean for {BotName} on {MessageId}: {@FuzzyExtractResult}",
|
||||
bot.Name, msg.Id, extractedId);
|
||||
|
||||
var mid = await _db.Execute(conn => conn.QuerySingleOrDefaultAsync<ulong?>(
|
||||
"select mid from messages where original_mid = @Mid", new {Mid = extractedId.Value}));
|
||||
"select mid from messages where original_mid = @Mid", new { Mid = extractedId.Value }));
|
||||
if (mid == null) return;
|
||||
|
||||
// If we've gotten this far, we found a logged deletion of a trigger message. Just yeet it!
|
||||
@@ -167,9 +167,9 @@ namespace PluralKit.Bot
|
||||
// Regex also checks that this is a deletion.
|
||||
var stringWithId = msg.Embeds?.FirstOrDefault()?.Description ?? msg.Content;
|
||||
if (stringWithId == null) return null;
|
||||
|
||||
|
||||
var match = _auttajaRegex.Match(stringWithId);
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractDyno(Message msg)
|
||||
@@ -178,7 +178,7 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Footer == null || !(embed.Description?.Contains("deleted in") ?? false)) return null;
|
||||
var match = _dynoRegex.Match(embed.Footer.Text ?? "");
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractLoggerA(Message msg)
|
||||
@@ -188,11 +188,11 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed == null) return null;
|
||||
if (!embed.Description.StartsWith("Message deleted in")) return null;
|
||||
|
||||
|
||||
var idField = embed.Fields.FirstOrDefault(f => f.Name == "ID");
|
||||
if (idField.Value == null) return null; // "OrDefault" = all-null object
|
||||
var match = _loggerARegex.Match(idField.Value);
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractLoggerB(Message msg)
|
||||
@@ -202,7 +202,7 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Footer == null || !(embed.Title?.EndsWith("A Message Was Deleted!") ?? false)) return null;
|
||||
var match = _loggerBRegex.Match(embed.Footer.Text ?? "");
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractGenericBot(Message msg)
|
||||
@@ -211,7 +211,7 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Footer == null || !(embed.Title?.Contains("Message Deleted") ?? false)) return null;
|
||||
var match = _basicRegex.Match(embed.Footer.Text ?? "");
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractBlargBot(Message msg)
|
||||
@@ -221,7 +221,7 @@ namespace PluralKit.Bot
|
||||
if (embed == null || !(embed.Title?.EndsWith("Message Deleted") ?? false)) return null;
|
||||
var field = embed.Fields.FirstOrDefault(f => f.Name == "Message ID");
|
||||
var match = _basicRegex.Match(field.Value ?? "");
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractMantaro(Message msg)
|
||||
@@ -229,7 +229,7 @@ namespace PluralKit.Bot
|
||||
// Plain message, "Message (ID: [id]) created by [user] (ID: [id]) in channel [channel] was deleted.
|
||||
if (!(msg.Content?.Contains("was deleted.") ?? false)) return null;
|
||||
var match = _mantaroRegex.Match(msg.Content);
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static FuzzyExtractResult? ExtractCarlBot(Message msg)
|
||||
@@ -239,13 +239,13 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Footer == null || embed.Timestamp == null || !(embed.Title?.StartsWith("Message deleted in") ?? false)) return null;
|
||||
var match = _carlRegex.Match(embed.Footer.Text ?? "");
|
||||
return match.Success
|
||||
return match.Success
|
||||
? new FuzzyExtractResult
|
||||
{
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = OffsetDateTimePattern.Rfc3339.Parse(embed.Timestamp).GetValueOrThrow().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
private static FuzzyExtractResult? ExtractCircle(Message msg)
|
||||
@@ -261,16 +261,17 @@ namespace PluralKit.Bot
|
||||
var field = embed.Fields.FirstOrDefault(f => f.Name == "Message Author");
|
||||
if (field.Value == null) return null;
|
||||
stringWithId = field.Value;
|
||||
}
|
||||
}
|
||||
if (stringWithId == null) return null;
|
||||
|
||||
|
||||
var match = _circleRegex.Match(stringWithId);
|
||||
return match.Success
|
||||
? new FuzzyExtractResult {
|
||||
return match.Success
|
||||
? new FuzzyExtractResult
|
||||
{
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = msg.Timestamp().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
private static FuzzyExtractResult? ExtractPancake(Message msg)
|
||||
@@ -286,18 +287,18 @@ namespace PluralKit.Bot
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = msg.Timestamp().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
|
||||
private static ulong? ExtractUnbelievaBoat(Message msg)
|
||||
{
|
||||
// Embed author is "Message Deleted", footer contains message ID per regex
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Footer == null || embed.Author?.Name != "Message Deleted") return null;
|
||||
var match = _unbelievaboatRegex.Match(embed.Footer.Text ?? "");
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
|
||||
private static FuzzyExtractResult? ExtractVanessa(Message msg)
|
||||
{
|
||||
// Title is "Message Deleted", embed description contains mention
|
||||
@@ -310,9 +311,9 @@ namespace PluralKit.Bot
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = msg.Timestamp().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
|
||||
private static FuzzyExtractResult? ExtractSAL(Message msg)
|
||||
{
|
||||
// Title is "Message Deleted!", field "Message Author" contains ID
|
||||
@@ -327,7 +328,7 @@ namespace PluralKit.Bot
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = msg.Timestamp().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
private static FuzzyExtractResult? ExtractGearBot(Message msg)
|
||||
@@ -342,7 +343,7 @@ namespace PluralKit.Bot
|
||||
User = ulong.Parse(match.Groups[1].Value),
|
||||
ApproxTimestamp = msg.Timestamp().ToInstant()
|
||||
}
|
||||
: (FuzzyExtractResult?) null;
|
||||
: (FuzzyExtractResult?)null;
|
||||
}
|
||||
|
||||
private static ulong? ExtractGiselleBot(Message msg)
|
||||
@@ -350,7 +351,7 @@ namespace PluralKit.Bot
|
||||
var embed = msg.Embeds?.FirstOrDefault();
|
||||
if (embed?.Title == null || embed.Title != "🗑 Message Deleted") return null;
|
||||
var match = _GiselleRegex.Match(embed?.Description);
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?) null;
|
||||
return match.Success ? ulong.Parse(match.Groups[1].Value) : (ulong?)null;
|
||||
}
|
||||
|
||||
private static FuzzyExtractResult? ExtractVortex(Message msg)
|
||||
|
@@ -43,11 +43,11 @@ namespace PluralKit.Bot
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
|
||||
// Aggregate guild/channel stats
|
||||
var guildCount = 0;
|
||||
var channelCount = 0;
|
||||
|
||||
|
||||
// No LINQ today, sorry
|
||||
await foreach (var guild in _cache.GetAllGuilds())
|
||||
{
|
||||
@@ -58,10 +58,10 @@ namespace PluralKit.Bot
|
||||
channelCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, guildCount);
|
||||
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount);
|
||||
|
||||
|
||||
// Aggregate DB stats
|
||||
var counts = await _db.Execute(c => c.QueryFirstAsync<Counts>("select (select count(*) from systems) as systems, (select count(*) from members) as members, (select count(*) from switches) as switches, (select count(*) from messages) as messages, (select count(*) from groups) as groups"));
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.Systems);
|
||||
@@ -69,7 +69,7 @@ namespace PluralKit.Bot
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.Switches);
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.Messages);
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.GroupCount, counts.Groups);
|
||||
|
||||
|
||||
// Process info
|
||||
var process = Process.GetCurrentProcess();
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPhysicalMemory, process.WorkingSet64);
|
||||
@@ -78,10 +78,10 @@ namespace PluralKit.Bot
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessThreads, process.Threads.Count);
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessHandles, process.HandleCount);
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.CpuUsage, await _cpu.EstimateCpuUsage());
|
||||
|
||||
|
||||
// Database info
|
||||
_metrics.Measure.Gauge.SetValue(CoreMetrics.DatabaseConnections, _countHolder.ConnectionCount);
|
||||
|
||||
|
||||
// Other shiz
|
||||
_metrics.Measure.Gauge.SetValue(BotMetrics.WebhookCacheSize, _webhookCache.CacheSize);
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace PluralKit.Bot
|
||||
public class Counts
|
||||
{
|
||||
public int Systems { get; }
|
||||
public int Members { get; }
|
||||
public int Members { get; }
|
||||
public int Switches { get; }
|
||||
public int Messages { get; }
|
||||
public int Groups { get; }
|
||||
|
@@ -38,7 +38,7 @@ namespace PluralKit.Bot
|
||||
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
|
||||
|
||||
public ShardInfoService(ILogger logger, Cluster client, IMetrics metrics, IDatabase db, ModelRepository repo)
|
||||
{
|
||||
_client = client;
|
||||
@@ -68,18 +68,19 @@ namespace PluralKit.Bot
|
||||
if (_shardInfo.TryGetValue(shard.ShardId, out var info))
|
||||
{
|
||||
// Skip adding listeners if we've seen this shard & already added listeners to it
|
||||
if (info.HasAttachedListeners)
|
||||
if (info.HasAttachedListeners)
|
||||
return;
|
||||
} else _shardInfo[shard.ShardId] = info = new ShardInfo();
|
||||
|
||||
}
|
||||
else _shardInfo[shard.ShardId] = info = new ShardInfo();
|
||||
|
||||
// Call our own SocketOpened listener manually (and then attach the listener properly)
|
||||
|
||||
|
||||
// Register listeners for new shards
|
||||
shard.Resumed += () => ReadyOrResumed(shard);
|
||||
shard.Ready += () => ReadyOrResumed(shard);
|
||||
shard.SocketClosed += (closeStatus, message) => SocketClosed(shard, closeStatus, message);
|
||||
shard.HeartbeatReceived += latency => Heartbeated(shard, latency);
|
||||
|
||||
|
||||
// Register that we've seen it
|
||||
info.HasAttachedListeners = true;
|
||||
}
|
||||
@@ -100,7 +101,7 @@ namespace PluralKit.Bot
|
||||
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
ReportShardStatus();
|
||||
|
||||
|
||||
_ = ExecuteWithDatabase(async c =>
|
||||
{
|
||||
await _repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Up);
|
||||
@@ -114,7 +115,7 @@ namespace PluralKit.Bot
|
||||
info.DisconnectionCount++;
|
||||
info.Connected = false;
|
||||
ReportShardStatus();
|
||||
|
||||
|
||||
_ = ExecuteWithDatabase(c =>
|
||||
_repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Down));
|
||||
}
|
||||
@@ -125,7 +126,7 @@ namespace PluralKit.Bot
|
||||
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
info.ShardLatency = latency.ToDuration();
|
||||
|
||||
|
||||
_ = ExecuteWithDatabase(c =>
|
||||
_repo.RegisterShardHeartbeat(c, shard.ShardId, latency.ToDuration()));
|
||||
}
|
||||
|
@@ -34,7 +34,7 @@ namespace PluralKit.Bot
|
||||
_logger = logger.ForContext<WebhookCacheService>();
|
||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<Webhook>>>();
|
||||
}
|
||||
|
||||
|
||||
public async Task<Webhook> GetWebhook(ulong channelId)
|
||||
{
|
||||
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
|
||||
@@ -46,14 +46,14 @@ namespace PluralKit.Bot
|
||||
return _webhooks.GetOrAdd(channelId, new Lazy<Task<Webhook>>(Factory));
|
||||
}
|
||||
var lazyWebhookValue = GetWebhookTaskInner();
|
||||
|
||||
|
||||
// If we've cached a failed Task, we need to clear it and try again
|
||||
// This is so errors don't become "sticky" and *they* in turn get cached (not good)
|
||||
// although, keep in mind this block gets hit the call *after* the task failed (since we only await it below)
|
||||
if (lazyWebhookValue.IsValueCreated && lazyWebhookValue.Value.IsFaulted)
|
||||
{
|
||||
_logger.Warning(lazyWebhookValue.Value.Exception, "Cached webhook task for {Channel} faulted with below exception", channelId);
|
||||
|
||||
|
||||
// Specifically don't recurse here so we don't infinite-loop - if this one errors too, it'll "stick"
|
||||
// until next time this function gets hit (which is okay, probably).
|
||||
_webhooks.TryRemove(channelId, out _);
|
||||
@@ -72,7 +72,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
// note: webhook.ChannelId may not be the same as channelId >.>
|
||||
_logger.Debug("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
||||
|
||||
|
||||
_webhooks.TryRemove(webhook.ChannelId, out _);
|
||||
return await GetWebhook(channelId);
|
||||
}
|
||||
@@ -81,7 +81,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channelId);
|
||||
_metrics.Measure.Meter.Mark(BotMetrics.WebhookCacheMisses);
|
||||
|
||||
|
||||
_logger.Debug("Finding webhook for channel {Channel}", channelId);
|
||||
var webhooks = await FetchChannelWebhooks(channelId);
|
||||
|
||||
@@ -89,12 +89,12 @@ namespace PluralKit.Bot
|
||||
var ourWebhook = webhooks.FirstOrDefault(IsWebhookMine);
|
||||
if (ourWebhook != null)
|
||||
return ourWebhook;
|
||||
|
||||
|
||||
// We don't have one, so we gotta create a new one
|
||||
// but first, make sure we haven't hit the webhook cap yet...
|
||||
if (webhooks.Length >= 10)
|
||||
throw new PKError("This channel has the maximum amount of possible webhooks (10) already created. A server admin must delete one or more webhooks so PluralKit can create one for proxying.");
|
||||
|
||||
|
||||
return await DoCreateWebhook(channelId);
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ namespace PluralKit.Bot
|
||||
return new Webhook[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task<Webhook> DoCreateWebhook(ulong channelId)
|
||||
{
|
||||
_logger.Information("Creating new webhook for channel {Channel}", channelId);
|
||||
|
@@ -21,10 +21,12 @@ using Serilog;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class WebhookExecutionErrorOnDiscordsEnd: Exception {
|
||||
public class WebhookExecutionErrorOnDiscordsEnd: Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd {
|
||||
|
||||
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd
|
||||
{
|
||||
// Exceptions for control flow? don't mind if I do
|
||||
// TODO: rewrite both of these as a normal exceptional return value (0?) in case of error to be discarded by caller
|
||||
}
|
||||
@@ -41,7 +43,7 @@ namespace PluralKit.Bot
|
||||
public Embed[] Embeds { get; init; }
|
||||
public bool AllowEveryone { get; init; }
|
||||
}
|
||||
|
||||
|
||||
public class WebhookExecutorService
|
||||
{
|
||||
private readonly IDiscordCache _cache;
|
||||
@@ -64,31 +66,32 @@ namespace PluralKit.Bot
|
||||
public async Task<Message> ExecuteWebhook(ProxyRequest req)
|
||||
{
|
||||
_logger.Verbose("Invoking webhook in channel {Channel}", req.ChannelId);
|
||||
|
||||
|
||||
// Get a webhook, execute it
|
||||
var webhook = await _webhookCache.GetWebhook(req.ChannelId);
|
||||
var webhookMessage = await ExecuteWebhookInner(webhook, req);
|
||||
|
||||
|
||||
// Log the relevant metrics
|
||||
_metrics.Measure.Meter.Mark(BotMetrics.MessagesProxied);
|
||||
_logger.Information("Invoked webhook {Webhook} in channel {Channel} (thread {ThreadId})", webhook.Id,
|
||||
req.ChannelId, req.ThreadId);
|
||||
|
||||
|
||||
return webhookMessage;
|
||||
}
|
||||
|
||||
public async Task<Message> EditWebhookMessage(ulong channelId, ulong messageId, string newContent)
|
||||
{
|
||||
var webhook = await _webhookCache.GetWebhook(channelId);
|
||||
var allowedMentions = newContent.ParseMentions() with {
|
||||
var allowedMentions = newContent.ParseMentions() with
|
||||
{
|
||||
Roles = Array.Empty<ulong>(),
|
||||
Parse = Array.Empty<AllowedMentions.ParseType>()
|
||||
};
|
||||
|
||||
return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId,
|
||||
new WebhookMessageEditRequest {Content = newContent, AllowedMentions = allowedMentions});
|
||||
new WebhookMessageEditRequest { Content = newContent, AllowedMentions = allowedMentions });
|
||||
}
|
||||
|
||||
|
||||
private async Task<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)
|
||||
{
|
||||
var guild = _cache.GetGuild(req.GuildId);
|
||||
@@ -96,7 +99,8 @@ namespace PluralKit.Bot
|
||||
|
||||
var allowedMentions = content.ParseMentions();
|
||||
if (!req.AllowEveryone)
|
||||
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with {
|
||||
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with
|
||||
{
|
||||
// also clear @everyones
|
||||
Parse = Array.Empty<AllowedMentions.ParseType>()
|
||||
};
|
||||
@@ -109,18 +113,19 @@ namespace PluralKit.Bot
|
||||
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
||||
Embeds = req.Embeds
|
||||
};
|
||||
|
||||
|
||||
MultipartFile[] files = null;
|
||||
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, 8 * 1024 * 1024);
|
||||
if (attachmentChunks.Count > 0)
|
||||
{
|
||||
_logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks",
|
||||
_logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks",
|
||||
req.Attachments.Length, req.Attachments.Select(a => a.Size).Sum() / 1024 / 1024, attachmentChunks.Count);
|
||||
files = await GetAttachmentFiles(attachmentChunks[0]);
|
||||
}
|
||||
|
||||
|
||||
Message webhookMessage;
|
||||
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime)) {
|
||||
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime))
|
||||
{
|
||||
try
|
||||
{
|
||||
webhookMessage = await _rest.ExecuteWebhook(webhook.Id, webhook.Token, webhookReq, files, req.ThreadId);
|
||||
@@ -139,14 +144,14 @@ namespace PluralKit.Bot
|
||||
// but is still in our cache. Invalidate, refresh, try again
|
||||
_logger.Warning("Error invoking webhook {Webhook} in channel {Channel} (thread {ThreadId})",
|
||||
webhook.Id, webhook.ChannelId, req.ThreadId);
|
||||
|
||||
|
||||
var newWebhook = await _webhookCache.InvalidateAndRefreshWebhook(req.ChannelId, webhook);
|
||||
return await ExecuteWebhookInner(newWebhook, req, hasRetried: true);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We don't care about whether the sending succeeds, and we don't want to *wait* for it, so we just fork it off
|
||||
var _ = TrySendRemainingAttachments(webhook, req.Name, req.AvatarUrl, attachmentChunks, req.ThreadId);
|
||||
@@ -161,11 +166,11 @@ namespace PluralKit.Bot
|
||||
for (var i = 1; i < attachmentChunks.Count; i++)
|
||||
{
|
||||
var files = await GetAttachmentFiles(attachmentChunks[i]);
|
||||
var req = new ExecuteWebhookRequest {Username = name, AvatarUrl = avatarUrl};
|
||||
var req = new ExecuteWebhookRequest { Username = name, AvatarUrl = avatarUrl };
|
||||
await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task<MultipartFile[]> GetAttachmentFiles(IReadOnlyCollection<Message.Attachment> attachments)
|
||||
{
|
||||
async Task<MultipartFile> GetStream(Message.Attachment attachment)
|
||||
@@ -184,7 +189,7 @@ namespace PluralKit.Bot
|
||||
// If any individual attachment is larger than 8MB, will throw an error
|
||||
var chunks = new List<List<Message.Attachment>>();
|
||||
var list = new List<Message.Attachment>();
|
||||
|
||||
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
if (attachment.Size >= sizeThreshold) throw Errors.AttachmentTooLarge;
|
||||
@@ -194,7 +199,7 @@ namespace PluralKit.Bot
|
||||
chunks.Add(list);
|
||||
list = new List<Message.Attachment>();
|
||||
}
|
||||
|
||||
|
||||
list.Add(attachment);
|
||||
}
|
||||
|
||||
@@ -212,7 +217,7 @@ namespace PluralKit.Bot
|
||||
// since Discord blocks webhooks containing the word "Clyde"... for some reason. /shrug
|
||||
return Regex.Replace(name, "(c)(lyde)", Replacement, RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
|
||||
private string FixSingleCharacterName(string proxyName)
|
||||
{
|
||||
if (proxyName.Length == 1)
|
||||
|
Reference in New Issue
Block a user