change file structure
This commit is contained in:
parent
2a6488488e
commit
1b6e2a57e9
|
@ -1,2 +1,3 @@
|
|||
node_modules
|
||||
.env
|
||||
.env
|
||||
build
|
63
index.ts
63
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<BotConfig<Context>, "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<typeof createBot>;
|
|
@ -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": "",
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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>;
|
|
@ -18,8 +18,7 @@
|
|||
}
|
||||
},
|
||||
"include": [
|
||||
"index.ts",
|
||||
"src/**/*",
|
||||
"scripts/**/*",
|
||||
"index.ts",
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue