PluralKit/PluralKit.Bot/Utils/SentryUtils.cs

99 lines
3.7 KiB
C#
Raw Normal View History

2020-05-02 14:00:43 +00:00
using System.Collections.Generic;
2021-01-31 13:50:10 +00:00
using Myriad.Extensions;
2020-12-22 12:15:26 +00:00
using Myriad.Gateway;
2020-05-02 14:00:43 +00:00
using Sentry;
namespace PluralKit.Bot
{
2021-08-27 15:03:47 +00:00
public interface ISentryEnricher<T> where T : IGatewayEvent
2020-05-02 14:00:43 +00:00
{
2020-12-22 12:15:26 +00:00
void Enrich(Scope scope, Shard shard, T evt);
2020-05-02 14:00:43 +00:00
}
2021-01-31 13:50:10 +00:00
public class SentryEnricher:
ISentryEnricher<MessageCreateEvent>,
ISentryEnricher<MessageDeleteEvent>,
ISentryEnricher<MessageUpdateEvent>,
ISentryEnricher<MessageDeleteBulkEvent>,
ISentryEnricher<MessageReactionAddEvent>
2020-05-02 14:00:43 +00:00
{
2021-01-31 13:50:10 +00:00
private readonly Bot _bot;
public SentryEnricher(Bot bot)
{
_bot = bot;
}
2021-08-27 15:03:47 +00:00
2020-05-02 14:00:43 +00:00
// TODO: should this class take the Scope by dependency injection instead?
// Would allow us to create a centralized "chain of handlers" where this class could just be registered as an entry in
2021-08-27 15:03:47 +00:00
2021-01-31 13:50:10 +00:00
public void Enrich(Scope scope, Shard shard, MessageCreateEvent evt)
2020-05-02 14:00:43 +00:00
{
2021-01-31 13:50:10 +00:00
scope.AddBreadcrumb(evt.Content, "event.message", data: new Dictionary<string, string>
2020-05-02 14:00:43 +00:00
{
{"user", evt.Author.Id.ToString()},
2021-01-31 13:50:10 +00:00
{"channel", evt.ChannelId.ToString()},
{"guild", evt.GuildId.ToString()},
{"message", evt.Id.ToString()},
2020-05-02 14:00:43 +00:00
});
2021-04-29 09:10:19 +00:00
scope.SetTag("shard", shard.ShardId.ToString());
2020-05-02 14:00:43 +00:00
// Also report information about the bot's permissions in the channel
// We get a lot of permission errors so this'll be useful for determining problems
2021-01-31 13:50:10 +00:00
var perms = _bot.PermissionsIn(evt.ChannelId);
2020-05-02 14:00:43 +00:00
scope.AddBreadcrumb(perms.ToPermissionString(), "permissions");
}
2021-01-31 13:50:10 +00:00
public void Enrich(Scope scope, Shard shard, MessageDeleteEvent evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.messageDelete",
data: new Dictionary<string, string>()
{
2021-01-31 13:50:10 +00:00
{"channel", evt.ChannelId.ToString()},
{"guild", evt.GuildId.ToString()},
{"message", evt.Id.ToString()},
2020-05-02 14:00:43 +00:00
});
2021-04-29 09:10:19 +00:00
scope.SetTag("shard", shard.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2021-01-31 13:50:10 +00:00
public void Enrich(Scope scope, Shard shard, MessageUpdateEvent evt)
2020-05-02 14:00:43 +00:00
{
2021-01-31 13:50:10 +00:00
scope.AddBreadcrumb(evt.Content.Value ?? "<unknown>", "event.messageEdit",
2020-05-02 14:00:43 +00:00
data: new Dictionary<string, string>()
{
2021-01-31 13:50:10 +00:00
{"channel", evt.ChannelId.ToString()},
{"guild", evt.GuildId.Value.ToString()},
{"message", evt.Id.ToString()}
2020-05-02 14:00:43 +00:00
});
2021-04-29 09:10:19 +00:00
scope.SetTag("shard", shard.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2021-01-31 13:50:10 +00:00
public void Enrich(Scope scope, Shard shard, MessageDeleteBulkEvent evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.messageDelete",
data: new Dictionary<string, string>()
{
2021-01-31 13:50:10 +00:00
{"channel", evt.ChannelId.ToString()},
{"guild", evt.GuildId.ToString()},
{"messages", string.Join(",", evt.Ids)},
2020-05-02 14:00:43 +00:00
});
2021-04-29 09:10:19 +00:00
scope.SetTag("shard", shard.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2021-01-31 13:50:10 +00:00
public void Enrich(Scope scope, Shard shard, MessageReactionAddEvent evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.reaction",
data: new Dictionary<string, string>()
{
2021-01-31 13:50:10 +00:00
{"user", evt.UserId.ToString()},
{"channel", evt.ChannelId.ToString()},
{"guild", (evt.GuildId ?? 0).ToString()},
{"message", evt.MessageId.ToString()},
2020-05-02 14:00:43 +00:00
{"reaction", evt.Emoji.Name}
});
2021-04-29 09:10:19 +00:00
scope.SetTag("shard", shard.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
}
}