diff --git a/locales/en.ftl b/locales/en.ftl index 09fb4d5..beb6e00 100644 --- a/locales/en.ftl +++ b/locales/en.ftl @@ -26,4 +26,7 @@ setting-attribution-0 = nah setting-attribution-1 = sure setting-lang = language -setting-lang-unset = same as telegram \ No newline at end of file +setting-lang-unset = same as telegram + +stats-personal = i helped you with downloading { $count } times! (˶ᵔ ᵕ ᵔ˶) +stats-global = i helped with downloading { $count } times! (˶ᵔ ᵕ ᵔ˶) \ No newline at end of file diff --git a/locales/ru.ftl b/locales/ru.ftl index 0fc08b4..a04214f 100644 --- a/locales/ru.ftl +++ b/locales/ru.ftl @@ -26,4 +26,7 @@ setting-attribution-0 = не setting-attribution-1 = давай setting-lang = язык -setting-lang-unset = как в тг \ No newline at end of file +setting-lang-unset = как в тг + +stats-personal = я помог тебе с загрузкой { $count } раз! (˶ᵔ ᵕ ᵔ˶) +stats-global = я помог с загрузкой { $count } раз! (˶ᵔ ᵕ ᵔ˶) \ No newline at end of file diff --git a/migrations/0002_dry_screwball.sql b/migrations/0002_dry_screwball.sql new file mode 100644 index 0000000..05eb72a --- /dev/null +++ b/migrations/0002_dry_screwball.sql @@ -0,0 +1 @@ +ALTER TABLE users ADD `downloads` integer; \ No newline at end of file diff --git a/migrations/meta/0002_snapshot.json b/migrations/meta/0002_snapshot.json new file mode 100644 index 0000000..c30e870 --- /dev/null +++ b/migrations/meta/0002_snapshot.json @@ -0,0 +1,97 @@ +{ + "version": "5", + "dialect": "sqlite", + "id": "7cf9cdab-b059-4b49-b8ff-c3b620bc0151", + "prevId": "e6eab90c-c703-4ac7-907f-2ad96aedb224", + "tables": { + "requests": { + "name": "requests", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "author_id": { + "name": "author_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "users": { + "name": "users", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "output": { + "name": "output", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "attribution": { + "name": "attribution", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "language": { + "name": "language", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "downloads": { + "name": "downloads", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "users_id_unique": { + "name": "users_id_unique", + "columns": [ + "id" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} \ No newline at end of file diff --git a/migrations/meta/_journal.json b/migrations/meta/_journal.json index 65a6eb2..ad23d98 100644 --- a/migrations/meta/_journal.json +++ b/migrations/meta/_journal.json @@ -15,6 +15,13 @@ "when": 1708695593228, "tag": "0001_giant_vin_gonzales", "breakpoints": true + }, + { + "idx": 2, + "version": "5", + "when": 1718465870677, + "tag": "0002_dry_screwball", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/db/schema.ts b/src/db/schema.ts index a9ac55c..80be902 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -11,4 +11,5 @@ export const users = sqliteTable("users", { preferredOutput: text("output"), preferredAttribution: int("attribution").notNull().default(0), languageOverride: text("language"), + downloadCount: int("downloads"), }) diff --git a/src/index.ts b/src/index.ts index 1977be7..6cd1a64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import { settingsMiddleware, updateSetting, } from "#settings" +import { getDownloadStats, incrementDownloadCount } from "#stats" type CoboldContext = Context & I18nFlavor & TextFlavor & SettingsFlavor const bot = new Bot(env.BOT_TOKEN) @@ -53,6 +54,18 @@ const settingsReplyMarkup = (ctx: CoboldContext, settingsOverride?: Settings): I ])), }) +bot.command("stats", async (ctx) => { + const count = await getDownloadStats() + await ctx.reply(ctx.t("stats-global", { count })) +}) + +bot.command("mystats", async (ctx) => { + const id = ctx.from?.id + if (!id) return + const count = await getDownloadStats(id) + await ctx.reply(ctx.t("stats-personal", { count })) +}) + bot.command("settings", async (ctx) => { await ctx.reply(ctx.t("settings-title"), { reply_markup: settingsReplyMarkup(ctx), @@ -145,6 +158,9 @@ const onOutputSelected = async ( caption: ctx.t("error", { message: ctx.evaluateText(result.error) }), }) + const fromId = ctx.from?.id + if (fromId) incrementDownloadCount(fromId).catch(() => { /* noop */ }) + await ctx.editMessageCaption({ caption: ctx.t("uploading-title"), }) diff --git a/src/settings.ts b/src/settings.ts index 9a2e5d4..2bf3151 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -8,7 +8,7 @@ import { literal, translatable } from "#text" import { I18nFlavor } from "@grammyjs/i18n" export const settingCallbackPrefix = "setting" -export type Settings = Omit, "id"> +export type Settings = Omit, "id" | "downloadCount"> export type SettingsFlavor = { userSettings: Settings, } diff --git a/src/stats.ts b/src/stats.ts new file mode 100644 index 0000000..cf905c9 --- /dev/null +++ b/src/stats.ts @@ -0,0 +1,18 @@ +import { db } from "#db/database" +import { users } from "#db/schema" +import { eq, sql, sum } from "drizzle-orm" + +export async function incrementDownloadCount(user: number) { + await db.insert(users) + .values({ id: user, downloadCount: 1 }) + .onConflictDoUpdate({ target: users.id, set: { downloadCount: sql`${users.downloadCount} + 1` } }) + .returning() +} + +export async function getDownloadStats(user?: number) { + const res = await db + .select({ downloadCount: sum(users.downloadCount) }) + .from(users) + .where(user !== undefined ? eq(users.id, user) : undefined) + return res.reduce((p, c) => p + +(c.downloadCount || ""), 0) +}