change file structure

This commit is contained in:
Timofey Gelazoniya 2023-09-10 21:27:48 +03:00
parent 2a6488488e
commit 1b6e2a57e9
Signed by: zeldon
GPG Key ID: 047886915281DD2A
6 changed files with 75 additions and 75 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules node_modules
.env .env
build

View File

@ -1,48 +1,27 @@
import { Bot as TelegramBot, BotConfig } from "grammy"; #!/usr/bin/env tsx
import { Context, createContextConstructor } from "#root/context.js";
import { logger } from "#root/logger.js"; import { onShutdown } from "node-graceful-shutdown";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode"; import { createBot } from "#root/bot.js";
import { config } from "#root/config.js"; import { config } from "#root/config.js";
import { autoChatAction } from "@grammyjs/auto-chat-action"; import { logger } from "#root/logger.js";
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 = { try {
config?: Omit<BotConfig<Context>, "ContextConstructor">; const bot = createBot(config.BOT_TOKEN);
};
export function createBot(token: string, options: Options = {}) { // Graceful shutdown
const bot = new TelegramBot(token, { onShutdown(async () => {
...options.config, logger.info("shutdown");
ContextConstructor: createContextConstructor({ logger }), await bot.stop();
}); });
bot.api.config.use(parseMode("html")); await bot.start({
onStart: ({ username }) =>
bot.use(ignoreOld()); logger.info({
msg: "bot running...",
if (config.isDev) { username,
bot.use(updateLogger()); }),
} });
} catch (error) {
bot.use(autoChatAction(bot.api)); logger.error(error);
bot.use(hydrateReply); process.exit(1);
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<typeof createBot>;

View File

@ -11,9 +11,9 @@
"clean": "rimraf build", "clean": "rimraf build",
"typecheck": "tsc", "typecheck": "tsc",
"build": "npm run clean && tsc --noEmit false", "build": "npm run clean && tsc --noEmit false",
"dev": "npm run clean && tsc-watch --onSuccess \"tsx ./scripts/start.ts\"", "dev": "npm run clean && tsc-watch --onSuccess \"tsx index.ts\"",
"start": "tsc && tsx ./scripts/start.ts", "start": "tsc && tsx index.ts",
"start:force": "tsx ./scripts/start.ts" "start:force": "tsx index.ts"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View File

@ -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);
}

48
src/bot.ts Normal file
View File

@ -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<BotConfig<Context>, "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<typeof createBot>;

View File

@ -18,8 +18,7 @@
} }
}, },
"include": [ "include": [
"index.ts",
"src/**/*", "src/**/*",
"scripts/**/*", "index.ts",
] ]
} }