Skip to content

Commit

Permalink
Add /stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec committed Jun 22, 2024
1 parent 15af2e4 commit 92774eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bot/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./unhandled.js";
export * from "./reset.js";
export * from "./mint.js";
export * from "./start.js";
export * from "./stats.js";
export * from "./dice.js";
export * from "./whales.js";
export * from "./line.js";
Expand Down
23 changes: 23 additions & 0 deletions src/bot/features/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { isAdmin } from "#root/bot/filters/is-admin.js";
import { userStats } from "../models/user";

const composer = new Composer<Context>();

const feature = composer.chatType("private").filter(isAdmin);

feature.command("stats", logHandle("command-stats"), async (ctx) => {
const stats = await userStats();
return ctx.reply(`All: ${stats.all}
With wallet: ${stats.notMinted}
NFT minted: ${stats.minted}
Active month: ${stats.month}
Active week: ${stats.week}
Active day: ${stats.day}
`);
});

export { composer as statsFeature };
2 changes: 2 additions & 0 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
transactionFeature,
webappFeature,
addressesFeature,
statsFeature,
} from "#root/bot/features/index.js";
import { errorHandler } from "#root/bot/handlers/index.js";
import { i18n, isMultipleLocales } from "#root/bot/i18n.js";
Expand Down Expand Up @@ -80,6 +81,7 @@ export function createBot(token: string, options: Options) {
protectedBot.use(collectionFeature);
protectedBot.use(topupFeature);
protectedBot.use(adminFeature);
protectedBot.use(statsFeature);
protectedBot.use(whalesFeature);
protectedBot.use(lineFeature);
protectedBot.use(webappFeature);
Expand Down
21 changes: 21 additions & 0 deletions src/bot/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,24 @@ export async function addPoints(userId: number, add: bigint) {
throw error;
}
}

export async function userStats() {
const all = await UserModel.countDocuments();
const minted = await countUsers(true);
const notMinted = await countUsers(false);
const now = Date.now();
const dayMs = 24 * 60 * 60 * 1000;
const monthAgo = new Date(now - 30 * dayMs);
const weekAgo = new Date(now - 7 * dayMs);
const dayAgo = new Date(now - 1 * dayMs);
const month = await UserModel.countDocuments({
updatedAt: { $gte: monthAgo },
});
const week = await UserModel.countDocuments({
updatedAt: { $gte: weekAgo },
});
const day = await UserModel.countDocuments({
updatedAt: { $gte: dayAgo },
});
return { all, minted, notMinted, month, week, day };
}

0 comments on commit 92774eb

Please sign in to comment.