Converted enough to send the system card
This commit is contained in:
@@ -5,9 +5,13 @@ using System.Threading.Tasks;
|
||||
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus.Exceptions;
|
||||
|
||||
using Humanizer;
|
||||
|
||||
using Myriad.Cache;
|
||||
using Myriad.Rest;
|
||||
using Myriad.Types;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
using PluralKit.Core;
|
||||
@@ -18,54 +22,79 @@ namespace PluralKit.Bot {
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly DiscordShardedClient _client;
|
||||
private readonly IDiscordCache _cache;
|
||||
private readonly DiscordApiClient _rest;
|
||||
|
||||
public EmbedService(DiscordShardedClient client, IDatabase db, ModelRepository repo)
|
||||
public EmbedService(DiscordShardedClient client, IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
|
||||
{
|
||||
_client = client;
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_cache = cache;
|
||||
_rest = rest;
|
||||
}
|
||||
|
||||
private Task<(ulong Id, User? User)[]> GetUsers(IEnumerable<ulong> ids)
|
||||
{
|
||||
async Task<(ulong Id, User? User)> Inner(ulong id)
|
||||
{
|
||||
if (_cache.TryGetUser(id, out var cachedUser))
|
||||
return (id, cachedUser);
|
||||
|
||||
var user = await _rest.GetUser(id);
|
||||
if (user == null)
|
||||
return (id, null);
|
||||
// todo: move to "GetUserCached" helper
|
||||
await _cache.SaveUser(user);
|
||||
return (id, user);
|
||||
}
|
||||
|
||||
return Task.WhenAll(ids.Select(Inner));
|
||||
}
|
||||
|
||||
public async Task<DiscordEmbed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
|
||||
public async Task<Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
|
||||
{
|
||||
await using var conn = await _db.Obtain();
|
||||
|
||||
// Fetch/render info for all accounts simultaneously
|
||||
var accounts = await _repo.GetSystemAccounts(conn, system.Id);
|
||||
var users = await Task.WhenAll(accounts.Select(async uid => (await cctx.Shard.GetUser(uid))?.NameAndMention() ?? $"(deleted account {uid})"));
|
||||
var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})");
|
||||
|
||||
var memberCount = cctx.MatchPrivateFlag(ctx) ? await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public) : await _repo.GetSystemMemberCount(conn, system.Id);
|
||||
|
||||
var eb = new DiscordEmbedBuilder()
|
||||
.WithColor(DiscordUtils.Gray)
|
||||
.WithTitle(system.Name ?? null)
|
||||
.WithThumbnail(system.AvatarUrl)
|
||||
.WithFooter($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}");
|
||||
var embed = new Embed
|
||||
{
|
||||
Title = system.Name,
|
||||
Thumbnail = new(system.AvatarUrl),
|
||||
Footer = new($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}"),
|
||||
Color = (uint?) DiscordUtils.Gray.Value
|
||||
};
|
||||
var fields = new List<Embed.Field>();
|
||||
|
||||
var latestSwitch = await _repo.GetLatestSwitch(conn, system.Id);
|
||||
if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
|
||||
{
|
||||
var switchMembers = await _repo.GetSwitchMembers(conn, latestSwitch.Id).ToListAsync();
|
||||
if (switchMembers.Count > 0)
|
||||
eb.AddField("Fronter".ToQuantity(switchMembers.Count(), ShowQuantityAs.None),
|
||||
string.Join(", ", switchMembers.Select(m => m.NameFor(ctx))));
|
||||
if (switchMembers.Count > 0)
|
||||
fields.Add(new("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))));
|
||||
}
|
||||
|
||||
if (system.Tag != null) eb.AddField("Tag", system.Tag.EscapeMarkdown());
|
||||
eb.AddField("Linked accounts", string.Join("\n", users).Truncate(1000), true);
|
||||
if (system.Tag != null)
|
||||
fields.Add(new("Tag", system.Tag.EscapeMarkdown()));
|
||||
fields.Add(new("Linked accounts", string.Join("\n", users).Truncate(1000), true));
|
||||
|
||||
if (system.MemberListPrivacy.CanAccess(ctx))
|
||||
{
|
||||
if (memberCount > 0)
|
||||
eb.AddField($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true);
|
||||
fields.Add(new($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true));
|
||||
else
|
||||
eb.AddField($"Members ({memberCount})", "Add one with `pk;member new`!", true);
|
||||
fields.Add(new($"Members ({memberCount})", "Add one with `pk;member new`!", true));
|
||||
}
|
||||
|
||||
if (system.DescriptionFor(ctx) is { } desc)
|
||||
eb.AddField("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false);
|
||||
fields.Add(new("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false));
|
||||
|
||||
return eb.Build();
|
||||
return embed with { Fields = fields.ToArray() };
|
||||
}
|
||||
|
||||
public DiscordEmbed CreateLoggedMessageEmbed(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, DiscordUser sender, string content, DiscordChannel channel) {
|
||||
|
@@ -60,18 +60,16 @@ namespace PluralKit.Bot {
|
||||
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
|
||||
{
|
||||
// TODO: fetch it directly on cache miss?
|
||||
var channel = await _cache.GetChannel(channelId);
|
||||
if (_cache.TryGetChannel(channelId, out var channel))
|
||||
return channel;
|
||||
|
||||
if (channel == null)
|
||||
{
|
||||
// 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});
|
||||
}
|
||||
// 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 channel;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,7 @@ using App.Metrics;
|
||||
using Humanizer;
|
||||
|
||||
using Myriad.Cache;
|
||||
using Myriad.Extensions;
|
||||
using Myriad.Rest;
|
||||
using Myriad.Rest.Types;
|
||||
using Myriad.Rest.Types.Requests;
|
||||
@@ -77,20 +78,22 @@ namespace PluralKit.Bot
|
||||
|
||||
private async Task<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)
|
||||
{
|
||||
var guild = await _cache.GetGuild(req.GuildId)!;
|
||||
var guild = _cache.GetGuild(req.GuildId);
|
||||
var content = req.Content.Truncate(2000);
|
||||
|
||||
var allowedMentions = content.ParseMentions();
|
||||
if (!req.AllowEveryone)
|
||||
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild);
|
||||
|
||||
var webhookReq = new ExecuteWebhookRequest
|
||||
{
|
||||
Username = FixClyde(req.Name).Truncate(80),
|
||||
Content = content,
|
||||
AllowedMentions = null, // todo
|
||||
AllowedMentions = allowedMentions,
|
||||
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
||||
Embeds = req.Embeds
|
||||
};
|
||||
|
||||
// dwb.AddMentions(content.ParseAllMentions(guild, req.AllowEveryone));
|
||||
|
||||
MultipartFile[] files = null;
|
||||
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, 8 * 1024 * 1024);
|
||||
if (attachmentChunks.Count > 0)
|
||||
|
Reference in New Issue
Block a user