Skip to content

Commit

Permalink
Add metrics for new Tagged forum posts
Browse files Browse the repository at this point in the history
  • Loading branch information
vcarl committed Aug 19, 2023
1 parent b0b738f commit 0820c32
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/features/stats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fetch from "node-fetch";
import queryString from "query-string";
import { Client } from "discord.js";
import { ChannelType, Client } from "discord.js";
import { amplitudeKey } from "../helpers/env";
import { difference } from "../helpers/sets";
import { mapAppliedTagsToTagNames } from "../helpers/discord";

type AmplitudeValue = string | number | boolean;
type EmitEventData = Record<string, AmplitudeValue | AmplitudeValue[]>;
Expand Down Expand Up @@ -32,13 +34,16 @@ const emitEvent = (
};

const message = "message sent";
const postTagged = "post tagged";
const threadCreated = "thread created";
const threadReplyRemoved = "thread reply removed";
const threadTimeout = "thread timeout";
const threadResolved = "thread resolved";
const reacted = "reaction added";

export const threadStats = {
postTagged: (tags: string[], channel: string) =>
emitEvent(postTagged, { data: { tags, channel } }),
threadCreated: (channel: string) =>
emitEvent(threadCreated, { data: { channel } }),
threadReplyRemoved: (channel: string) =>
Expand All @@ -57,6 +62,37 @@ export const threadStats = {
};

const stats = (client: Client) => {
client.on("threadUpdate", async (oldThread, newThread) => {
if (oldThread.parent?.type !== ChannelType.GuildForum) {
return;
}
const appliedTags = [
...difference(
new Set(newThread.appliedTags),
new Set(oldThread.appliedTags),
),
];

if (appliedTags.length === 0) {
return;
}

threadStats.postTagged(
mapAppliedTagsToTagNames(appliedTags, oldThread.parent),
newThread.parentId ?? "0",
);
});
client.on("threadCreate", async (thread) => {
if (
thread.parent?.type === ChannelType.GuildForum &&
thread.appliedTags.length > 0
) {
threadStats.postTagged(
mapAppliedTagsToTagNames(thread.appliedTags, thread.parent),
thread.parentId ?? "0",
);
}
});
client.on("messageReactionAdd", async (reaction, user) => {
const { message, emoji } = reaction;
const { channel } = message;
Expand All @@ -72,6 +108,7 @@ const stats = (client: Client) => {
data: {
channel: channelId,
emoji: emoji.toString() ?? "n/a",
target: message.author?.id ?? "0",
},
userId: user.id,
});
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SlashCommandBuilder,
APIApplicationCommand,
APIApplicationCommandOption,
ForumChannel,
} from "discord.js";
import prettyBytes from "pretty-bytes";

Expand Down Expand Up @@ -113,6 +114,16 @@ export const quoteAndEscape = (content: string) => {
return escapeDisruptiveContent(quoteMessageContent(content));
};

export const mapAppliedTagsToTagNames = (
appliedTags: string[],
channel: ForumChannel,
) => {
const { availableTags: tags } = channel;
const tagLookup = new Map(tags.map((e) => [e.id, e]));

return appliedTags.map((id) => tagLookup.get(id)?.name ?? "");
};

//
// Types and type helpers for command configs
//
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/sets.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/**
* Difference returns a list of items that are in A but is not present in B.
*/
export const difference = <T>(a: Set<T>, b: Set<T>) =>
new Set(Array.from(a).filter((x) => !b.has(x)));

0 comments on commit 0820c32

Please sign in to comment.