feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,37 +1,35 @@
using System.Threading.Tasks;
using NodaTime;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class CommandMessageService
{
public class CommandMessageService
private readonly IClock _clock;
private readonly IDatabase _db;
private readonly ILogger _logger;
private readonly ModelRepository _repo;
public CommandMessageService(IDatabase db, ModelRepository repo, IClock clock, ILogger logger)
{
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;
_repo = repo;
_clock = clock;
_logger = logger.ForContext<CommandMessageService>();
}
public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId)
{
_logger.Debug("Registering command response {MessageId} from author {AuthorId} in {ChannelId}", messageId, authorId, channelId);
await _repo.SaveCommandMessage(messageId, channelId, authorId);
}
public async Task<CommandMessage?> GetCommandMessage(ulong messageId)
{
return await _repo.GetCommandMessage(messageId);
}
_db = db;
_repo = repo;
_clock = clock;
_logger = logger.ForContext<CommandMessageService>();
}
public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId)
{
_logger.Debug(
"Registering command response {MessageId} from author {AuthorId} in {ChannelId}",
messageId, authorId, channelId
);
await _repo.SaveCommandMessage(messageId, channelId, authorId);
}
public async Task<CommandMessage?> GetCommandMessage(ulong messageId) =>
await _repo.GetCommandMessage(messageId);
}

View File

@@ -1,47 +1,45 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class CpuStatService
{
public class CpuStatService
private readonly ILogger _logger;
public CpuStatService(ILogger logger)
{
private readonly ILogger _logger;
_logger = logger;
}
public double LastCpuMeasure { get; private set; }
public double LastCpuMeasure { get; private set; }
public CpuStatService(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// Gets the current CPU usage. Estimation takes ~5 seconds (of mostly sleeping).
/// </summary>
public async Task<double> EstimateCpuUsage()
{
// We get the current processor time, wait 5 seconds, then compare
// https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
/// <summary>
/// Gets the current CPU usage. Estimation takes ~5 seconds (of mostly sleeping).
/// </summary>
public async Task<double> EstimateCpuUsage()
{
// 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();
_logger.Debug("Estimating CPU usage...");
var stopwatch = new Stopwatch();
stopwatch.Start();
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
stopwatch.Start();
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
await Task.Delay(5000);
await Task.Delay(5000);
stopwatch.Stop();
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
stopwatch.Stop();
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
var cpuTimePassed = cpuTimeAfter - cpuTimeBefore;
var timePassed = stopwatch.Elapsed;
var cpuTimePassed = cpuTimeAfter - cpuTimeBefore;
var timePassed = stopwatch.Elapsed;
var percent = cpuTimePassed / timePassed;
_logger.Debug("CPU usage measured as {Percent:P}", percent);
LastCpuMeasure = percent;
return percent;
}
var percent = cpuTimePassed / timePassed;
_logger.Debug("CPU usage measured as {Percent:P}", percent);
LastCpuMeasure = percent;
return percent;
}
}

View File

@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Humanizer;
using Myriad.Builders;
@@ -16,412 +11,431 @@ using NodaTime;
using PluralKit.Core;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class EmbedService
{
public class EmbedService
private readonly IDiscordCache _cache;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest;
public EmbedService(IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly IDiscordCache _cache;
private readonly DiscordApiClient _rest;
_db = db;
_repo = repo;
_cache = cache;
_rest = rest;
}
public EmbedService(IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
private Task<(ulong Id, User? User)[]> GetUsers(IEnumerable<ulong> ids)
{
async Task<(ulong Id, User? User)> Inner(ulong id)
{
_db = db;
_repo = repo;
_cache = cache;
_rest = rest;
var user = await _cache.GetOrFetchUser(_rest, id);
return (id, user);
}
private Task<(ulong Id, User? User)[]> GetUsers(IEnumerable<ulong> ids)
{
async Task<(ulong Id, User? User)> Inner(ulong id)
{
var user = await _cache.GetOrFetchUser(_rest, id);
return (id, user);
}
return Task.WhenAll(ids.Select(Inner));
}
return Task.WhenAll(ids.Select(Inner));
public async Task<Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
{
// Fetch/render info for all accounts simultaneously
var accounts = await _repo.GetSystemAccounts(system.Id);
var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})");
var memberCount = cctx.MatchPrivateFlag(ctx)
? await _repo.GetSystemMemberCount(system.Id, PrivacyLevel.Public)
: await _repo.GetSystemMemberCount(system.Id);
uint color;
try
{
color = system.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// There's no API for system colors yet, but defaulting to a blank color in advance can't be a bad idea
color = DiscordUtils.Gray;
}
public async Task<Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
var eb = new EmbedBuilder()
.Title(system.Name)
.Thumbnail(new Embed.EmbedThumbnail(system.AvatarUrl.TryGetCleanCdnUrl()))
.Footer(new Embed.EmbedFooter(
$"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}"))
.Color(color);
if (system.DescriptionPrivacy.CanAccess(ctx))
eb.Image(new Embed.EmbedImage(system.BannerImage));
var latestSwitch = await _repo.GetLatestSwitch(system.Id);
if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
{
var switchMembers =
await _db.Execute(conn => _repo.GetSwitchMembers(conn, latestSwitch.Id)).ToListAsync();
if (switchMembers.Count > 0)
eb.Field(new Embed.Field("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None),
string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))));
}
// Fetch/render info for all accounts simultaneously
var accounts = await _repo.GetSystemAccounts(system.Id);
var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})");
if (system.Tag != null)
eb.Field(new Embed.Field("Tag", system.Tag.EscapeMarkdown(), true));
var memberCount = cctx.MatchPrivateFlag(ctx) ? await _repo.GetSystemMemberCount(system.Id, PrivacyLevel.Public) : await _repo.GetSystemMemberCount(system.Id);
if (cctx.Guild != null)
{
var guildSettings = await _repo.GetSystemGuild(cctx.Guild.Id, system.Id);
uint color;
if (guildSettings.Tag != null && guildSettings.TagEnabled)
eb.Field(new Embed.Field($"Tag (in server '{cctx.Guild.Name}')", guildSettings.Tag
.EscapeMarkdown(), true));
if (!guildSettings.TagEnabled)
eb.Field(new Embed.Field($"Tag (in server '{cctx.Guild.Name}')",
"*(tag is disabled in this server)*"));
}
if (!system.Color.EmptyOrNull()) eb.Field(new Embed.Field("Color", $"#{system.Color}", true));
eb.Field(new Embed.Field("Linked accounts", string.Join("\n", users).Truncate(1000), true));
if (system.MemberListPrivacy.CanAccess(ctx))
{
if (memberCount > 0)
eb.Field(new Embed.Field($"Members ({memberCount})",
$"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true));
else
eb.Field(new Embed.Field($"Members ({memberCount})", "Add one with `pk;member new`!", true));
}
if (system.DescriptionFor(ctx) is { } desc)
eb.Field(new Embed.Field("Description", desc.NormalizeLineEndSpacing().Truncate(1024)));
return eb.Build();
}
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;
// sometimes Discord will just... not return the avatar hash with webhook messages
var avatar = proxiedMessage.Author.Avatar != null
? proxiedMessage.Author.AvatarUrl()
: member.AvatarFor(LookupContext.ByNonOwner);
var embed = new EmbedBuilder()
.Author(new Embed.EmbedAuthor($"#{channelName}: {name}", IconUrl: avatar))
.Thumbnail(new Embed.EmbedThumbnail(avatar))
.Description(proxiedMessage.Content?.NormalizeLineEndSpacing())
.Footer(new Embed.EmbedFooter(
$"System ID: {systemHid} | Member ID: {member.Hid} | Sender: {triggerMessage.Author.Username}#{triggerMessage.Author.Discriminator} ({triggerMessage.Author.Id}) | Message ID: {proxiedMessage.Id} | Original Message ID: {triggerMessage.Id}"))
.Timestamp(timestamp.ToDateTimeOffset().ToString("O"));
if (oldContent != null)
embed.Field(new Embed.Field("Old message", oldContent?.NormalizeLineEndSpacing().Truncate(1000)));
return embed.Build();
}
public async Task<Embed> CreateMemberEmbed(PKSystem system, PKMember member, Guild guild, LookupContext ctx)
{
// string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));
var name = member.NameFor(ctx);
if (system.Name != null) name = $"{name} ({system.Name})";
uint color;
try
{
color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// Bad API use can cause an invalid color string
// this is now fixed in the API, but might still have some remnants in the database
// so we just default to a blank color, yolo
color = DiscordUtils.Gray;
}
var guildSettings = guild != null ? await _repo.GetMemberGuild(guild.Id, member.Id) : null;
var guildDisplayName = guildSettings?.DisplayName;
var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);
var groups = await _repo.GetMemberGroups(member.Id)
.Where(g => g.Visibility.CanAccess(ctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
.ToListAsync();
var eb = new EmbedBuilder()
// TODO: add URL of website when that's up
.Author(new Embed.EmbedAuthor(name, IconUrl: avatar.TryGetCleanCdnUrl()))
// .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
.Color(color)
.Footer(new Embed.EmbedFooter(
$"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}" : "")}"));
if (member.DescriptionPrivacy.CanAccess(ctx))
eb.Image(new Embed.EmbedImage(member.BannerImage));
var description = "";
if (member.MemberVisibility == PrivacyLevel.Private) description += "*(this member is hidden)*\n";
if (guildSettings?.AvatarUrl != 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 Embed.EmbedThumbnail(avatar.TryGetCleanCdnUrl()));
if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx))
eb.Field(new Embed.Field("Display Name", member.DisplayName.Truncate(1024), true));
if (guild != null && guildDisplayName != null)
eb.Field(new Embed.Field($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true));
if (member.BirthdayFor(ctx) != null) eb.Field(new Embed.Field("Birthdate", member.BirthdayString, true));
if (member.PronounsFor(ctx) is { } pronouns && !string.IsNullOrWhiteSpace(pronouns))
eb.Field(new Embed.Field("Pronouns", pronouns.Truncate(1024), true));
if (member.MessageCountFor(ctx) is { } count && count > 0)
eb.Field(new Embed.Field("Message Count", member.MessageCount.ToString(), true));
if (member.HasProxyTags)
eb.Field(new Embed.Field("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)));
// if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
// if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
if (!member.Color.EmptyOrNull()) eb.Field(new Embed.Field("Color", $"#{member.Color}", true));
if (groups.Count > 0)
{
// More than 5 groups show in "compact" format without ID
var content = groups.Count > 5
? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
: string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
eb.Field(new Embed.Field($"Groups ({groups.Count})", content.Truncate(1000)));
}
if (member.DescriptionFor(ctx) is { } desc)
eb.Field(new Embed.Field("Description", member.Description.NormalizeLineEndSpacing()));
return eb.Build();
}
public async Task<Embed> CreateGroupEmbed(Context ctx, PKSystem system, PKGroup target)
{
var pctx = ctx.LookupContextFor(system);
var memberCount = ctx.MatchPrivateFlag(pctx)
? await _repo.GetGroupMemberCount(target.Id, PrivacyLevel.Public)
: await _repo.GetGroupMemberCount(target.Id);
var nameField = target.Name;
if (system.Name != null)
nameField = $"{nameField} ({system.Name})";
uint color;
try
{
color = target.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// There's no API for group colors yet, but defaulting to a blank color regardless
color = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Author(new Embed.EmbedAuthor(nameField, IconUrl: target.IconFor(pctx)))
.Color(color)
.Footer(new Embed.EmbedFooter(
$"System ID: {system.Hid} | Group ID: {target.Hid} | Created on {target.Created.FormatZoned(system)}"));
if (target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System)))
eb.Image(new Embed.EmbedImage(target.BannerImage));
if (target.DisplayName != null)
eb.Field(new Embed.Field("Display Name", target.DisplayName, true));
if (!target.Color.EmptyOrNull()) eb.Field(new Embed.Field("Color", $"#{target.Color}", true));
if (target.ListPrivacy.CanAccess(pctx))
{
if (memberCount == 0 && pctx == LookupContext.ByOwner)
// Only suggest the add command if this is actually the owner lol
eb.Field(new Embed.Field("Members (0)",
$"Add one with `pk;group {target.Reference()} add <member>`!"));
else
eb.Field(new Embed.Field($"Members ({memberCount})",
$"(see `pk;group {target.Reference()} list`)"));
}
if (target.DescriptionFor(pctx) is { } desc)
eb.Field(new Embed.Field("Description", desc));
if (target.IconFor(pctx) is { } icon)
eb.Thumbnail(new Embed.EmbedThumbnail(icon.TryGetCleanCdnUrl()));
return eb.Build();
}
public async Task<Embed> CreateFronterEmbed(PKSwitch sw, DateTimeZone zone, LookupContext ctx)
{
var members = await _db.Execute(c => _repo.GetSwitchMembers(c, sw.Id).ToListAsync().AsTask());
var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;
return new EmbedBuilder()
.Color(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? DiscordUtils.Gray)
.Field(new Embed.Field($"Current {"fronter".ToQuantity(members.Count, ShowQuantityAs.None)}",
members.Count > 0 ? string.Join(", ", members.Select(m => m.NameFor(ctx))) : "*(no fronter)*"))
.Field(new Embed.Field("Since",
$"{sw.Timestamp.FormatZoned(zone)} ({timeSinceSwitch.FormatDuration()} ago)"))
.Build();
}
public async Task<Embed> CreateMessageInfoEmbed(FullMessage msg, bool showContent)
{
var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel);
var ctx = LookupContext.ByNonOwner;
var serverMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
// Need this whole dance to handle cases where:
// - the user is deleted (userInfo == null)
// - the bot's no longer in the server we're querying (channel == null)
// - the member is no longer in the server we're querying (memberInfo == null)
// TODO: optimize ordering here a bit with new cache impl; and figure what happens if bot leaves server -> channel still cached -> hits this bit and 401s?
GuildMemberPartial memberInfo = null;
User userInfo = null;
if (channel != null)
{
GuildMember member = null;
try
{
color = system.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
member = await _rest.GetGuildMember(channel.GuildId!.Value, msg.Message.Sender);
}
catch (ArgumentException)
catch (ForbiddenException)
{
// There's no API for system colors yet, but defaulting to a blank color in advance can't be a bad idea
color = DiscordUtils.Gray;
// no permission, couldn't fetch, oh well
}
var eb = new EmbedBuilder()
.Title(system.Name)
.Thumbnail(new(system.AvatarUrl.TryGetCleanCdnUrl()))
.Footer(new($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}"))
.Color(color);
if (system.DescriptionPrivacy.CanAccess(ctx))
eb.Image(new(system.BannerImage));
var latestSwitch = await _repo.GetLatestSwitch(system.Id);
if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
{
var switchMembers = await _db.Execute(conn => _repo.GetSwitchMembers(conn, latestSwitch.Id)).ToListAsync();
if (switchMembers.Count > 0)
eb.Field(new("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))));
}
if (system.Tag != null)
eb.Field(new("Tag", system.Tag.EscapeMarkdown(), true));
if (cctx.Guild != null)
{
var guildSettings = await _repo.GetSystemGuild(cctx.Guild.Id, system.Id);
if (guildSettings.Tag != null && guildSettings.TagEnabled)
eb.Field(new($"Tag (in server '{cctx.Guild.Name}')", guildSettings.Tag
.EscapeMarkdown(), true));
if (!guildSettings.TagEnabled)
eb.Field(new($"Tag (in server '{cctx.Guild.Name}')", "*(tag is disabled in this server)*"));
}
if (!system.Color.EmptyOrNull()) eb.Field(new("Color", $"#{system.Color}", true));
eb.Field(new("Linked accounts", string.Join("\n", users).Truncate(1000), true));
if (system.MemberListPrivacy.CanAccess(ctx))
{
if (memberCount > 0)
eb.Field(new($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true));
else
eb.Field(new($"Members ({memberCount})", "Add one with `pk;member new`!", true));
}
if (system.DescriptionFor(ctx) is { } desc)
eb.Field(new("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false));
return eb.Build();
if (member != null)
// Don't do an extra request if we already have this info from the member lookup
userInfo = member.User;
memberInfo = member;
}
public Embed CreateLoggedMessageEmbed(Message triggerMessage, Message proxiedMessage, string systemHid, PKMember member, string channelName, string oldContent = null)
if (userInfo == null)
userInfo = await _cache.GetOrFetchUser(_rest, msg.Message.Sender);
// Calculate string displayed under "Sent by"
string userStr;
if (memberInfo != null && memberInfo.Nick != null)
userStr = $"**Username:** {userInfo.NameAndMention()}\n**Nickname:** {memberInfo.Nick}";
else if (userInfo != null) userStr = userInfo.NameAndMention();
else userStr = $"*(deleted user {msg.Message.Sender})*";
var content = serverMsg?.Content?.NormalizeLineEndSpacing();
if (content == null || !showContent)
content = "*(message contents deleted or inaccessible)*";
// Put it all together
var eb = new EmbedBuilder()
.Author(new Embed.EmbedAuthor(msg.Member.NameFor(ctx),
IconUrl: msg.Member.AvatarFor(ctx).TryGetCleanCdnUrl()))
.Description(content)
.Image(showContent ? new Embed.EmbedImage(serverMsg?.Attachments?.FirstOrDefault()?.Url) : null)
.Field(new Embed.Field("System",
msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true))
.Field(new Embed.Field("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true))
.Field(new Embed.Field("Sent by", userStr, true))
.Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));
var roles = memberInfo?.Roles?.ToList();
if (roles != null && roles.Count > 0 && showContent)
{
// TODO: pronouns in ?-reacted response using this card
var timestamp = DiscordUtils.SnowflakeToInstant(proxiedMessage.Id);
var name = proxiedMessage.Author.Username;
// sometimes Discord will just... not return the avatar hash with webhook messages
var avatar = proxiedMessage.Author.Avatar != null ? proxiedMessage.Author.AvatarUrl() : member.AvatarFor(LookupContext.ByNonOwner);
var embed = new EmbedBuilder()
.Author(new($"#{channelName}: {name}", IconUrl: avatar))
.Thumbnail(new(avatar))
.Description(proxiedMessage.Content?.NormalizeLineEndSpacing())
.Footer(new($"System ID: {systemHid} | Member ID: {member.Hid} | Sender: {triggerMessage.Author.Username}#{triggerMessage.Author.Discriminator} ({triggerMessage.Author.Id}) | Message ID: {proxiedMessage.Id} | Original Message ID: {triggerMessage.Id}"))
.Timestamp(timestamp.ToDateTimeOffset().ToString("O"));
if (oldContent != null)
embed.Field(new("Old message", oldContent?.NormalizeLineEndSpacing().Truncate(1000)));
return embed.Build();
}
public async Task<Embed> CreateMemberEmbed(PKSystem system, PKMember member, Guild guild, LookupContext ctx)
{
// string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));
var name = member.NameFor(ctx);
if (system.Name != null) name = $"{name} ({system.Name})";
uint color;
try
{
color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// Bad API use can cause an invalid color string
// this is now fixed in the API, but might still have some remnants in the database
// so we just default to a blank color, yolo
color = DiscordUtils.Gray;
}
var guildSettings = guild != null ? await _repo.GetMemberGuild(guild.Id, member.Id) : null;
var guildDisplayName = guildSettings?.DisplayName;
var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);
var groups = await _repo.GetMemberGroups(member.Id)
.Where(g => g.Visibility.CanAccess(ctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
.ToListAsync();
var eb = new EmbedBuilder()
// TODO: add URL of website when that's up
.Author(new(name, IconUrl: avatar.TryGetCleanCdnUrl()))
// .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
.Color(color)
.Footer(new(
$"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}" : "")}"));
if (member.DescriptionPrivacy.CanAccess(ctx))
eb.Image(new(member.BannerImage));
var description = "";
if (member.MemberVisibility == PrivacyLevel.Private) description += "*(this member is hidden)*\n";
if (guildSettings?.AvatarUrl != 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.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)));
// if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
// if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
if (!member.Color.EmptyOrNull()) eb.Field(new("Color", $"#{member.Color}", true));
if (groups.Count > 0)
{
// More than 5 groups show in "compact" format without ID
var content = groups.Count > 5
? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
: string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
eb.Field(new($"Groups ({groups.Count})", content.Truncate(1000)));
}
if (member.DescriptionFor(ctx) is { } desc)
eb.Field(new("Description", member.Description.NormalizeLineEndSpacing(), false));
return eb.Build();
}
public async Task<Embed> CreateGroupEmbed(Context ctx, PKSystem system, PKGroup target)
{
var pctx = ctx.LookupContextFor(system);
var memberCount = ctx.MatchPrivateFlag(pctx) ? await _repo.GetGroupMemberCount(target.Id, PrivacyLevel.Public) : await _repo.GetGroupMemberCount(target.Id);
var nameField = target.Name;
if (system.Name != null)
nameField = $"{nameField} ({system.Name})";
uint color;
try
{
color = target.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// There's no API for group colors yet, but defaulting to a blank color regardless
color = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Author(new(nameField, IconUrl: target.IconFor(pctx)))
.Color(color)
.Footer(new($"System ID: {system.Hid} | Group ID: {target.Hid} | Created on {target.Created.FormatZoned(system)}"));
if (target.DescriptionPrivacy.CanAccess(ctx.LookupContextFor(target.System)))
eb.Image(new(target.BannerImage));
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))
{
if (memberCount == 0 && pctx == LookupContext.ByOwner)
// Only suggest the add command if this is actually the owner lol
eb.Field(new("Members (0)", $"Add one with `pk;group {target.Reference()} add <member>`!", false));
else
eb.Field(new($"Members ({memberCount})", $"(see `pk;group {target.Reference()} list`)", false));
}
if (target.DescriptionFor(pctx) is { } desc)
eb.Field(new("Description", desc));
if (target.IconFor(pctx) is { } icon)
eb.Thumbnail(new(icon.TryGetCleanCdnUrl()));
return eb.Build();
}
public async Task<Embed> CreateFronterEmbed(PKSwitch sw, DateTimeZone zone, LookupContext ctx)
{
var members = await _db.Execute(c => _repo.GetSwitchMembers(c, sw.Id).ToListAsync().AsTask());
var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;
return new EmbedBuilder()
.Color(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? DiscordUtils.Gray)
.Field(new($"Current {"fronter".ToQuantity(members.Count, ShowQuantityAs.None)}", members.Count > 0 ? string.Join(", ", members.Select(m => m.NameFor(ctx))) : "*(no fronter)*"))
.Field(new("Since", $"{sw.Timestamp.FormatZoned(zone)} ({timeSinceSwitch.FormatDuration()} ago)"))
.Build();
}
public async Task<Embed> CreateMessageInfoEmbed(FullMessage msg, bool showContent)
{
var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel);
var ctx = LookupContext.ByNonOwner;
var serverMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
// Need this whole dance to handle cases where:
// - the user is deleted (userInfo == null)
// - the bot's no longer in the server we're querying (channel == null)
// - the member is no longer in the server we're querying (memberInfo == null)
// TODO: optimize ordering here a bit with new cache impl; and figure what happens if bot leaves server -> channel still cached -> hits this bit and 401s?
GuildMemberPartial memberInfo = null;
User userInfo = null;
if (channel != null)
{
GuildMember member = null;
try
{
member = await _rest.GetGuildMember(channel.GuildId!.Value, msg.Message.Sender);
}
catch (ForbiddenException)
{
// no permission, couldn't fetch, oh well
}
if (member != null)
// Don't do an extra request if we already have this info from the member lookup
userInfo = member.User;
memberInfo = member;
}
if (userInfo == null)
userInfo = await _cache.GetOrFetchUser(_rest, msg.Message.Sender);
// Calculate string displayed under "Sent by"
string userStr;
if (memberInfo != null && memberInfo.Nick != null)
userStr = $"**Username:** {userInfo.NameAndMention()}\n**Nickname:** {memberInfo.Nick}";
else if (userInfo != null) userStr = userInfo.NameAndMention();
else userStr = $"*(deleted user {msg.Message.Sender})*";
var content = serverMsg?.Content?.NormalizeLineEndSpacing();
if (content == null || !showContent)
content = "*(message contents deleted or inaccessible)*";
// Put it all together
var eb = new EmbedBuilder()
.Author(new(msg.Member.NameFor(ctx), IconUrl: msg.Member.AvatarFor(ctx).TryGetCleanCdnUrl()))
.Description(content)
.Image(showContent ? new(serverMsg?.Attachments?.FirstOrDefault()?.Url) : null)
.Field(new("System",
msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true))
.Field(new("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true))
.Field(new("Sent by", userStr, true))
.Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));
var roles = memberInfo?.Roles?.ToList();
if (roles != null && roles.Count > 0 && showContent)
{
var rolesString = string.Join(", ", (await Task.WhenAll(roles
var rolesString = string.Join(", ", (await Task.WhenAll(roles
.Select(async id =>
{
var role = await _cache.TryGetRole(id);
if (role != null)
return role;
return new Role()
{
Name = "*(unknown role)*",
Position = 0,
};
return new Role { Name = "*(unknown role)*", Position = 0 };
})))
.OrderByDescending(role => role.Position)
.Select(role => role.Name));
eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));
}
return eb.Build();
.OrderByDescending(role => role.Position)
.Select(role => role.Name));
eb.Field(new Embed.Field($"Account roles ({roles.Count})", rolesString.Truncate(1024)));
}
public Task<Embed> CreateFrontPercentEmbed(FrontBreakdown breakdown, PKSystem system, PKGroup group, DateTimeZone tz, LookupContext ctx, string embedTitle, bool ignoreNoFronters, bool showFlat)
return eb.Build();
}
public Task<Embed> CreateFrontPercentEmbed(FrontBreakdown breakdown, PKSystem system, PKGroup group,
DateTimeZone tz, LookupContext ctx, string embedTitle,
bool ignoreNoFronters, bool showFlat)
{
var color = system.Color;
if (group != null) color = group.Color;
uint embedColor;
try
{
string color = system.Color;
if (group != null)
{
color = group.Color;
}
uint embedColor;
try
{
embedColor = color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
embedColor = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Title(embedTitle)
.Color(embedColor);
string footer = $"Since {breakdown.RangeStart.FormatZoned(tz)} ({(breakdown.RangeEnd - breakdown.RangeStart).FormatDuration()} ago)";
Duration period;
if (showFlat)
{
period = Duration.FromTicks(breakdown.MemberSwitchDurations.Values.ToList().Sum(i => i.TotalTicks));
footer += ". Showing flat list (percentages add up to 100%)";
if (!ignoreNoFronters) period += breakdown.NoFronterDuration;
else footer += ", ignoring switch-outs";
}
else if (ignoreNoFronters)
{
period = breakdown.RangeEnd - breakdown.RangeStart - breakdown.NoFronterDuration;
footer += ". Ignoring switch-outs";
}
else
period = breakdown.RangeEnd - breakdown.RangeStart;
eb.Footer(new(footer));
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"
// We convert to a list of pairs so we can add the no-fronter value
// Dictionary doesn't allow for null keys so we instead have a pair with a null key ;)
var pairs = breakdown.MemberSwitchDurations.ToList();
if (breakdown.NoFronterDuration != Duration.Zero && !ignoreNoFronters)
pairs.Add(new KeyValuePair<PKMember, Duration>(null, breakdown.NoFronterDuration));
var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList();
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()})"));
}
if (membersOrdered.Count > maxEntriesToDisplay)
{
eb.Field(new("(others)",
membersOrdered.Skip(maxEntriesToDisplay)
.Aggregate(Duration.Zero, (prod, next) => prod + next.Value)
.FormatDuration(), true));
}
return Task.FromResult(eb.Build());
embedColor = color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
embedColor = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Title(embedTitle)
.Color(embedColor);
var footer =
$"Since {breakdown.RangeStart.FormatZoned(tz)} ({(breakdown.RangeEnd - breakdown.RangeStart).FormatDuration()} ago)";
Duration period;
if (showFlat)
{
period = Duration.FromTicks(breakdown.MemberSwitchDurations.Values.ToList().Sum(i => i.TotalTicks));
footer += ". Showing flat list (percentages add up to 100%)";
if (!ignoreNoFronters) period += breakdown.NoFronterDuration;
else footer += ", ignoring switch-outs";
}
else if (ignoreNoFronters)
{
period = breakdown.RangeEnd - breakdown.RangeStart - breakdown.NoFronterDuration;
footer += ". Ignoring switch-outs";
}
else
{
period = breakdown.RangeEnd - breakdown.RangeStart;
}
eb.Footer(new Embed.EmbedFooter(footer));
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"
// We convert to a list of pairs so we can add the no-fronter value
// Dictionary doesn't allow for null keys so we instead have a pair with a null key ;)
var pairs = breakdown.MemberSwitchDurations.ToList();
if (breakdown.NoFronterDuration != Duration.Zero && !ignoreNoFronters)
pairs.Add(new KeyValuePair<PKMember, Duration>(null, breakdown.NoFronterDuration));
var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList();
foreach (var pair in membersOrdered)
{
var frac = pair.Value / period;
eb.Field(new Embed.Field(pair.Key?.NameFor(ctx) ?? "*(no fronter)*",
$"{frac * 100:F0}% ({pair.Value.FormatDuration()})"));
}
if (membersOrdered.Count > maxEntriesToDisplay)
eb.Field(new Embed.Field("(others)",
membersOrdered.Skip(maxEntriesToDisplay)
.Aggregate(Duration.Zero, (prod, next) => prod + next.Value)
.FormatDuration(), true));
return Task.FromResult(eb.Build());
}
}

View File

@@ -1,94 +1,91 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using App.Metrics;
using Myriad.Builders;
using Myriad.Rest;
using Myriad.Rest.Types.Requests;
using Myriad.Types;
using NodaTime;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class ErrorMessageService
{
public class ErrorMessageService
// globally rate limit errors for now, don't want to spam users when something breaks
private static readonly Duration MinErrorInterval = Duration.FromSeconds(10);
private static readonly Duration IntervalFromStartup = Duration.FromMinutes(5);
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly DiscordApiClient _rest;
public ErrorMessageService(IMetrics metrics, ILogger logger, DiscordApiClient rest)
{
// globally rate limit errors for now, don't want to spam users when something breaks
private static readonly Duration MinErrorInterval = Duration.FromSeconds(10);
private static readonly Duration IntervalFromStartup = Duration.FromMinutes(5);
_metrics = metrics;
_logger = logger;
_rest = rest;
// private readonly ConcurrentDictionary<ulong, Instant> _lastErrorInChannel = new ConcurrentDictionary<ulong, Instant>();
private Instant lastErrorTime { get; set; }
lastErrorTime = SystemClock.Instance.GetCurrentInstant();
}
private readonly IMetrics _metrics;
private readonly ILogger _logger;
private readonly DiscordApiClient _rest;
// private readonly ConcurrentDictionary<ulong, Instant> _lastErrorInChannel = new ConcurrentDictionary<ulong, Instant>();
private Instant lastErrorTime { get; set; }
public ErrorMessageService(IMetrics metrics, ILogger logger, DiscordApiClient rest)
public async Task SendErrorMessage(ulong channelId, string errorId)
{
var now = SystemClock.Instance.GetCurrentInstant();
if (!ShouldSendErrorMessage(channelId, now))
{
_metrics = metrics;
_logger = logger;
_rest = rest;
lastErrorTime = SystemClock.Instance.GetCurrentInstant();
_logger.Warning("Rate limited sending error message to {ChannelId} with error code {ErrorId}",
channelId, errorId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "throttled");
return;
}
public async Task SendErrorMessage(ulong channelId, string errorId)
var embed = new EmbedBuilder()
.Color(0xE74C3C)
.Title("Internal error occurred")
.Description(
"For support, please send the error code above in **#bug-reports-and-errors** on **[the support server *(click to join)*](https://discord.gg/PczBt78)** with a description of what you were doing at the time.")
.Footer(new Embed.EmbedFooter(errorId))
.Timestamp(now.ToDateTimeOffset().ToString("O"));
try
{
var now = SystemClock.Instance.GetCurrentInstant();
if (!ShouldSendErrorMessage(channelId, now))
{
_logger.Warning("Rate limited sending error message to {ChannelId} with error code {ErrorId}", channelId, errorId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "throttled");
return;
}
await _rest.CreateMessage(channelId,
new MessageRequest { Content = $"> **Error code:** `{errorId}`", Embed = embed.Build() });
var embed = new EmbedBuilder()
.Color(0xE74C3C)
.Title("Internal error occurred")
.Description("For support, please send the error code above in **#bug-reports-and-errors** on **[the support server *(click to join)*](https://discord.gg/PczBt78)** with a description of what you were doing at the time.")
.Footer(new(errorId))
.Timestamp(now.ToDateTimeOffset().ToString("O"));
try
{
await _rest.CreateMessage(channelId, new()
{
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");
}
catch (Exception e)
{
_logger.Error(e, "Error sending error message to {ChannelId}", channelId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "failed");
throw;
}
_logger.Information("Sent error message to {ChannelId} with error code {ErrorId}", channelId, errorId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "sent");
}
private bool ShouldSendErrorMessage(ulong channelId, Instant now)
catch (Exception e)
{
// if (_lastErrorInChannel.TryGetValue(channelId, out var lastErrorTime))
var startupTime = Instant.FromDateTimeUtc(Process.GetCurrentProcess().StartTime.ToUniversalTime());
// don't send errors during startup
// mostly because Npgsql throws a bunch of errors when opening connections sometimes???
if ((now - startupTime) < IntervalFromStartup)
return false;
var interval = now - lastErrorTime;
if (interval < MinErrorInterval)
return false;
// _lastErrorInChannel[channelId] = now;
lastErrorTime = now;
return true;
_logger.Error(e, "Error sending error message to {ChannelId}", channelId);
_metrics.Measure.Meter.Mark(BotMetrics.ErrorMessagesSent, "failed");
throw;
}
}
private bool ShouldSendErrorMessage(ulong channelId, Instant now)
{
// if (_lastErrorInChannel.TryGetValue(channelId, out var lastErrorTime))
var startupTime = Instant.FromDateTimeUtc(Process.GetCurrentProcess().StartTime.ToUniversalTime());
// don't send errors during startup
// mostly because Npgsql throws a bunch of errors when opening connections sometimes???
if (now - startupTime < IntervalFromStartup)
return false;
var interval = now - lastErrorTime;
if (interval < MinErrorInterval)
return false;
// _lastErrorInChannel[channelId] = now;
lastErrorTime = now;
return true;
}
}

View File

@@ -1,100 +1,94 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using NodaTime;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class InteractionDispatchService: IDisposable
{
public class InteractionDispatchService: IDisposable
private static readonly Duration DefaultExpiry = Duration.FromMinutes(15);
private readonly Task _cleanupWorker;
private readonly IClock _clock;
private readonly CancellationTokenSource _cts = new();
private readonly ConcurrentDictionary<Guid, RegisteredInteraction> _handlers = new();
private readonly ILogger _logger;
public InteractionDispatchService(IClock clock, ILogger logger)
{
private static readonly Duration DefaultExpiry = Duration.FromMinutes(15);
_clock = clock;
_logger = logger.ForContext<InteractionDispatchService>();
private readonly ConcurrentDictionary<Guid, RegisteredInteraction> _handlers = new();
private readonly CancellationTokenSource _cts = new();
private readonly IClock _clock;
private readonly ILogger _logger;
private readonly Task _cleanupWorker;
_cleanupWorker = CleanupLoop(_cts.Token);
}
public InteractionDispatchService(IClock clock, ILogger logger)
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
public async ValueTask<bool> Dispatch(string customId, InteractionContext context)
{
if (!Guid.TryParse(customId, out var customIdGuid))
return false;
if (!_handlers.TryGetValue(customIdGuid, out var handler))
return false;
await handler.Callback.Invoke(context);
return true;
}
public void Unregister(string customId)
{
if (!Guid.TryParse(customId, out var customIdGuid))
return;
_handlers.TryRemove(customIdGuid, out _);
}
public string Register(Func<InteractionContext, Task> callback, Duration? expiry = null)
{
var key = Guid.NewGuid();
var handler = new RegisteredInteraction
{
_clock = clock;
_logger = logger.ForContext<InteractionDispatchService>();
Callback = callback,
Expiry = _clock.GetCurrentInstant() + (expiry ?? DefaultExpiry)
};
_cleanupWorker = CleanupLoop(_cts.Token);
}
_handlers[key] = handler;
return key.ToString();
}
public async ValueTask<bool> Dispatch(string customId, InteractionContext context)
private async Task CleanupLoop(CancellationToken ct)
{
while (true)
{
if (!Guid.TryParse(customId, out var customIdGuid))
return false;
if (!_handlers.TryGetValue(customIdGuid, out var handler))
return false;
await handler.Callback.Invoke(context);
return true;
}
public void Unregister(string customId)
{
if (!Guid.TryParse(customId, out var customIdGuid))
return;
_handlers.TryRemove(customIdGuid, out _);
}
public string Register(Func<InteractionContext, Task> callback, Duration? expiry = null)
{
var key = Guid.NewGuid();
var handler = new RegisteredInteraction
{
Callback = callback,
Expiry = _clock.GetCurrentInstant() + (expiry ?? DefaultExpiry)
};
_handlers[key] = handler;
return key.ToString();
}
private async Task CleanupLoop(CancellationToken ct)
{
while (true)
{
DoCleanup();
await Task.Delay(TimeSpan.FromMinutes(1), ct);
}
}
private void DoCleanup()
{
var now = _clock.GetCurrentInstant();
var removedCount = 0;
foreach (var (key, value) in _handlers.ToArray())
{
if (value.Expiry < now)
{
_handlers.TryRemove(key, out _);
removedCount++;
}
}
_logger.Debug("Removed {ExpiredInteractions} expired interactions", removedCount);
}
private struct RegisteredInteraction
{
public Instant Expiry;
public Func<InteractionContext, Task> Callback;
}
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
DoCleanup();
await Task.Delay(TimeSpan.FromMinutes(1), ct);
}
}
private void DoCleanup()
{
var now = _clock.GetCurrentInstant();
var removedCount = 0;
foreach (var (key, value) in _handlers.ToArray())
if (value.Expiry < now)
{
_handlers.TryRemove(key, out _);
removedCount++;
}
_logger.Debug("Removed {ExpiredInteractions} expired interactions", removedCount);
}
private struct RegisteredInteraction
{
public Instant Expiry;
public Func<InteractionContext, Task> Callback;
}
}

View File

@@ -1,76 +1,74 @@
#nullable enable
using System.Collections.Concurrent;
using System.Collections.Generic;
using Myriad.Types;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class LastMessageCacheService
{
// TODO: Should this be moved to Myriad.Cache?
public class LastMessageCacheService
private readonly IDictionary<ulong, CacheEntry> _cache = new ConcurrentDictionary<ulong, CacheEntry>();
public void AddMessage(Message msg)
{
private readonly IDictionary<ulong, CacheEntry> _cache = new ConcurrentDictionary<ulong, CacheEntry>();
public void AddMessage(Message msg)
{
var previous = GetLastMessage(msg.ChannelId);
var current = ToCachedMessage(msg);
_cache[msg.ChannelId] = new(current, previous?.Current);
}
private CachedMessage ToCachedMessage(Message msg) =>
new(msg.Id, msg.ReferencedMessage.Value?.Id, msg.Author.Username);
public CacheEntry? GetLastMessage(ulong channel)
{
return _cache.TryGetValue(channel, out var message) ? message : null;
}
public void HandleMessageDeletion(ulong channel, ulong message)
{
var storedMessage = GetLastMessage(channel);
if (storedMessage == null)
return;
if (message == storedMessage.Current.Id)
if (storedMessage.Previous != null)
_cache[channel] = new(storedMessage.Previous, null);
else
_cache.Remove(channel);
else if (message == storedMessage.Previous?.Id)
_cache[channel] = new(storedMessage.Current, null);
}
public void HandleMessageDeletion(ulong channel, List<ulong> messages)
{
var storedMessage = GetLastMessage(channel);
if (storedMessage == null)
return;
if (!(messages.Contains(storedMessage.Current.Id) || storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Id)))
// none of the deleted messages are relevant to the cache
return;
ulong? newLastMessage = null;
if (messages.Contains(storedMessage.Current.Id))
newLastMessage = storedMessage.Previous?.Id;
if (storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Id))
if (newLastMessage == storedMessage.Previous?.Id)
newLastMessage = null;
else
{
_cache[channel] = new(storedMessage.Current, null);
return;
}
if (newLastMessage == null)
_cache.Remove(channel);
}
var previous = GetLastMessage(msg.ChannelId);
var current = ToCachedMessage(msg);
_cache[msg.ChannelId] = new CacheEntry(current, previous?.Current);
}
public record CacheEntry(CachedMessage Current, CachedMessage? Previous);
private CachedMessage ToCachedMessage(Message msg) =>
new(msg.Id, msg.ReferencedMessage.Value?.Id, msg.Author.Username);
public record CachedMessage(ulong Id, ulong? ReferencedMessage, string AuthorUsername);
}
public CacheEntry? GetLastMessage(ulong channel) =>
_cache.TryGetValue(channel, out var message) ? message : null;
public void HandleMessageDeletion(ulong channel, ulong message)
{
var storedMessage = GetLastMessage(channel);
if (storedMessage == null)
return;
if (message == storedMessage.Current.Id)
if (storedMessage.Previous != null)
_cache[channel] = new CacheEntry(storedMessage.Previous, null);
else
_cache.Remove(channel);
else if (message == storedMessage.Previous?.Id)
_cache[channel] = new CacheEntry(storedMessage.Current, null);
}
public void HandleMessageDeletion(ulong channel, List<ulong> messages)
{
var storedMessage = GetLastMessage(channel);
if (storedMessage == null)
return;
if (!(messages.Contains(storedMessage.Current.Id) ||
storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Id)))
// none of the deleted messages are relevant to the cache
return;
ulong? newLastMessage = null;
if (messages.Contains(storedMessage.Current.Id))
newLastMessage = storedMessage.Previous?.Id;
if (storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Id))
if (newLastMessage == storedMessage.Previous?.Id)
{
newLastMessage = null;
}
else
{
_cache[channel] = new CacheEntry(storedMessage.Current, null);
return;
}
if (newLastMessage == null)
_cache.Remove(channel);
}
}
public record CacheEntry(CachedMessage Current, CachedMessage? Previous);
public record CachedMessage(ulong Id, ulong? ReferencedMessage, string AuthorUsername);

View File

@@ -1,108 +1,113 @@
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Rest;
using Myriad.Rest.Types.Requests;
using Myriad.Types;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class LogChannelService
{
public class LogChannelService
private readonly Bot _bot;
private readonly IDiscordCache _cache;
private readonly IDatabase _db;
private readonly EmbedService _embed;
private readonly ILogger _logger;
private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest;
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo,
IDiscordCache cache, DiscordApiClient rest, Bot bot)
{
private readonly EmbedService _embed;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly ILogger _logger;
private readonly IDiscordCache _cache;
private readonly DiscordApiClient _rest;
private readonly Bot _bot;
_embed = embed;
_db = db;
_repo = repo;
_cache = cache;
_rest = rest;
_bot = bot;
_logger = logger.ForContext<LogChannelService>();
}
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest, Bot bot)
public async ValueTask LogMessage(MessageContext ctx, PKMessage proxiedMessage, Message trigger,
Message hookMessage, string oldContent = null)
{
var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxiedMessage);
if (logChannel == null)
return;
var triggerChannel = await _cache.GetChannel(proxiedMessage.Channel);
var system = await _repo.GetSystem(ctx.SystemId.Value);
var member = await _repo.GetMember(proxiedMessage.Member);
// 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 MessageRequest { Content = url, Embed = embed });
}
private async Task<Channel?> GetAndCheckLogChannel(MessageContext ctx, Message trigger,
PKMessage proxiedMessage)
{
if (proxiedMessage.Guild == null && proxiedMessage.Channel != trigger.ChannelId)
// a very old message is being edited outside of its original channel
// we can't know if we're in the correct guild, so skip fetching a log channel
return null;
var guildId = proxiedMessage.Guild ?? trigger.GuildId.Value;
var logChannelId = ctx.LogChannel;
var isBlacklisted = ctx.InLogBlacklist;
if (proxiedMessage.Guild != trigger.GuildId)
{
_embed = embed;
_db = db;
_repo = repo;
_cache = cache;
_rest = rest;
_bot = bot;
_logger = logger.ForContext<LogChannelService>();
// we're editing a message from a different server, get log channel info from the database
var guild = await _repo.GetGuild(proxiedMessage.Guild.Value);
logChannelId = guild.LogChannel;
isBlacklisted = guild.Blacklist.Any(x => x == logChannelId);
}
public async ValueTask LogMessage(MessageContext ctx, PKMessage proxiedMessage, Message trigger, Message hookMessage, string oldContent = null)
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 = await _cache.PermissionsIn(logChannel.Id);
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
{
var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxiedMessage);
if (logChannel == null)
return;
var triggerChannel = await _cache.GetChannel(proxiedMessage.Channel);
var system = await _repo.GetSystem(ctx.SystemId.Value);
var member = await _repo.GetMember(proxiedMessage.Member);
// 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 });
}
private async Task<Channel?> GetAndCheckLogChannel(MessageContext ctx, Message trigger, PKMessage proxiedMessage)
{
if (proxiedMessage.Guild == null && proxiedMessage.Channel != trigger.ChannelId)
// a very old message is being edited outside of its original channel
// we can't know if we're in the correct guild, so skip fetching a log channel
return null;
var guildId = proxiedMessage.Guild ?? trigger.GuildId.Value;
var logChannelId = ctx.LogChannel;
var isBlacklisted = ctx.InLogBlacklist;
if (proxiedMessage.Guild != trigger.GuildId)
{
// we're editing a message from a different server, get log channel info from the database
var guild = await _repo.GetGuild(proxiedMessage.Guild.Value);
logChannelId = guild.LogChannel;
isBlacklisted = guild.Blacklist.Any(x => x == logChannelId);
}
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 = await _cache.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})",
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
return null;
}
return logChannel;
}
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
{
// TODO: fetch it directly on cache miss?
if (await _cache.TryGetChannel(channelId) is Channel 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 });
_logger.Information(
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
return null;
}
return logChannel;
}
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
{
// TODO: fetch it directly on cache miss?
if (await _cache.TryGetChannel(channelId) is Channel 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 });
return null;
}
}

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Dapper;
using Myriad.Cache;
using Myriad.Extensions;
@@ -10,8 +8,6 @@ using Myriad.Rest;
using Myriad.Rest.Exceptions;
using Myriad.Types;
using Dapper;
using NodaTime;
using NodaTime.Extensions;
using NodaTime.Text;
@@ -20,376 +16,386 @@ using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class LoggerCleanService
{
public class LoggerCleanService
private static readonly Regex _basicRegex = new("(\\d{17,19})");
private static readonly Regex _dynoRegex = new("Message ID: (\\d{17,19})");
private static readonly Regex _carlRegex = new("ID: (\\d{17,19})");
private static readonly Regex _circleRegex = new("\\(`(\\d{17,19})`\\)");
private static readonly Regex _loggerARegex = new("Message = (\\d{17,19})");
private static readonly Regex _loggerBRegex = new("MessageID:(\\d{17,19})");
private static readonly Regex _auttajaRegex = new("Message (\\d{17,19}) deleted");
private static readonly Regex _mantaroRegex =
new("Message \\(?ID:? (\\d{17,19})\\)? created by .* in channel .* was deleted\\.");
private static readonly Regex _pancakeRegex = new("Message from <@(\\d{17,19})> deleted in");
private static readonly Regex _unbelievaboatRegex = new("Message ID: (\\d{17,19})");
private static readonly Regex _vanessaRegex = new("Message sent by <@!?(\\d{17,19})> deleted in");
private static readonly Regex _salRegex = new("\\(ID: (\\d{17,19})\\)");
private static readonly Regex _GearBotRegex = new("\\(``(\\d{17,19})``\\) in <#\\d{17,19}> has been removed.");
private static readonly Regex _GiselleRegex = new("\\*\\*Message ID\\*\\*: `(\\d{17,19})`");
private static readonly Regex _VortexRegex =
new("`\\[(\\d\\d:\\d\\d:\\d\\d)\\]` .* \\(ID:(\\d{17,19})\\).* <#\\d{17,19}>:");
private static readonly Dictionary<ulong, LoggerBot> _bots = new[]
{
private static readonly Regex _basicRegex = new("(\\d{17,19})");
private static readonly Regex _dynoRegex = new("Message ID: (\\d{17,19})");
private static readonly Regex _carlRegex = new("ID: (\\d{17,19})");
private static readonly Regex _circleRegex = new("\\(`(\\d{17,19})`\\)");
private static readonly Regex _loggerARegex = new("Message = (\\d{17,19})");
private static readonly Regex _loggerBRegex = new("MessageID:(\\d{17,19})");
private static readonly Regex _auttajaRegex = new("Message (\\d{17,19}) deleted");
private static readonly Regex _mantaroRegex = new("Message \\(?ID:? (\\d{17,19})\\)? created by .* in channel .* was deleted\\.");
private static readonly Regex _pancakeRegex = new("Message from <@(\\d{17,19})> deleted in");
private static readonly Regex _unbelievaboatRegex = new("Message ID: (\\d{17,19})");
private static readonly Regex _vanessaRegex = new("Message sent by <@!?(\\d{17,19})> deleted in");
private static readonly Regex _salRegex = new("\\(ID: (\\d{17,19})\\)");
private static readonly Regex _GearBotRegex = new("\\(``(\\d{17,19})``\\) in <#\\d{17,19}> has been removed.");
private static readonly Regex _GiselleRegex = new("\\*\\*Message ID\\*\\*: `(\\d{17,19})`");
private static readonly Regex _VortexRegex = new("`\\[(\\d\\d:\\d\\d:\\d\\d)\\]` .* \\(ID:(\\d{17,19})\\).* <#\\d{17,19}>:");
new LoggerBot("Carl-bot", 23514896210395136, fuzzyExtractFunc: ExtractCarlBot,
webhookName: "Carl-bot Logging"),
new LoggerBot("Circle", 497196352866877441, fuzzyExtractFunc: ExtractCircle),
new LoggerBot("Pancake", 239631525350604801, fuzzyExtractFunc: ExtractPancake),
private static readonly Dictionary<ulong, LoggerBot> _bots = new[]
// 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("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("SafetyAtLast", 401549924199694338, fuzzyExtractFunc: ExtractSAL),
new LoggerBot("GearBot", 349977940198555660, fuzzyExtractFunc: ExtractGearBot),
new LoggerBot("GiselleBot", 356831787445387285, ExtractGiselleBot),
new LoggerBot("Vortex", 240254129333731328, fuzzyExtractFunc: ExtractVortex)
}.ToDictionary(b => b.Id);
private static readonly Dictionary<string, LoggerBot> _botsByWebhookName = _bots.Values
.Where(b => b.WebhookName != null)
.ToDictionary(b => b.WebhookName);
private readonly Bot _bot; // todo: get rid of this nasty
private readonly IDiscordCache _cache;
private readonly DiscordApiClient _client;
private readonly IDatabase _db;
private readonly ILogger _logger;
public LoggerCleanService(IDatabase db, DiscordApiClient client, IDiscordCache cache, Bot bot, ILogger logger)
{
_db = db;
_client = client;
_cache = cache;
_bot = bot;
_logger = logger.ForContext<LoggerCleanService>();
}
public ICollection<LoggerBot> Bots => _bots.Values;
public async ValueTask HandleLoggerBotCleanup(Message msg)
{
var channel = await _cache.GetChannel(msg.ChannelId);
if (channel.Type != Channel.ChannelType.GuildText) return;
if (!(await _cache.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;
try
{
new LoggerBot("Carl-bot", 23514896210395136, fuzzyExtractFunc: ExtractCarlBot, webhookName: "Carl-bot Logging"),
new LoggerBot("Circle", 497196352866877441, fuzzyExtractFunc: ExtractCircle),
new LoggerBot("Pancake", 239631525350604801, fuzzyExtractFunc: ExtractPancake),
// 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("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("SafetyAtLast", 401549924199694338, fuzzyExtractFunc: ExtractSAL),
new LoggerBot("GearBot", 349977940198555660, fuzzyExtractFunc: ExtractGearBot),
new LoggerBot("GiselleBot", 356831787445387285, ExtractGiselleBot),
new LoggerBot("Vortex", 240254129333731328, fuzzyExtractFunc: ExtractVortex),
}.ToDictionary(b => b.Id);
private static readonly Dictionary<string, LoggerBot> _botsByWebhookName = _bots.Values
.Where(b => b.WebhookName != null)
.ToDictionary(b => b.WebhookName);
private readonly IDatabase _db;
private readonly DiscordApiClient _client;
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;
_client = client;
_cache = cache;
_bot = bot;
_logger = logger.ForContext<LoggerCleanService>();
}
public ICollection<LoggerBot> Bots => _bots.Values;
public async ValueTask HandleLoggerBotCleanup(Message msg)
{
var channel = await _cache.GetChannel(msg.ChannelId);
if (channel.Type != Channel.ChannelType.GuildText) return;
if (!(await _cache.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;
try
// We try two ways of extracting the actual message, depending on the bots
if (bot.FuzzyExtractFunc != null)
{
// We try two ways of extracting the actual message, depending on the bots
if (bot.FuzzyExtractFunc != null)
{
// Some bots (Carl, Circle, etc) only give us a user ID and a rough timestamp, so we try our best to
// "cross-reference" those with the message DB. We know the deletion event happens *after* the message
// was sent, so we're checking for any messages sent in the same guild within 3 seconds before the
// delete event timestamp, which is... good enough, I think? Potential for false positives and negatives
// 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;
// Some bots (Carl, Circle, etc) only give us a user ID and a rough timestamp, so we try our best to
// "cross-reference" those with the message DB. We know the deletion event happens *after* the message
// was sent, so we're checking for any messages sent in the same guild within 3 seconds before the
// delete event timestamp, which is... good enough, I think? Potential for false positives and negatives
// 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);
_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",
new
{
fuzzy.Value.User,
Guild = msg.GuildId,
ApproxId = DiscordUtils.InstantToSnowflake(
fuzzy.Value.ApproxTimestamp - Duration.FromSeconds(3))
}));
var mid = await _db.Execute(conn =>
conn.QuerySingleOrDefaultAsync<ulong?>(
"select mid from messages where sender = @User and mid > @ApproxID and guild = @Guild limit 1",
new
{
fuzzy.Value.User,
Guild = msg.GuildId,
ApproxId = DiscordUtils.InstantToSnowflake(
fuzzy.Value.ApproxTimestamp - Duration.FromSeconds(3))
}));
// If we didn't find a corresponding message, bail
if (mid == null)
return;
// If we didn't find a corresponding message, bail
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);
}
else if (bot.ExtractFunc != null)
{
// 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 }));
if (mid == null) return;
// If we've gotten this far, we found a logged deletion of a trigger message. Just yeet it!
await _client.DeleteMessage(msg.ChannelId, msg.Id);
} // else should not happen, but idk, it might
// Otherwise, we can *reasonably assume* that this is a logged deletion, so delete the log message.
await _client.DeleteMessage(msg.ChannelId, msg.Id);
}
catch (NotFoundException)
else if (bot.ExtractFunc != null)
{
// Sort of a temporary measure: getting an error in Sentry about a NotFoundException from D#+ here
// The only thing I can think of that'd cause this are the DeleteAsync() calls which 404 when
// the message doesn't exist anyway - so should be safe to just ignore it, right?
}
// 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 }));
if (mid == null) return;
// If we've gotten this far, we found a logged deletion of a trigger message. Just yeet it!
await _client.DeleteMessage(msg.ChannelId, msg.Id);
} // else should not happen, but idk, it might
}
private static ulong? ExtractAuttaja(Message msg)
catch (NotFoundException)
{
// Auttaja has an optional "compact mode" that logs without embeds
// That one puts the ID in the message content, non-compact puts it in the embed description.
// 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;
}
private static ulong? ExtractDyno(Message msg)
{
// Embed *description* contains "Message sent by [mention] deleted in [channel]", contains message ID in footer per regex
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;
}
private static ulong? ExtractLoggerA(Message msg)
{
// This is for Logger#6088 (298822483060981760), distinct from Logger#6278 (327424261180620801).
// Embed contains title "Message deleted in [channel]", and an ID field containing both message and user ID (see regex).
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;
}
private static ulong? ExtractLoggerB(Message msg)
{
// This is for Logger#6278 (327424261180620801), distinct from Logger#6088 (298822483060981760).
// Embed title ends with "A Message Was Deleted!", footer contains message ID as per regex.
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;
}
private static ulong? ExtractGenericBot(Message msg)
{
// Embed, title is "Message Deleted", ID plain in footer.
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;
}
private static ulong? ExtractBlargBot(Message msg)
{
// Embed, title ends with "Message Deleted", contains ID plain in a field.
var embed = msg.Embeds?.FirstOrDefault();
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;
}
private static ulong? ExtractMantaro(Message msg)
{
// 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;
}
private static FuzzyExtractResult? ExtractCarlBot(Message msg)
{
// Embed, title is "Message deleted in [channel], **user** ID in the footer, timestamp as, well, timestamp in embed.
// This is the *deletion* timestamp, which we can assume is a couple seconds at most after the message was originally sent
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
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = OffsetDateTimePattern.Rfc3339.Parse(embed.Timestamp).GetValueOrThrow().ToInstant()
}
: (FuzzyExtractResult?)null;
}
private static FuzzyExtractResult? ExtractCircle(Message msg)
{
// Like Auttaja, Circle has both embed and compact modes, but the regex works for both.
// Compact: "Message from [user] ([id]) deleted in [channel]", no timestamp (use message time)
// Embed: Message Author field: "[user] ([id])", then an embed timestamp
string stringWithId = msg.Content;
if (msg.Embeds?.Length > 0)
{
var embed = msg.Embeds?.First();
if (embed.Author?.Name == null || !embed.Author.Name.StartsWith("Message Deleted in")) return null;
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
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: (FuzzyExtractResult?)null;
}
private static FuzzyExtractResult? ExtractPancake(Message msg)
{
// Embed, author is "Message Deleted", description includes a mention, timestamp is *message send time* (but no ID)
// so we use the message timestamp to get somewhere *after* the message was proxied
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Description == null || embed.Author?.Name != "Message Deleted") return null;
var match = _pancakeRegex.Match(embed.Description);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: (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;
}
private static FuzzyExtractResult? ExtractVanessa(Message msg)
{
// Title is "Message Deleted", embed description contains mention
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Title == null || embed.Title != "Message Deleted" || embed.Description == null) return null;
var match = _vanessaRegex.Match(embed.Description);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: (FuzzyExtractResult?)null;
}
private static FuzzyExtractResult? ExtractSAL(Message msg)
{
// Title is "Message Deleted!", field "Message Author" contains ID
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Title == null || embed.Title != "Message Deleted!") return null;
var authorField = embed.Fields.FirstOrDefault(f => f.Name == "Message Author");
if (authorField == null) return null;
var match = _salRegex.Match(authorField.Value);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: (FuzzyExtractResult?)null;
}
private static FuzzyExtractResult? ExtractGearBot(Message msg)
{
// Simple text based message log.
// No message ID, but we have timestamp and author ID.
// Not using timestamp here though (seems to be same as message timestamp), might be worth implementing in the future.
var match = _GearBotRegex.Match(msg.Content);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: (FuzzyExtractResult?)null;
}
private static ulong? ExtractGiselleBot(Message msg)
{
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;
}
private static FuzzyExtractResult? ExtractVortex(Message msg)
{
// timestamp is HH:MM:SS
// however, that can be set to the user's timezone, so we just use the message timestamp
var match = _VortexRegex.Match(msg.Content);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[2].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
public class LoggerBot
{
public string Name;
public ulong Id;
public Func<Message, ulong?> ExtractFunc;
public Func<Message, FuzzyExtractResult?> FuzzyExtractFunc;
public string WebhookName;
public LoggerBot(string name, ulong id, Func<Message, ulong?> extractFunc = null, Func<Message, FuzzyExtractResult?> fuzzyExtractFunc = null, string webhookName = null)
{
Name = name;
Id = id;
FuzzyExtractFunc = fuzzyExtractFunc;
ExtractFunc = extractFunc;
WebhookName = webhookName;
}
}
public struct FuzzyExtractResult
{
public ulong User { get; set; }
public Instant ApproxTimestamp { get; set; }
// Sort of a temporary measure: getting an error in Sentry about a NotFoundException from D#+ here
// The only thing I can think of that'd cause this are the DeleteAsync() calls which 404 when
// the message doesn't exist anyway - so should be safe to just ignore it, right?
}
}
private static ulong? ExtractAuttaja(Message msg)
{
// Auttaja has an optional "compact mode" that logs without embeds
// That one puts the ID in the message content, non-compact puts it in the embed description.
// 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) : null;
}
private static ulong? ExtractDyno(Message msg)
{
// Embed *description* contains "Message sent by [mention] deleted in [channel]", contains message ID in footer per regex
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) : null;
}
private static ulong? ExtractLoggerA(Message msg)
{
// This is for Logger#6088 (298822483060981760), distinct from Logger#6278 (327424261180620801).
// Embed contains title "Message deleted in [channel]", and an ID field containing both message and user ID (see regex).
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) : null;
}
private static ulong? ExtractLoggerB(Message msg)
{
// This is for Logger#6278 (327424261180620801), distinct from Logger#6088 (298822483060981760).
// Embed title ends with "A Message Was Deleted!", footer contains message ID as per regex.
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) : null;
}
private static ulong? ExtractGenericBot(Message msg)
{
// Embed, title is "Message Deleted", ID plain in footer.
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) : null;
}
private static ulong? ExtractBlargBot(Message msg)
{
// Embed, title ends with "Message Deleted", contains ID plain in a field.
var embed = msg.Embeds?.FirstOrDefault();
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) : null;
}
private static ulong? ExtractMantaro(Message msg)
{
// 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) : null;
}
private static FuzzyExtractResult? ExtractCarlBot(Message msg)
{
// Embed, title is "Message deleted in [channel], **user** ID in the footer, timestamp as, well, timestamp in embed.
// This is the *deletion* timestamp, which we can assume is a couple seconds at most after the message was originally sent
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
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = OffsetDateTimePattern.Rfc3339.Parse(embed.Timestamp).GetValueOrThrow()
.ToInstant()
}
: null;
}
private static FuzzyExtractResult? ExtractCircle(Message msg)
{
// Like Auttaja, Circle has both embed and compact modes, but the regex works for both.
// Compact: "Message from [user] ([id]) deleted in [channel]", no timestamp (use message time)
// Embed: Message Author field: "[user] ([id])", then an embed timestamp
var stringWithId = msg.Content;
if (msg.Embeds?.Length > 0)
{
var embed = msg.Embeds?.First();
if (embed.Author?.Name == null || !embed.Author.Name.StartsWith("Message Deleted in")) return null;
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
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
private static FuzzyExtractResult? ExtractPancake(Message msg)
{
// Embed, author is "Message Deleted", description includes a mention, timestamp is *message send time* (but no ID)
// so we use the message timestamp to get somewhere *after* the message was proxied
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Description == null || embed.Author?.Name != "Message Deleted") return null;
var match = _pancakeRegex.Match(embed.Description);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: 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) : null;
}
private static FuzzyExtractResult? ExtractVanessa(Message msg)
{
// Title is "Message Deleted", embed description contains mention
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Title == null || embed.Title != "Message Deleted" || embed.Description == null) return null;
var match = _vanessaRegex.Match(embed.Description);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
private static FuzzyExtractResult? ExtractSAL(Message msg)
{
// Title is "Message Deleted!", field "Message Author" contains ID
var embed = msg.Embeds?.FirstOrDefault();
if (embed?.Title == null || embed.Title != "Message Deleted!") return null;
var authorField = embed.Fields.FirstOrDefault(f => f.Name == "Message Author");
if (authorField == null) return null;
var match = _salRegex.Match(authorField.Value);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
private static FuzzyExtractResult? ExtractGearBot(Message msg)
{
// Simple text based message log.
// No message ID, but we have timestamp and author ID.
// Not using timestamp here though (seems to be same as message timestamp), might be worth implementing in the future.
var match = _GearBotRegex.Match(msg.Content);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[1].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
private static ulong? ExtractGiselleBot(Message msg)
{
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) : null;
}
private static FuzzyExtractResult? ExtractVortex(Message msg)
{
// timestamp is HH:MM:SS
// however, that can be set to the user's timezone, so we just use the message timestamp
var match = _VortexRegex.Match(msg.Content);
return match.Success
? new FuzzyExtractResult
{
User = ulong.Parse(match.Groups[2].Value),
ApproxTimestamp = msg.Timestamp().ToInstant()
}
: null;
}
public class LoggerBot
{
public ulong Id;
public string Name;
public string WebhookName;
public Func<Message, ulong?> ExtractFunc;
public Func<Message, FuzzyExtractResult?> FuzzyExtractFunc;
public LoggerBot(string name, ulong id, Func<Message, ulong?> extractFunc = null,
Func<Message, FuzzyExtractResult?> fuzzyExtractFunc = null, string webhookName = null)
{
Name = name;
Id = id;
FuzzyExtractFunc = fuzzyExtractFunc;
ExtractFunc = extractFunc;
WebhookName = webhookName;
}
}
public struct FuzzyExtractResult
{
public ulong User { get; set; }
public Instant ApproxTimestamp { get; set; }
}
}

View File

@@ -1,95 +1,94 @@
using System.Diagnostics;
using System.Threading.Tasks;
using App.Metrics;
using Dapper;
using Myriad.Cache;
using Myriad.Types;
using NodaTime.Extensions;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
using Stopwatch = System.Diagnostics.Stopwatch;
namespace PluralKit.Bot;
public class PeriodicStatCollector
{
public class PeriodicStatCollector
private readonly IDiscordCache _cache;
private readonly DbConnectionCountHolder _countHolder;
private readonly CpuStatService _cpu;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly ModelRepository _repo;
private readonly WebhookCacheService _webhookCache;
public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache,
DbConnectionCountHolder countHolder, CpuStatService cpu, ModelRepository repo,
IDiscordCache cache)
{
private readonly IMetrics _metrics;
private readonly IDiscordCache _cache;
private readonly CpuStatService _cpu;
_metrics = metrics;
_webhookCache = webhookCache;
_countHolder = countHolder;
_cpu = cpu;
_repo = repo;
_cache = cache;
_logger = logger.ForContext<PeriodicStatCollector>();
}
private readonly ModelRepository _repo;
public async Task CollectStats()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
private readonly WebhookCacheService _webhookCache;
// Aggregate guild/channel stats
var guildCount = 0;
var channelCount = 0;
private readonly DbConnectionCountHolder _countHolder;
private readonly ILogger _logger;
public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, ModelRepository repo, IDiscordCache cache)
// No LINQ today, sorry
await foreach (var guild in _cache.GetAllGuilds())
{
_metrics = metrics;
_webhookCache = webhookCache;
_countHolder = countHolder;
_cpu = cpu;
_repo = repo;
_cache = cache;
_logger = logger.ForContext<PeriodicStatCollector>();
guildCount++;
foreach (var channel in await _cache.GetGuildChannels(guild.Id))
if (DiscordUtils.IsValidGuildChannel(channel))
channelCount++;
}
public async Task CollectStats()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
_metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, guildCount);
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount);
// Aggregate guild/channel stats
var guildCount = 0;
var channelCount = 0;
// Aggregate DB stats
// just fetching from database here - actual updating of the data is done in PluralKit.ScheduledTasks
// if you're not running ScheduledTasks and want up-to-date counts, uncomment the following line:
// await _repo.UpdateStats();
var counts = await _repo.GetStats();
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.SystemCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.MemberCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.GroupCount, counts.GroupCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.SwitchCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.MessageCount);
// No LINQ today, sorry
await foreach (var guild in _cache.GetAllGuilds())
{
guildCount++;
foreach (var channel in await _cache.GetGuildChannels(guild.Id))
{
if (DiscordUtils.IsValidGuildChannel(channel))
channelCount++;
}
}
// Process info
var process = Process.GetCurrentProcess();
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPhysicalMemory, process.WorkingSet64);
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessVirtualMemory, process.VirtualMemorySize64);
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPrivateMemory, process.PrivateMemorySize64);
_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());
_metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, guildCount);
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount);
// Database info
_metrics.Measure.Gauge.SetValue(CoreMetrics.DatabaseConnections, _countHolder.ConnectionCount);
// Aggregate DB stats
// just fetching from database here - actual updating of the data is done in PluralKit.ScheduledTasks
// if you're not running ScheduledTasks and want up-to-date counts, uncomment the following line:
// await _repo.UpdateStats();
var counts = await _repo.GetStats();
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.SystemCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.MemberCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.GroupCount, counts.GroupCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.SwitchCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.MessageCount);
// Other shiz
_metrics.Measure.Gauge.SetValue(BotMetrics.WebhookCacheSize, _webhookCache.CacheSize);
// Process info
var process = Process.GetCurrentProcess();
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPhysicalMemory, process.WorkingSet64);
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessVirtualMemory, process.VirtualMemorySize64);
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPrivateMemory, process.PrivateMemorySize64);
_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);
stopwatch.Stop();
_logger.Debug("Updated metrics in {Time}", stopwatch.ElapsedDuration());
}
stopwatch.Stop();
_logger.Debug("Updated metrics in {Time}", stopwatch.ElapsedDuration());
}
}

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;
using App.Metrics;
@@ -15,138 +11,142 @@ using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
// TODO: how much of this do we need now that we have logging in the shard library?
// A lot could probably be cleaned up...
public class ShardInfoService
{
// TODO: how much of this do we need now that we have logging in the shard library?
// A lot could probably be cleaned up...
public class ShardInfoService
private readonly Cluster _client;
private readonly IDatabase _db;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly ModelRepository _repo;
private readonly Dictionary<int, ShardInfo> _shardInfo = new();
public ShardInfoService(ILogger logger, Cluster client, IMetrics metrics, IDatabase db, ModelRepository repo)
{
public class ShardInfo
_client = client;
_metrics = metrics;
_db = db;
_repo = repo;
_logger = logger.ForContext<ShardInfoService>();
}
public ICollection<ShardInfo> Shards => _shardInfo.Values;
public void Init()
{
// We initialize this before any shards are actually created and connected
// This means the client won't know the shard count, so we attach a listener every time a shard gets connected
_client.ShardCreated += InitializeShard;
}
private void ReportShardStatus()
{
foreach (var (id, shard) in _shardInfo)
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardLatency, new MetricTags("shard", id.ToString()),
shard.ShardLatency.TotalMilliseconds);
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardsConnected, _shardInfo.Count(s => s.Value.Connected));
}
private void InitializeShard(Shard shard)
{
// Get or insert info in the client dict
if (_shardInfo.TryGetValue(shard.ShardId, out var info))
{
public bool HasAttachedListeners;
public Instant LastConnectionTime;
public Instant LastHeartbeatTime;
public int DisconnectionCount;
public Duration ShardLatency;
public bool Connected;
// Skip adding listeners if we've seen this shard & already added listeners to it
if (info.HasAttachedListeners)
return;
}
else
{
_shardInfo[shard.ShardId] = info = new ShardInfo();
}
private readonly IMetrics _metrics;
private readonly ILogger _logger;
private readonly Cluster _client;
private readonly Dictionary<int, ShardInfo> _shardInfo = new();
// Call our own SocketOpened listener manually (and then attach the listener properly)
private readonly IDatabase _db;
private readonly ModelRepository _repo;
// 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);
public ShardInfoService(ILogger logger, Cluster client, IMetrics metrics, IDatabase db, ModelRepository repo)
// Register that we've seen it
info.HasAttachedListeners = true;
}
private ShardInfo TryGetShard(Shard shard)
{
// If we haven't seen this shard before, add it to the dict!
// I don't think this will ever occur since the shard number is constant up-front and we handle those
// in the RefreshShardList handler above but you never know, I guess~
if (!_shardInfo.TryGetValue(shard.ShardId, out var info))
_shardInfo[shard.ShardId] = info = new ShardInfo();
return info;
}
private void ReadyOrResumed(Shard shard)
{
var info = TryGetShard(shard);
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
ReportShardStatus();
_ = ExecuteWithDatabase(async c =>
{
_client = client;
_metrics = metrics;
_db = db;
_repo = repo;
_logger = logger.ForContext<ShardInfoService>();
}
await _repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Up);
await _repo.RegisterShardConnection(c, shard.ShardId);
});
}
public void Init()
private void SocketClosed(Shard shard, WebSocketCloseStatus? closeStatus, string message)
{
var info = TryGetShard(shard);
info.DisconnectionCount++;
info.Connected = false;
ReportShardStatus();
_ = ExecuteWithDatabase(c =>
_repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Down));
}
private void Heartbeated(Shard shard, TimeSpan latency)
{
var info = TryGetShard(shard);
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
info.ShardLatency = latency.ToDuration();
_ = ExecuteWithDatabase(c =>
_repo.RegisterShardHeartbeat(c, shard.ShardId, latency.ToDuration()));
}
private async Task ExecuteWithDatabase(Func<IPKConnection, Task> fn)
{
// wrapper function to log errors because we "async void" it at call site :(
try
{
// We initialize this before any shards are actually created and connected
// This means the client won't know the shard count, so we attach a listener every time a shard gets connected
_client.ShardCreated += InitializeShard;
await using var conn = await _db.Obtain();
await fn(conn);
}
private void ReportShardStatus()
catch (Exception e)
{
foreach (var (id, shard) in _shardInfo)
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardLatency, new MetricTags("shard", id.ToString()), shard.ShardLatency.TotalMilliseconds);
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardsConnected, _shardInfo.Count(s => s.Value.Connected));
_logger.Error(e, "Error persisting shard status");
}
}
private void InitializeShard(Shard shard)
{
// Get or insert info in the client dict
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)
return;
}
else _shardInfo[shard.ShardId] = info = new ShardInfo();
public ShardInfo GetShardInfo(Shard shard) => _shardInfo[shard.ShardId];
// 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;
}
private ShardInfo TryGetShard(Shard shard)
{
// If we haven't seen this shard before, add it to the dict!
// I don't think this will ever occur since the shard number is constant up-front and we handle those
// in the RefreshShardList handler above but you never know, I guess~
if (!_shardInfo.TryGetValue(shard.ShardId, out var info))
_shardInfo[shard.ShardId] = info = new ShardInfo();
return info;
}
private void ReadyOrResumed(Shard shard)
{
var info = TryGetShard(shard);
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
ReportShardStatus();
_ = ExecuteWithDatabase(async c =>
{
await _repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Up);
await _repo.RegisterShardConnection(c, shard.ShardId);
});
}
private void SocketClosed(Shard shard, WebSocketCloseStatus? closeStatus, string message)
{
var info = TryGetShard(shard);
info.DisconnectionCount++;
info.Connected = false;
ReportShardStatus();
_ = ExecuteWithDatabase(c =>
_repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Down));
}
private void Heartbeated(Shard shard, TimeSpan latency)
{
var info = TryGetShard(shard);
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
info.ShardLatency = latency.ToDuration();
_ = ExecuteWithDatabase(c =>
_repo.RegisterShardHeartbeat(c, shard.ShardId, latency.ToDuration()));
}
private async Task ExecuteWithDatabase(Func<IPKConnection, Task> fn)
{
// wrapper function to log errors because we "async void" it at call site :(
try
{
await using var conn = await _db.Obtain();
await fn(conn);
}
catch (Exception e)
{
_logger.Error(e, "Error persisting shard status");
}
}
public ShardInfo GetShardInfo(Shard shard) => _shardInfo[shard.ShardId];
public ICollection<ShardInfo> Shards => _shardInfo.Values;
public class ShardInfo
{
public bool Connected;
public int DisconnectionCount;
public bool HasAttachedListeners;
public Instant LastConnectionTime;
public Instant LastHeartbeatTime;
public Duration ShardLatency;
}
}

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using App.Metrics;
@@ -13,116 +9,118 @@ using Myriad.Types;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class WebhookCacheService
{
public class WebhookCacheService
public static readonly string WebhookName = "PluralKit Proxy Webhook";
private readonly IDiscordCache _cache;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly DiscordApiClient _rest;
private readonly ConcurrentDictionary<ulong, Lazy<Task<Webhook>>> _webhooks;
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache)
{
public static readonly string WebhookName = "PluralKit Proxy Webhook";
private readonly DiscordApiClient _rest;
private readonly ConcurrentDictionary<ulong, Lazy<Task<Webhook>>> _webhooks;
private readonly IMetrics _metrics;
private readonly ILogger _logger;
private readonly IDiscordCache _cache;
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache)
{
_metrics = metrics;
_rest = rest;
_cache = cache;
_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
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
// webhook from being created twice.
Lazy<Task<Webhook>> GetWebhookTaskInner()
{
Task<Webhook> Factory() => GetOrCreateWebhook(channelId);
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 _);
lazyWebhookValue = GetWebhookTaskInner();
}
// It's possible to "move" a webhook to a different channel after creation
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
var webhook = await lazyWebhookValue.Value;
if (webhook.ChannelId != channelId && webhook.ChannelId != 0)
return await InvalidateAndRefreshWebhook(channelId, webhook);
return webhook;
}
public async Task<Webhook> InvalidateAndRefreshWebhook(ulong channelId, Webhook webhook)
{
// 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);
}
private async Task<Webhook?> GetOrCreateWebhook(ulong channelId)
{
_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);
// If the channel has a webhook created by PK, just return that one
var ourUserId = await _cache.GetOwnUser();
var ourWebhook = webhooks.FirstOrDefault(hook => IsWebhookMine(ourUserId, hook));
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);
}
private async Task<Webhook[]> FetchChannelWebhooks(ulong channelId)
{
try
{
return await _rest.GetChannelWebhooks(channelId);
}
catch (HttpRequestException e)
{
_logger.Warning(e, "Error occurred while fetching webhook list");
// This happens sometimes when Discord returns a malformed request for the webhook list
// Nothing we can do than just assume that none exist.
return new Webhook[0];
}
}
private async Task<Webhook> DoCreateWebhook(ulong channelId)
{
_logger.Information("Creating new webhook for channel {Channel}", channelId);
return await _rest.CreateWebhook(channelId, new CreateWebhookRequest(WebhookName));
}
private bool IsWebhookMine(ulong userId, Webhook arg) => arg.User?.Id == userId && arg.Name == WebhookName;
public int CacheSize => _webhooks.Count;
_metrics = metrics;
_rest = rest;
_cache = cache;
_logger = logger.ForContext<WebhookCacheService>();
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<Webhook>>>();
}
public int CacheSize => _webhooks.Count;
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
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
// webhook from being created twice.
Lazy<Task<Webhook>> GetWebhookTaskInner()
{
Task<Webhook> Factory() => GetOrCreateWebhook(channelId);
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 _);
lazyWebhookValue = GetWebhookTaskInner();
}
// It's possible to "move" a webhook to a different channel after creation
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
var webhook = await lazyWebhookValue.Value;
if (webhook.ChannelId != channelId && webhook.ChannelId != 0)
return await InvalidateAndRefreshWebhook(channelId, webhook);
return webhook;
}
public async Task<Webhook> InvalidateAndRefreshWebhook(ulong channelId, Webhook webhook)
{
// 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);
}
private async Task<Webhook?> GetOrCreateWebhook(ulong channelId)
{
_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);
// If the channel has a webhook created by PK, just return that one
var ourUserId = await _cache.GetOwnUser();
var ourWebhook = webhooks.FirstOrDefault(hook => IsWebhookMine(ourUserId, hook));
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);
}
private async Task<Webhook[]> FetchChannelWebhooks(ulong channelId)
{
try
{
return await _rest.GetChannelWebhooks(channelId);
}
catch (HttpRequestException e)
{
_logger.Warning(e, "Error occurred while fetching webhook list");
// This happens sometimes when Discord returns a malformed request for the webhook list
// Nothing we can do than just assume that none exist.
return new Webhook[0];
}
}
private async Task<Webhook> DoCreateWebhook(ulong channelId)
{
_logger.Information("Creating new webhook for channel {Channel}", channelId);
return await _rest.CreateWebhook(channelId, new CreateWebhookRequest(WebhookName));
}
private bool IsWebhookMine(ulong userId, Webhook arg) => arg.User?.Id == userId && arg.Name == WebhookName;
}

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using App.Metrics;
using Humanizer;
@@ -11,6 +7,7 @@ using Humanizer;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Rest;
using Myriad.Rest.Exceptions;
using Myriad.Rest.Types;
using Myriad.Rest.Types.Requests;
using Myriad.Types;
@@ -19,237 +16,242 @@ using Newtonsoft.Json;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class WebhookExecutionErrorOnDiscordsEnd: Exception { }
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd
{
public class WebhookExecutionErrorOnDiscordsEnd: Exception
// 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
}
public record ProxyRequest
{
public ulong GuildId { get; init; }
public ulong ChannelId { get; init; }
public ulong? ThreadId { get; init; }
public string Name { get; init; }
public string? AvatarUrl { get; init; }
public string? Content { get; init; }
public Message.Attachment[] Attachments { get; init; }
public int FileSizeLimit { get; init; }
public Embed[] Embeds { get; init; }
public bool AllowEveryone { get; init; }
}
public class WebhookExecutorService
{
private readonly IDiscordCache _cache;
private readonly HttpClient _client;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly DiscordApiClient _rest;
private readonly WebhookCacheService _webhookCache;
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger,
HttpClient client, IDiscordCache cache, DiscordApiClient rest)
{
_metrics = metrics;
_webhookCache = webhookCache;
_client = client;
_cache = cache;
_rest = rest;
_logger = logger.ForContext<WebhookExecutorService>();
}
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd
public async Task<Message> ExecuteWebhook(ProxyRequest req)
{
// 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
_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 record ProxyRequest
public async Task<Message> EditWebhookMessage(ulong channelId, ulong messageId, string newContent)
{
public ulong GuildId { get; init; }
public ulong ChannelId { get; init; }
public ulong? ThreadId { get; init; }
public string Name { get; init; }
public string? AvatarUrl { get; init; }
public string? Content { get; init; }
public Message.Attachment[] Attachments { get; init; }
public int FileSizeLimit { get; init; }
public Embed[] Embeds { get; init; }
public bool AllowEveryone { get; init; }
var allowedMentions = newContent.ParseMentions() with
{
Roles = Array.Empty<ulong>(),
Parse = Array.Empty<AllowedMentions.ParseType>()
};
ulong? threadId = null;
var root = await _cache.GetRootChannel(channelId);
if (root.Id != channelId)
threadId = channelId;
var webhook = await _webhookCache.GetWebhook(root.Id);
return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId,
new WebhookMessageEditRequest { Content = newContent, AllowedMentions = allowedMentions },
threadId);
}
public class WebhookExecutorService
private async Task<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)
{
private readonly IDiscordCache _cache;
private readonly WebhookCacheService _webhookCache;
private readonly DiscordApiClient _rest;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly HttpClient _client;
var guild = await _cache.GetGuild(req.GuildId);
var content = req.Content.Truncate(2000);
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger, HttpClient client, IDiscordCache cache, DiscordApiClient rest)
{
_metrics = metrics;
_webhookCache = webhookCache;
_client = client;
_cache = cache;
_rest = rest;
_logger = logger.ForContext<WebhookExecutorService>();
}
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 allowedMentions = newContent.ParseMentions() with
var allowedMentions = content.ParseMentions();
if (!req.AllowEveryone)
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with
{
Roles = Array.Empty<ulong>(),
// also clear @everyones
Parse = Array.Empty<AllowedMentions.ParseType>()
};
ulong? threadId = null;
var root = await _cache.GetRootChannel(channelId);
if (root.Id != channelId)
threadId = channelId;
var webhookReq = new ExecuteWebhookRequest
{
Username = FixProxyName(req.Name).Truncate(80),
Content = content,
AllowedMentions = allowedMentions,
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
Embeds = req.Embeds
};
var webhook = await _webhookCache.GetWebhook(root.Id);
return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId,
new WebhookMessageEditRequest { Content = newContent, AllowedMentions = allowedMentions },
threadId);
MultipartFile[] files = null;
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit);
if (attachmentChunks.Count > 0)
{
_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]);
webhookReq.Attachments = files.Select(f => new Message.Attachment
{
Id = (ulong)Array.IndexOf(files, f),
Description = f.Description,
Filename = f.Filename
}).ToArray();
}
private async Task<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)
Message webhookMessage;
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime))
{
var guild = await _cache.GetGuild(req.GuildId);
var content = req.Content.Truncate(2000);
var allowedMentions = content.ParseMentions();
if (!req.AllowEveryone)
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with
try
{
webhookMessage =
await _rest.ExecuteWebhook(webhook.Id, webhook.Token, webhookReq, files, req.ThreadId);
}
catch (JsonReaderException)
{
// This happens sometimes when we hit a CloudFlare error (or similar) on Discord's end
// Nothing we can do about this - happens sometimes under server load, so just drop the message and give up
throw new WebhookExecutionErrorOnDiscordsEnd();
}
catch (NotFoundException e)
{
if (e.ErrorCode == 10015 && !hasRetried)
{
// also clear @everyones
Parse = Array.Empty<AllowedMentions.ParseType>()
};
// Error 10015 = "Unknown Webhook" - this likely means the webhook was deleted
// 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 webhookReq = new ExecuteWebhookRequest
{
Username = FixProxyName(req.Name).Truncate(80),
Content = content,
AllowedMentions = allowedMentions,
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
Embeds = req.Embeds
};
var newWebhook = await _webhookCache.InvalidateAndRefreshWebhook(req.ChannelId, webhook);
return await ExecuteWebhookInner(newWebhook, req, true);
}
MultipartFile[] files = null;
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit);
if (attachmentChunks.Count > 0)
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);
return webhookMessage;
}
private async Task TrySendRemainingAttachments(Webhook webhook, string name, string avatarUrl,
IReadOnlyList<IReadOnlyCollection<Message.Attachment>>
attachmentChunks, ulong? threadId)
{
if (attachmentChunks.Count <= 1) return;
for (var i = 1; i < attachmentChunks.Count; i++)
{
var files = await GetAttachmentFiles(attachmentChunks[i]);
var req = new ExecuteWebhookRequest
{
_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]);
webhookReq.Attachments = files.Select(f => new Message.Attachment()
Username = name,
AvatarUrl = avatarUrl,
Attachments = files.Select(f => new Message.Attachment
{
Id = (ulong)Array.IndexOf(files, f),
Description = f.Description,
Filename = f.Filename
}).ToArray();
}
Message webhookMessage;
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime))
{
try
{
webhookMessage = await _rest.ExecuteWebhook(webhook.Id, webhook.Token, webhookReq, files, req.ThreadId);
}
catch (JsonReaderException)
{
// This happens sometimes when we hit a CloudFlare error (or similar) on Discord's end
// Nothing we can do about this - happens sometimes under server load, so just drop the message and give up
throw new WebhookExecutionErrorOnDiscordsEnd();
}
catch (Myriad.Rest.Exceptions.NotFoundException e)
{
if (e.ErrorCode == 10015 && !hasRetried)
{
// Error 10015 = "Unknown Webhook" - this likely means the webhook was deleted
// 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);
return webhookMessage;
}
private async Task TrySendRemainingAttachments(Webhook webhook, string name, string avatarUrl, IReadOnlyList<IReadOnlyCollection<Message.Attachment>> attachmentChunks, ulong? threadId)
{
if (attachmentChunks.Count <= 1) return;
for (var i = 1; i < attachmentChunks.Count; i++)
{
var files = await GetAttachmentFiles(attachmentChunks[i]);
var req = new ExecuteWebhookRequest
{
Username = name,
AvatarUrl = avatarUrl,
Attachments = files.Select(f => new Message.Attachment()
{
Id = (ulong)Array.IndexOf(files, f),
Description = f.Description,
Filename = f.Filename,
}).ToArray()
};
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)
{
var attachmentResponse = await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead);
return new(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync(), attachment.Description);
}
return await Task.WhenAll(attachments.Select(GetStream));
}
private IReadOnlyList<IReadOnlyCollection<Message.Attachment>> ChunkAttachmentsOrThrow(
IReadOnlyList<Message.Attachment> attachments, int sizeThreshold)
{
// Splits a list of attachments into "chunks" of at most 8MB each
// 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>();
// sizeThreshold is in MB (user-readable)
var bytesThreshold = sizeThreshold * 1024 * 1024;
foreach (var attachment in attachments)
{
if (attachment.Size >= bytesThreshold) throw Errors.AttachmentTooLarge(sizeThreshold);
if (list.Sum(a => a.Size) + attachment.Size >= bytesThreshold)
{
chunks.Add(list);
list = new List<Message.Attachment>();
}
list.Add(attachment);
}
if (list.Count > 0) chunks.Add(list);
return chunks;
}
private string FixProxyName(string name) => FixSingleCharacterName(FixClyde(name));
private string FixClyde(string name)
{
static string Replacement(Match m) => m.Groups[1].Value + "\u200A" + m.Groups[2].Value;
// Adds a Unicode hair space (\u200A) between the "c" and the "lyde" to avoid Discord matching it
// 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)
return proxyName + "\u17b5";
return proxyName;
}).ToArray()
};
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)
{
var attachmentResponse =
await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead);
return new MultipartFile(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync(),
attachment.Description);
}
return await Task.WhenAll(attachments.Select(GetStream));
}
private IReadOnlyList<IReadOnlyCollection<Message.Attachment>> ChunkAttachmentsOrThrow(
IReadOnlyList<Message.Attachment> attachments, int sizeThreshold)
{
// Splits a list of attachments into "chunks" of at most 8MB each
// 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>();
// sizeThreshold is in MB (user-readable)
var bytesThreshold = sizeThreshold * 1024 * 1024;
foreach (var attachment in attachments)
{
if (attachment.Size >= bytesThreshold) throw Errors.AttachmentTooLarge(sizeThreshold);
if (list.Sum(a => a.Size) + attachment.Size >= bytesThreshold)
{
chunks.Add(list);
list = new List<Message.Attachment>();
}
list.Add(attachment);
}
if (list.Count > 0) chunks.Add(list);
return chunks;
}
private string FixProxyName(string name) => FixSingleCharacterName(FixClyde(name));
private string FixClyde(string name)
{
static string Replacement(Match m) => m.Groups[1].Value + "\u200A" + m.Groups[2].Value;
// Adds a Unicode hair space (\u200A) between the "c" and the "lyde" to avoid Discord matching it
// 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)
return proxyName + "\u17b5";
return proxyName;
}
}