Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

message metadata tracking #73

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ export interface Guilds {
settings: string | null;
}

export interface MessageStats {
author_id: string;
channel_category: string | null;
channel_id: string;
char_count: number;
guild_id: string;
message_id: string | null;
react_count: Generated<number>;
recipient_id: string | null;
sent_at: string;
word_count: number;
}

export interface Sessions {
data: string | null;
expires: string | null;
Expand All @@ -24,6 +37,7 @@ export interface Users {

export interface DB {
guilds: Guilds;
message_stats: MessageStats;
sessions: Sessions;
users: Users;
}
96 changes: 96 additions & 0 deletions app/discord/activityTracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Events, ChannelType } from "discord.js";
import type { Client, Message, PartialMessage, TextChannel } from "discord.js";
import db from "~/db.server";

export async function startActivityTracking(client: Client) {
const channelCache = new Map<string, TextChannel>();

async function getOrFetchChannel(msg: Message) {
// TODO: cache eviction?
return channelCache.has(msg.channelId)
? channelCache.get(msg.channelId)
: channelCache
.set(msg.channelId, (await msg.channel.fetch()) as TextChannel)
.get(msg.channelId);
}

client.on(Events.MessageCreate, async (msg) => {
const info = await getMessageStats(msg);
if (!info) return;
await db
.insertInto("message_stats")
.values({
...info,
message_id: msg.id,
author_id: msg.author!.id,
guild_id: msg.guildId!,
channel_id: msg.channelId,
recipient_id: msg.mentions?.repliedUser?.id ?? null,
channel_category: (await getOrFetchChannel(msg))?.parent?.name,
})
.execute();
});

client.on(Events.MessageUpdate, async (msg) => {
const info = await getMessageStats(msg);
if (!info) return;
await updateStatsById(msg.id).set(info).execute();
});

client.on(Events.MessageDelete, async (msg) => {
const info = await getMessageStats(msg);
if (!info) return;
await db
.deleteFrom("message_stats")
.where("message_id", "=", msg.id)
.execute();
});

client.on(Events.MessageReactionAdd, async (msg) => {
await updateStatsById(msg.message.id)
.set({ react_count: (eb) => eb(eb.ref("react_count"), "+", 1) })
.execute();
});

client.on(Events.MessageReactionRemove, async (msg) => {
await updateStatsById(msg.message.id)
.set({ react_count: (eb) => eb(eb.ref("react_count"), "-", 1) })
.execute();
});
}

function updateStatsById(id: string) {
return db.updateTable("message_stats").where("message_id", "=", id);
}

async function getMessageStats(msg: Message | PartialMessage) {
// TODO: more filters
if (msg.channel.type !== ChannelType.GuildText || msg.author?.bot) {
return;
}
const { content } = await msg.fetch();
return {
char_count: content?.length ?? 0,
word_count: content?.split(/\s+/).length ?? 0,
react_count: msg.reactions.cache.size,
sent_at: String(msg.createdTimestamp),
DanielFGray marked this conversation as resolved.
Show resolved Hide resolved
};
}

export async function reportByGuild(guildId: string) {
const result = await db
.selectFrom("message_stats")
.select((eb) => [
eb.fn.countAll().as("message_count"),
eb.fn.sum("char_count").as("char_total"),
eb.fn.sum("word_count").as("word_total"),
eb.fn.sum("react_count").as("react_total"),
eb.fn.avg("char_count").as("avg_chars"),
eb.fn.avg("word_count").as("avg_words"),
eb.fn.avg("react_count").as("avg_reacts"),
])
.where("guild_id", "=", guildId)
.groupBy("author_id")
.execute();
return result
}
2 changes: 2 additions & 0 deletions app/discord/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

import automod from "~/discord/automod";
import onboardGuild from "~/discord/onboardGuild";
import { startActivityTracking } from "~/discord/activityTracker";

import * as convene from "~/commands/convene";
import * as setup from "~/commands/setup";
Expand All @@ -27,6 +28,7 @@ export default function init() {
onboardGuild(client),
automod(client),
deployCommands(client),
startActivityTracking(client),
]);
});

Expand Down
21 changes: 21 additions & 0 deletions migrations/20240906155529-message_stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Kysely } from "kysely";

export async function up(db: Kysely<any>) {
return db.schema
.createTable("message_stats")
.addColumn("message_id", "text", (c) => c.primaryKey())
.addColumn("author_id", "text", (c) => c.notNull())
.addColumn("guild_id", "text", (c) => c.notNull())
.addColumn("channel_id", "text", (c) => c.notNull())
.addColumn("channel_category", "text")
.addColumn("recipient_id", "text")
.addColumn("char_count", "integer", (c) => c.notNull())
.addColumn("word_count", "integer", (c) => c.notNull())
.addColumn("react_count", "integer", (c) => c.notNull().defaultTo(0))
.addColumn("sent_at", "integer", (c) => c.notNull())
.execute();
}

export async function down(db: Kysely<any>) {
return db.schema.dropTable("message_stats").execute();
}
Loading