Skip to content

Commit

Permalink
simplify query partials and types
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFGray committed Sep 6, 2024
1 parent 38d057e commit c150db9
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions app/discord/activityTracker.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import {
Events,
ChannelType,
type Client,
type Message,
type PartialMessage,
} from "discord.js";
import type { DB } from "kysely-codegen";
import type { UpdateObjectExpression } from "kysely/dist/cjs/parser/update-set-parser";
import { Events, ChannelType } from "discord.js";
import type { Client, Message, PartialMessage } from "discord.js";
import db from "~/db.server";

export async function startActivityTracking(client: Client) {
client.on(Events.MessageCreate, async (msg) => {
const info = await getMessageStats(msg);
if (!info) return;

await db
.insertInto("message_stats")
.values({
Expand All @@ -34,7 +26,7 @@ export async function startActivityTracking(client: Client) {
const info = await getMessageStats(msg);
console.log(msg, info);
if (!info) return;
await updateStatsById(msg.id, info).execute();
await updateStatsById(msg.id).set(info).execute();
reportByGuild(msg.guildId!);
});

Expand All @@ -49,25 +41,26 @@ export async function startActivityTracking(client: Client) {
});

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

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

function updateStatsById(
id: string,
info: UpdateObjectExpression<DB, "message_stats", "message_stats">,
) {
return db.updateTable("message_stats").set(info).where("message_id", "=", id);
function updateStatsById(id: string) {
return db.updateTable("message_stats").where("message_id", "=", id);
}

async function getMessageStats(msg: Message | PartialMessage) {
Expand Down

0 comments on commit c150db9

Please sign in to comment.