From 1b6e2a57e9dcb7206560b94fc51b4040c1498bec Mon Sep 17 00:00:00 2001 From: xzeldon Date: Sun, 10 Sep 2023 21:27:48 +0300 Subject: [PATCH] change file structure --- .gitignore | 3 ++- index.ts | 63 ++++++++++++++++-------------------------------- package.json | 6 ++--- scripts/start.ts | 27 --------------------- src/bot.ts | 48 ++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +-- 6 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 scripts/start.ts create mode 100644 src/bot.ts diff --git a/.gitignore b/.gitignore index 1dcef2d..ba81622 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.env \ No newline at end of file +.env +build \ No newline at end of file diff --git a/index.ts b/index.ts index e15cd66..e4c65b2 100644 --- a/index.ts +++ b/index.ts @@ -1,48 +1,27 @@ -import { Bot as TelegramBot, BotConfig } from "grammy"; -import { Context, createContextConstructor } from "#root/context.js"; -import { logger } from "#root/logger.js"; -import { hydrateReply, parseMode } from "@grammyjs/parse-mode"; +#!/usr/bin/env tsx + +import { onShutdown } from "node-graceful-shutdown"; +import { createBot } from "#root/bot.js"; import { config } from "#root/config.js"; -import { autoChatAction } from "@grammyjs/auto-chat-action"; -import { hydrate } from "@grammyjs/hydrate"; -import { updateLogger } from "#root/middlewares/index.js"; -import { errorHandler } from "#root/helpers/error.js"; -import { welcomeFeature } from "#root/handlers/welcome.js"; -import { imageFeature } from "#root/handlers/imagine/imagine.js"; -import { ignoreOld } from "#root/middlewares/ignore-old.js"; +import { logger } from "#root/logger.js"; -type Options = { - config?: Omit, "ContextConstructor">; -}; +try { + const bot = createBot(config.BOT_TOKEN); -export function createBot(token: string, options: Options = {}) { - const bot = new TelegramBot(token, { - ...options.config, - ContextConstructor: createContextConstructor({ logger }), + // Graceful shutdown + onShutdown(async () => { + logger.info("shutdown"); + await bot.stop(); }); - bot.api.config.use(parseMode("html")); - - bot.use(ignoreOld()); - - if (config.isDev) { - bot.use(updateLogger()); - } - - bot.use(autoChatAction(bot.api)); - bot.use(hydrateReply); - bot.use(hydrate()); - - // Handlers - bot.use(welcomeFeature); - bot.use(imageFeature); - - // Must be the last handler - if (config.isDev) { - bot.catch(errorHandler); - } - - return bot; + await bot.start({ + onStart: ({ username }) => + logger.info({ + msg: "bot running...", + username, + }), + }); +} catch (error) { + logger.error(error); + process.exit(1); } - -export type Bot = ReturnType; \ No newline at end of file diff --git a/package.json b/package.json index 865836d..09b21da 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,9 @@ "clean": "rimraf build", "typecheck": "tsc", "build": "npm run clean && tsc --noEmit false", - "dev": "npm run clean && tsc-watch --onSuccess \"tsx ./scripts/start.ts\"", - "start": "tsc && tsx ./scripts/start.ts", - "start:force": "tsx ./scripts/start.ts" + "dev": "npm run clean && tsc-watch --onSuccess \"tsx index.ts\"", + "start": "tsc && tsx index.ts", + "start:force": "tsx index.ts" }, "keywords": [], "author": "", diff --git a/scripts/start.ts b/scripts/start.ts deleted file mode 100644 index 2e54864..0000000 --- a/scripts/start.ts +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env tsx - -import { onShutdown } from "node-graceful-shutdown"; -import { createBot } from "../index.js"; -import { config } from "#root/config.js"; -import { logger } from "#root/logger.js"; - -try { - const bot = createBot(config.BOT_TOKEN); - - // Graceful shutdown - onShutdown(async () => { - logger.info("shutdown"); - await bot.stop(); - }); - - await bot.start({ - onStart: ({ username }) => - logger.info({ - msg: "bot running...", - username, - }), - }); -} catch (error) { - logger.error(error); - process.exit(1); -} diff --git a/src/bot.ts b/src/bot.ts new file mode 100644 index 0000000..e15cd66 --- /dev/null +++ b/src/bot.ts @@ -0,0 +1,48 @@ +import { Bot as TelegramBot, BotConfig } from "grammy"; +import { Context, createContextConstructor } from "#root/context.js"; +import { logger } from "#root/logger.js"; +import { hydrateReply, parseMode } from "@grammyjs/parse-mode"; +import { config } from "#root/config.js"; +import { autoChatAction } from "@grammyjs/auto-chat-action"; +import { hydrate } from "@grammyjs/hydrate"; +import { updateLogger } from "#root/middlewares/index.js"; +import { errorHandler } from "#root/helpers/error.js"; +import { welcomeFeature } from "#root/handlers/welcome.js"; +import { imageFeature } from "#root/handlers/imagine/imagine.js"; +import { ignoreOld } from "#root/middlewares/ignore-old.js"; + +type Options = { + config?: Omit, "ContextConstructor">; +}; + +export function createBot(token: string, options: Options = {}) { + const bot = new TelegramBot(token, { + ...options.config, + ContextConstructor: createContextConstructor({ logger }), + }); + + bot.api.config.use(parseMode("html")); + + bot.use(ignoreOld()); + + if (config.isDev) { + bot.use(updateLogger()); + } + + bot.use(autoChatAction(bot.api)); + bot.use(hydrateReply); + bot.use(hydrate()); + + // Handlers + bot.use(welcomeFeature); + bot.use(imageFeature); + + // Must be the last handler + if (config.isDev) { + bot.catch(errorHandler); + } + + return bot; +} + +export type Bot = ReturnType; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7561673..0a31f39 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,8 +18,7 @@ } }, "include": [ - "index.ts", "src/**/*", - "scripts/**/*", + "index.ts", ] } \ No newline at end of file