PluralKit/PluralKit.Bot/Utils/SentryUtils.cs

96 lines
3.8 KiB
C#
Raw Normal View History

2020-05-02 14:00:43 +00:00
using System.Collections.Generic;
using System.Linq;
using DSharpPlus;
using DSharpPlus.EventArgs;
2020-12-22 12:15:26 +00:00
using Myriad.Gateway;
2020-05-02 14:00:43 +00:00
using Sentry;
namespace PluralKit.Bot
{
2020-12-22 12:15:26 +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
}
2020-12-22 12:15:26 +00:00
public class SentryEnricher //:
// TODO!!!
// ISentryEnricher<MessageCreateEventArgs>,
// ISentryEnricher<MessageDeleteEventArgs>,
// ISentryEnricher<MessageUpdateEventArgs>,
// ISentryEnricher<MessageBulkDeleteEventArgs>,
// ISentryEnricher<MessageReactionAddEventArgs>
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
2020-12-22 12:15:26 +00:00
public void Enrich(Scope scope, Shard shard, MessageCreateEventArgs evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb(evt.Message.Content, "event.message", data: new Dictionary<string, string>
{
{"user", evt.Author.Id.ToString()},
{"channel", evt.Channel.Id.ToString()},
{"guild", evt.Channel.GuildId.ToString()},
{"message", evt.Message.Id.ToString()},
});
2020-12-22 12:15:26 +00:00
scope.SetTag("shard", shard.ShardInfo?.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
var perms = evt.Channel.BotPermissions();
scope.AddBreadcrumb(perms.ToPermissionString(), "permissions");
}
2020-12-22 12:15:26 +00:00
public void Enrich(Scope scope, Shard shard, MessageDeleteEventArgs evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.messageDelete",
data: new Dictionary<string, string>()
{
{"channel", evt.Channel.Id.ToString()},
{"guild", evt.Channel.GuildId.ToString()},
{"message", evt.Message.Id.ToString()},
});
2020-12-22 12:15:26 +00:00
scope.SetTag("shard", shard.ShardInfo?.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2020-12-22 12:15:26 +00:00
public void Enrich(Scope scope, Shard shard, MessageUpdateEventArgs evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb(evt.Message.Content ?? "<unknown>", "event.messageEdit",
data: new Dictionary<string, string>()
{
{"channel", evt.Channel.Id.ToString()},
{"guild", evt.Channel.GuildId.ToString()},
{"message", evt.Message.Id.ToString()}
});
2020-12-22 12:15:26 +00:00
scope.SetTag("shard", shard.ShardInfo?.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2020-12-22 12:15:26 +00:00
public void Enrich(Scope scope, Shard shard, MessageBulkDeleteEventArgs evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.messageDelete",
data: new Dictionary<string, string>()
{
{"channel", evt.Channel.Id.ToString()},
{"guild", evt.Channel.Id.ToString()},
{"messages", string.Join(",", evt.Messages.Select(m => m.Id))},
});
2020-12-22 12:15:26 +00:00
scope.SetTag("shard", shard.ShardInfo?.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
2020-12-22 12:15:26 +00:00
public void Enrich(Scope scope, Shard shard, MessageReactionAddEventArgs evt)
2020-05-02 14:00:43 +00:00
{
scope.AddBreadcrumb("", "event.reaction",
data: new Dictionary<string, string>()
{
{"user", evt.User.Id.ToString()},
{"channel", (evt.Channel?.Id ?? 0).ToString()},
{"guild", (evt.Channel?.GuildId ?? 0).ToString()},
{"message", evt.Message.Id.ToString()},
{"reaction", evt.Emoji.Name}
});
2020-12-22 12:15:26 +00:00
scope.SetTag("shard", shard.ShardInfo?.ShardId.ToString());
2020-05-02 14:00:43 +00:00
}
}
}