Download src/middlewares/firstStartedBotMiddleware.ts from ItsMa8di/idbot: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/ItsMa8di/idbot/resolve/main/src/middlewares/firstStartedBotMiddleware.ts
- Command line
-
hf download hf://spaces/ItsMa8di/idbot/src/middlewares/firstStartedBotMiddleware.ts
-
curl -L -o firstStartedBotMiddleware.ts https://huggingface.co/spaces/ItsMa8di/idbot/resolve/main/src/middlewares/firstStartedBotMiddleware.ts
1.18 kB
| import { NextFunction } from "grammy"; | |
| import { BotContext } from "../../types"; | |
| import { UserController } from "../controllers/userController"; | |
| import { IUser } from "../models/User"; | |
| export default async function firstStartedBotMiddleware( | |
| ctx: BotContext, | |
| next: NextFunction, | |
| ) { | |
| try { | |
| // TODO: FETCH USER FROM THE DB AND IF DIDN'T EXISTS ADD IT TO IT | |
| const userid: number | undefined = ctx.from?.id; | |
| const username: string | undefined = ctx.from?.username; | |
| const firstname: string | undefined = ctx.from?.first_name; | |
| const lastname: string | undefined = ctx.from?.last_name; | |
| const isPremium: boolean = ctx.from?.is_premium as boolean; | |
| /////////////////////////////////////////// | |
| if (!userid) return; | |
| const user = await UserController.getByUserid(userid as number); | |
| if (!user) { | |
| // INSERT USER | |
| const newUser: IUser = { | |
| userid, | |
| username, | |
| firstname, | |
| lastname, | |
| isPremium, | |
| savedAccountTag: "", | |
| createdAt: new Date(), | |
| }; | |
| await UserController.create(newUser); | |
| } | |
| await next(); | |
| } catch (e) { | |
| console.error(e); | |
| return; | |
| } finally { | |
| } | |
| } | |