Skip to content

Commit

Permalink
Add Sentry instrumentation for event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonejt committed Apr 22, 2024
1 parent 68b0059 commit 6286b5c
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 6 deletions.
9 changes: 8 additions & 1 deletion commands/dominator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from "discord.js";
import { startSpan } from "@sentry/node";
import { ACTIONS, ATTRIBUTES, ATTR_PRETTY, ACTION_PRETTY, buildStringChoice, buildIntegerChoice } from "../clients/constants.js";
import { MessageDominators, MessageDominator } from "../clients/backend/dominator/messageDominators.js";
import { MemberDominators, MemberDominator } from "../clients/backend/dominator/memberDominators.js";
Expand Down Expand Up @@ -79,4 +80,10 @@ async function execute(interaction: ChatInputCommandInteraction) {
}
}

export default { data, execute };
async function executeWrapper(interaction: ChatInputCommandInteraction) {
startSpan({
name: `/dominator ${interaction.commandId} ${Date.now()}`
}, () => execute(interaction));
}

export default { data, execute: executeWrapper };
9 changes: 8 additions & 1 deletion commands/psychopass.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js";
import { startSpan } from "@sentry/node";
import { PsychoPasses } from "../clients/backend/psychopass/psychoPasses.js";
import { CommunityPsychoPasses } from "../clients/backend/psychopass/communityPsychoPasses.js";
import embedPsychoPass from "../embeds/psychoPass.js";
Expand Down Expand Up @@ -26,4 +27,10 @@ async function execute(interaction: ChatInputCommandInteraction) {
}
}

export default { data, execute };
async function executeWrapper(interaction: ChatInputCommandInteraction) {
startSpan({
name: `/psychopass ${interaction.commandId} ${Date.now()}`
}, () => execute(interaction));
}

export default { data, execute: executeWrapper };
9 changes: 8 additions & 1 deletion commands/sibyl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from "discord.js";
import { startSpan } from "@sentry/node";
import Communities from "../clients/backend/communities.js";

const data = new SlashCommandBuilder()
Expand Down Expand Up @@ -36,4 +37,10 @@ async function execute(interaction: ChatInputCommandInteraction) {
}
}

export default { data, execute };
async function executeWrapper(interaction: ChatInputCommandInteraction) {
startSpan({
name: `/sibyl ${interaction.commandId} ${Date.now()}`
}, () => execute(interaction));
}

export default { data, execute: executeWrapper };
7 changes: 7 additions & 0 deletions events/guildCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Guild } from "discord.js";
import { startSpan } from "@sentry/node";
import Communities from "../clients/backend/communities.js";

export default async function guildCreate(guild: Guild) {
startSpan({
name: `guildCreate ${guild.id} ${Date.now()}`
}, () => onGuildCreate(guild));
}

async function onGuildCreate(guild: Guild) {
Communities.create(guild.id);
guild.client.guilds.fetch();
guild.client.user.setPresence({ activities: [{ name: `${guild.client.guilds.cache.size} Servers`, type: 3 }] });
Expand Down
7 changes: 7 additions & 0 deletions events/guildDelete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Guild } from "discord.js";
import { startSpan } from "@sentry/node";
import Communities from "../clients/backend/communities.js";

export default async function guildDelete(guild: Guild) {
startSpan({
name: `guildDelete ${guild.id} ${Date.now()}`
}, () => onGuildDelete(guild));
}

async function onGuildDelete(guild: Guild) {
Communities.delete(guild.id);
guild.client.guilds.fetch();
guild.client.user.setPresence({ activities: [{ name: `${guild.client.guilds.cache.size} Servers`, type: 3 }] });
Expand Down
7 changes: 7 additions & 0 deletions events/guildMemberAdd.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { GuildMember, TextChannel } from "discord.js";
import { startSpan } from "@sentry/node";
import Communities from "../clients/backend/communities.js";
import { PsychoPasses, PsychoPass } from "../clients/backend/psychopass/psychoPasses.js";
import { MemberDominators, MemberDominator } from "../clients/backend/dominator/memberDominators.js";
import { ATTRIBUTES, ACTIONS, DEFAULT_MUTE_PERIOD, Reason, ATTR_PRETTY } from "../clients/constants.js";
import embedMemberModeration from "../embeds/memberModeration.js";

export async function guildMemberAdd(member: GuildMember) {
startSpan({
name: `guildMemberAdd ${member.user.id} ${Date.now()}`
}, () => onGuildMemberAdd(member));
}

async function onGuildMemberAdd(member: GuildMember) {
console.log(`A new @${member.user.username} (${member.user.id}) has joined Server: ${member.guild.name} (${member.guild.id})`);
const psychoPass = await PsychoPasses.read(member.user.id);
if (psychoPass) moderateMember(member);
Expand Down
7 changes: 7 additions & 0 deletions events/guildMemberRemove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { PartialGuildMember, GuildMember } from "discord.js";
import { startSpan } from "@sentry/node";
import { CommunityPsychoPasses } from "../clients/backend/psychopass/communityPsychoPasses.js";

export default async function guildMemberRemove(member: GuildMember | PartialGuildMember) {
startSpan({
name: `guildMemberRemove ${member.user.id} ${Date.now()}`
}, () => onGuildMemberRemove(member));
}

async function onGuildMemberRemove(member: GuildMember | PartialGuildMember) {
console.log(`@${member.user.username} (${member.user.id}) has left Server: ${member.guild.name} (${member.guild.id})`);
CommunityPsychoPasses.update(member.guild.id, member.user.id);
}
9 changes: 8 additions & 1 deletion events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { SibylDiscordClient } from "../clients/discord.js";
import { Interaction } from "discord.js";
import { startSpan } from "@sentry/node";
import { SibylDiscordClient } from "../clients/discord.js";

export default async function interactionCreate(interaction: Interaction) {
startSpan({
name: `interactionCreate ${interaction.id} ${Date.now()}`
}, () => onInteractionCreate(interaction));
}

async function onInteractionCreate(interaction: Interaction) {
if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) {
const command = (interaction.client as SibylDiscordClient).commands.get(interaction.commandName);

Expand Down
7 changes: 7 additions & 0 deletions events/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Message, TextChannel } from "discord.js";
import { startSpan } from "@sentry/node";
import { analyzeComment, MessageAnalysis } from "../clients/perspectiveAPI.js";
import ingestMessage from "../clients/backend/ingestMessage.js";
import Communities from "../clients/backend/communities.js";
Expand All @@ -8,6 +9,12 @@ import { moderateMember } from "./guildMemberAdd.js";
import embedMessageModeration from "../embeds/messageModeration.js";

export async function messageCreate(message: Message) {
startSpan({
name: `messageCreate ${message.author.id} ${Date.now()}`
}, () => onMessageCreate(message));
}

async function onMessageCreate(message: Message) {
if (message.author.id === process.env.DISCORD_CLIENT_ID ||
message.author.bot ||
(message.channel as TextChannel).nsfw ||
Expand Down
9 changes: 8 additions & 1 deletion events/messageUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Message, PartialMessage, TextChannel } from "discord.js";
import { startSpan } from "@sentry/node";
import { MessageAnalysis, analyzeComment } from "../clients/perspectiveAPI.js";
import ingestMessage from "../clients/backend/ingestMessage.js";
import { MessageDominators, MessageDominator } from "../clients/backend/dominator/messageDominators.js";
import { ACTIONS, Reason } from "../clients/constants.js";
import { moderateMember } from "./guildMemberAdd.js";
import { moderateMessage } from "./messageCreate.js";

export default async function messageUpdate(_: Message | PartialMessage, newMessage: Message | PartialMessage) {
export default async function messageUpdate(oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage) {
startSpan({
name: `messageUpdate ${oldMessage.id} ${newMessage.id} ${Date.now()}`
}, () => onMessageUpdate(oldMessage, newMessage));
}

async function onMessageUpdate(_: Message | PartialMessage, newMessage: Message | PartialMessage) {
newMessage = newMessage as Message;
if (newMessage.author.id === process.env.DISCORD_CLIENT_ID ||
newMessage.author.bot ||
Expand Down
9 changes: 8 additions & 1 deletion events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { startSpan } from "@sentry/node";
import { Client } from "discord.js";

export default async function ready(client: Client): Promise<void> {
export default async function ready(client: Client) {
startSpan({
name: `ready ${Date.now()}`
}, () => onReady(client));
}

async function onReady(client: Client) {
console.log(`Logged in as ${client.user?.username}!`);
await client.guilds.fetch();
client.user!.setPresence({ activities: [{ name: `${client.guilds.cache.size} Servers`, type: 3 }] });
Expand Down

0 comments on commit 6286b5c

Please sign in to comment.