diff --git a/src/index.ts b/src/index.ts index 498abf1..ac1965f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,6 +41,14 @@ client.on(Events.InteractionCreate, (interaction) => { } }); +client.on(Events.EntitlementCreate, (entitlement) => { + void logger.log("info", `User ${entitlement.userId} has subscribed!`); +}); + +client.on(Events.EntitlementDelete, (entitlement) => { + void logger.log("info", `User ${entitlement.userId} has unsubscribed... :c`); +}); + client.on(Events.ClientReady, () => { void logger.log("debug", "Bot is ready."); }); diff --git a/src/modules/alt.ts b/src/modules/alt.ts index 93dd413..8bcaea2 100644 --- a/src/modules/alt.ts +++ b/src/modules/alt.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js"; @@ -109,4 +110,7 @@ export const alt = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "alt-text"); }; diff --git a/src/modules/evaluate.ts b/src/modules/evaluate.ts index 8ac2981..0605917 100644 --- a/src/modules/evaluate.ts +++ b/src/modules/evaluate.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; /** @@ -40,4 +41,7 @@ export const evaluate = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "evaluate"); }; diff --git a/src/modules/mood.ts b/src/modules/mood.ts index 42d2d17..3d0dbc3 100644 --- a/src/modules/mood.ts +++ b/src/modules/mood.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; /** @@ -41,4 +42,7 @@ export const mood = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "mood"); }; diff --git a/src/modules/proofread.ts b/src/modules/proofread.ts index 71f5af6..56dd70d 100644 --- a/src/modules/proofread.ts +++ b/src/modules/proofread.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; /** @@ -41,4 +42,7 @@ export const proofread = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "proofread"); }; diff --git a/src/modules/query.ts b/src/modules/query.ts index 68566d9..30c9df0 100644 --- a/src/modules/query.ts +++ b/src/modules/query.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; /** @@ -41,4 +42,7 @@ export const query = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "query"); }; diff --git a/src/modules/summarise.ts b/src/modules/summarise.ts index c58b84d..6b2d824 100644 --- a/src/modules/summarise.ts +++ b/src/modules/summarise.ts @@ -6,6 +6,7 @@ import { MessageFlags, type ChatInputCommandInteraction } from "discord.js"; import { personality } from "../config/personality.js"; import { ai } from "../utils/ai.js"; +import { calculateCost } from "../utils/calculateCost.js"; import { isSubscribed } from "../utils/isSubscribed.js"; /** @@ -41,4 +42,7 @@ export const summarise = async( response?.text ?? "I'm sorry, I don't have an answer for that. Please try again later.", ); + + const { usage } = messages; + await calculateCost(usage, interaction.user.username, "summarise"); }; diff --git a/src/utils/calculateCost.ts b/src/utils/calculateCost.ts new file mode 100644 index 0000000..2d90245 --- /dev/null +++ b/src/utils/calculateCost.ts @@ -0,0 +1,30 @@ +/** + * @copyright nhcarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ +import { logger } from "./logger.js"; +import type { Usage } from "@anthropic-ai/sdk/resources/index.js"; + +/** + * Calculates the cost of a command run by a user, and sends to + * our logging service. + * @param usage -- The usage payload from Anthropic. + * @param uuid -- The Discord ID of the user who ran the command. + * @param command -- The command that was run. + */ +export const calculateCost = async( + usage: Usage, + uuid: string, + command: string, +): Promise => { + const inputCost = usage.input_tokens * (3 / 1_000_000); + const outputCost = usage.output_tokens * (15 / 1_000_000); + const totalCost = inputCost + outputCost; + await logger.log( + "info", + `User ${uuid} ran \`${command}\` which accepted ${usage.input_tokens.toString()} and generated ${usage.output_tokens.toString()}. + + Total cost: ${totalCost.toLocaleString("en-GB", { currency: "USD", style: "currency" })}`, + ); +};