Fix various bugs and regressions
This commit is contained in:
parent
8785354a2b
commit
80c572f594
@ -73,11 +73,14 @@ namespace Myriad.Rest.Ratelimit
|
||||
try
|
||||
{
|
||||
_semaphore.Wait();
|
||||
|
||||
_logger.Verbose("{BucketKey}/{BucketMajor}: Received rate limit headers: {@RateLimitHeaders}",
|
||||
Key, Major, headers);
|
||||
|
||||
if (headers.ResetAfter != null)
|
||||
{
|
||||
var headerNextReset = DateTimeOffset.UtcNow + headers.ResetAfter.Value; // todo: server time
|
||||
if (headerNextReset > _nextReset)
|
||||
if (_nextReset == null || headerNextReset > _nextReset)
|
||||
{
|
||||
_logger.Debug("{BucketKey}/{BucketMajor}: Received reset time {NextReset} from server (after: {NextResetAfter}, remaining: {Remaining}, local remaining: {LocalRemaining})",
|
||||
Key, Major, headerNextReset, headers.ResetAfter.Value, headers.Remaining, Remaining);
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
public record GuildMemberPartial
|
||||
{
|
||||
public string Nick { get; init; }
|
||||
public string? Nick { get; init; }
|
||||
public ulong[] Roles { get; init; }
|
||||
public string JoinedAt { get; init; }
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ using Myriad.Cache;
|
||||
using Myriad.Extensions;
|
||||
using Myriad.Gateway;
|
||||
using Myriad.Rest;
|
||||
using Myriad.Rest.Exceptions;
|
||||
using Myriad.Types;
|
||||
|
||||
using NodaTime;
|
||||
@ -244,9 +245,12 @@ namespace PluralKit.Bot
|
||||
|
||||
// Once we've sent it to Sentry, report it to the user (if we have permission to)
|
||||
var reportChannel = handler.ErrorChannelFor(evt);
|
||||
// TODO: ID lookup
|
||||
// if (reportChannel != null && reportChannel.BotHasAllPermissions(Permissions.SendMessages | Permissions.EmbedLinks))
|
||||
// await _errorMessageService.SendErrorMessage(reportChannel, sentryEvent.EventId.ToString());
|
||||
if (reportChannel != null)
|
||||
{
|
||||
var botPerms = PermissionsIn(reportChannel.Value);
|
||||
if (botPerms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
|
||||
await _errorMessageService.SendErrorMessage(reportChannel.Value, sentryEvent.EventId.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,9 +101,9 @@ namespace PluralKit.Bot
|
||||
{
|
||||
Content = text,
|
||||
Embed = embed,
|
||||
AllowedMentions = mentions
|
||||
// Default to an empty allowed mentions object instead of null (which means no mentions allowed)
|
||||
AllowedMentions = mentions ?? new AllowedMentions()
|
||||
});
|
||||
// TODO: mentions should default to empty and not null?
|
||||
|
||||
if (embed != null)
|
||||
{
|
||||
|
@ -144,7 +144,6 @@ namespace PluralKit.Bot
|
||||
try
|
||||
{
|
||||
var dm = await ctx.RestNew.CreateDm(ctx.AuthorNew.Id);
|
||||
// TODO: send file
|
||||
|
||||
var msg = await ctx.RestNew.CreateMessage(dm.Id,
|
||||
new MessageRequest {Content = $"{Emojis.Success} Here you go!"},
|
||||
|
@ -84,8 +84,7 @@ namespace PluralKit.Bot {
|
||||
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0;
|
||||
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0;
|
||||
|
||||
// TODO: shard stuff
|
||||
var shardId = ctx.ShardNew.ShardInfo?.ShardId ?? -1;
|
||||
var shardId = ctx.ShardNew.ShardInfo.ShardId;
|
||||
var shardTotal = ctx.Cluster.Shards.Count;
|
||||
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
|
||||
var shardInfo = _shards.GetShardInfo(ctx.ShardNew);
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using NodaTime;
|
||||
@ -70,6 +71,7 @@ namespace PluralKit.Bot
|
||||
embedTitle,
|
||||
async (builder, switches) =>
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var entry in switches)
|
||||
{
|
||||
var lastSw = entry.LastTime;
|
||||
@ -98,17 +100,13 @@ namespace PluralKit.Bot
|
||||
stringToAdd =
|
||||
$"**{membersStr}** ({sw.Timestamp.FormatZoned(system.Zone)}, {switchSince.FormatDuration()} ago)\n";
|
||||
}
|
||||
|
||||
try // Unfortunately the only way to test DiscordEmbedBuilder.Description max length is this
|
||||
{
|
||||
// TODO: what is this??
|
||||
// builder.Description += stringToAdd;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
|
||||
if (sb.Length + stringToAdd.Length >= 1024)
|
||||
break;
|
||||
}// TODO: Make sure this works
|
||||
sb.Append(stringToAdd);
|
||||
}
|
||||
|
||||
builder.Description(sb.ToString());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ namespace PluralKit.Bot
|
||||
public async Task Handle(Shard shard, MessageCreateEvent evt)
|
||||
{
|
||||
if (evt.Author.Id == shard.User?.Id) return;
|
||||
if (evt.Type != Message.MessageType.Default) return;
|
||||
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
|
||||
if (IsDuplicateMessage(evt)) return;
|
||||
|
||||
var guild = evt.GuildId != null ? _cache.GetGuild(evt.GuildId.Value) : null;
|
||||
|
@ -88,7 +88,8 @@ namespace PluralKit.Bot
|
||||
if (ctx.SystemId == null) return false;
|
||||
|
||||
// Make sure channel is a guild text channel and this is a normal message
|
||||
if ((channel.Type != Channel.ChannelType.GuildText && channel.Type != Channel.ChannelType.GuildNews) || msg.Type != Message.MessageType.Default) return false;
|
||||
if (channel.Type != Channel.ChannelType.GuildText && channel.Type != Channel.ChannelType.GuildNews) return false;
|
||||
if (msg.Type != Message.MessageType.Default && msg.Type != Message.MessageType.Reply) return false;
|
||||
|
||||
// Make sure author is a normal user
|
||||
if (msg.Author.System == true || msg.Author.Bot || msg.WebhookId != null) return false;
|
||||
@ -109,12 +110,13 @@ namespace PluralKit.Bot
|
||||
{
|
||||
// Create reply embed
|
||||
var embeds = new List<Embed>();
|
||||
if (trigger.MessageReference?.ChannelId == trigger.ChannelId)
|
||||
if (trigger.Type == Message.MessageType.Reply && trigger.MessageReference?.ChannelId == trigger.ChannelId)
|
||||
{
|
||||
var repliedTo = await FetchReplyOriginalMessage(trigger.MessageReference);
|
||||
var repliedTo = trigger.ReferencedMessage.Value;
|
||||
if (repliedTo != null)
|
||||
{
|
||||
var embed = CreateReplyEmbed(repliedTo);
|
||||
var nickname = await FetchReferencedMessageAuthorNickname(trigger, repliedTo);
|
||||
var embed = CreateReplyEmbed(trigger, repliedTo, nickname);
|
||||
if (embed != null)
|
||||
embeds.Add(embed);
|
||||
}
|
||||
@ -130,7 +132,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
GuildId = trigger.GuildId!.Value,
|
||||
ChannelId = trigger.ChannelId,
|
||||
Name = FixSingleCharacterName(match.Member.ProxyName(ctx)),
|
||||
Name = match.Member.ProxyName(ctx),
|
||||
AvatarUrl = match.Member.ProxyAvatar(ctx),
|
||||
Content = content,
|
||||
Attachments = trigger.Attachments,
|
||||
@ -140,39 +142,39 @@ namespace PluralKit.Bot
|
||||
await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match);
|
||||
}
|
||||
|
||||
private async Task<Message?> FetchReplyOriginalMessage(Message.Reference reference)
|
||||
private async Task<string?> FetchReferencedMessageAuthorNickname(Message trigger, Message referenced)
|
||||
{
|
||||
if (referenced.WebhookId != null)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
var msg = await _rest.GetMessage(reference.ChannelId!.Value, reference.MessageId!.Value);
|
||||
if (msg == null)
|
||||
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but it was not found",
|
||||
reference.ChannelId, reference.MessageId);
|
||||
return msg;
|
||||
var member = await _rest.GetGuildMember(trigger.GuildId!.Value, referenced.Author.Id);
|
||||
return member?.Nick;
|
||||
}
|
||||
catch (UnauthorizedException)
|
||||
catch (ForbiddenException)
|
||||
{
|
||||
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but bot was not allowed to",
|
||||
reference.ChannelId, reference.MessageId);
|
||||
_logger.Warning("Failed to fetch member {UserId} in guild {GuildId} when getting reply nickname, falling back to username",
|
||||
referenced.Author.Id, trigger.GuildId!.Value);
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Embed CreateReplyEmbed(Message original)
|
||||
private Embed CreateReplyEmbed(Message trigger, Message repliedTo, string? nickname)
|
||||
{
|
||||
var jumpLink = $"https://discord.com/channels/{original.GuildId}/{original.ChannelId}/{original.Id}";
|
||||
// repliedTo doesn't have a GuildId field :/
|
||||
var jumpLink = $"https://discord.com/channels/{trigger.GuildId}/{repliedTo.ChannelId}/{repliedTo.Id}";
|
||||
|
||||
var content = new StringBuilder();
|
||||
|
||||
var hasContent = !string.IsNullOrWhiteSpace(original.Content);
|
||||
var hasContent = !string.IsNullOrWhiteSpace(repliedTo.Content);
|
||||
if (hasContent)
|
||||
{
|
||||
var msg = original.Content;
|
||||
var msg = repliedTo.Content;
|
||||
if (msg.Length > 100)
|
||||
{
|
||||
msg = original.Content.Substring(0, 100);
|
||||
var spoilersInOriginalString = Regex.Matches(original.Content, @"\|\|").Count;
|
||||
msg = repliedTo.Content.Substring(0, 100);
|
||||
var spoilersInOriginalString = Regex.Matches(repliedTo.Content, @"\|\|").Count;
|
||||
var spoilersInTruncatedString = Regex.Matches(msg, @"\|\|").Count;
|
||||
if (spoilersInTruncatedString % 2 == 1 && spoilersInOriginalString % 2 == 0)
|
||||
msg += "||";
|
||||
@ -181,7 +183,7 @@ namespace PluralKit.Bot
|
||||
|
||||
content.Append($"**[Reply to:]({jumpLink})** ");
|
||||
content.Append(msg);
|
||||
if (original.Attachments.Length > 0)
|
||||
if (repliedTo.Attachments.Length > 0)
|
||||
content.Append($" {Emojis.Paperclip}");
|
||||
}
|
||||
else
|
||||
@ -189,11 +191,8 @@ namespace PluralKit.Bot
|
||||
content.Append($"*[(click to see attachment)]({jumpLink})*");
|
||||
}
|
||||
|
||||
// TODO: get the nickname somehow
|
||||
var username = original.Author.Username;
|
||||
// var username = original.Member?.Nick ?? original.Author.Username;
|
||||
|
||||
var avatarUrl = $"https://cdn.discordapp.com/avatars/{original.Author.Id}/{original.Author.Avatar}.png";
|
||||
var username = nickname ?? repliedTo.Author.Username;
|
||||
var avatarUrl = $"https://cdn.discordapp.com/avatars/{repliedTo.Author.Id}/{repliedTo.Author.Avatar}.png";
|
||||
|
||||
return new Embed
|
||||
{
|
||||
@ -288,12 +287,6 @@ namespace PluralKit.Bot
|
||||
return true;
|
||||
}
|
||||
|
||||
private string FixSingleCharacterName(string proxyName)
|
||||
{
|
||||
if (proxyName.Length == 1) return proxyName += "\u17b5";
|
||||
else return proxyName;
|
||||
}
|
||||
|
||||
private void CheckProxyNameBoundsOrError(string proxyName)
|
||||
{
|
||||
if (proxyName.Length > Limits.MaxProxyNameLength) throw Errors.ProxyNameTooLong(proxyName);
|
||||
|
@ -53,8 +53,6 @@ namespace PluralKit.Bot {
|
||||
}
|
||||
|
||||
// Send embed!
|
||||
|
||||
// TODO: fix?
|
||||
await using var conn = await _db.Obtain();
|
||||
var embed = _embed.CreateLoggedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value),
|
||||
await _repo.GetMember(conn, proxy.Member.Id), hookMessage, trigger.Id, trigger.Author, proxy.Content,
|
||||
|
@ -87,7 +87,7 @@ namespace PluralKit.Bot
|
||||
|
||||
var webhookReq = new ExecuteWebhookRequest
|
||||
{
|
||||
Username = FixClyde(req.Name).Truncate(80),
|
||||
Username = FixProxyName(req.Name).Truncate(80),
|
||||
Content = content,
|
||||
AllowedMentions = allowedMentions,
|
||||
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
||||
@ -185,6 +185,8 @@ namespace PluralKit.Bot
|
||||
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;
|
||||
@ -193,5 +195,12 @@ namespace PluralKit.Bot
|
||||
// since Discord blocks webhooks containing the word "Clyde"... for some reason. /shrug
|
||||
return Regex.Replace(name, "(c)(lyde)", Replacement, RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
private string FixSingleCharacterName(string proxyName)
|
||||
{
|
||||
if (proxyName.Length == 1)
|
||||
return proxyName + "\u17b5";
|
||||
return proxyName;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user