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