-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 1.91 KB
/
index.js
File metadata and controls
77 lines (64 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { Bot, GrammyError, HttpError } = require("grammy");
const { autoQuote } = require("@roziscoding/grammy-autoquote");
const fs = require("fs");
const path = require("path");
if (fs.existsSync(".env")) {
require("dotenv").config();
}
const botToken = process.env.BOT_TOKEN;
if (!botToken) {
throw new Error("BOT_TOKEN is not set in environment variables! Exiting...");
}
async function start() {
const bot = new Bot(botToken);
bot.use(autoQuote);
const commandFilesDir = path.resolve(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandFilesDir)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(path.join(commandFilesDir, file));
bot.command(command.name, async (ctx) => {
await command.handler(ctx);
});
if (command.alias) {
for (const alias of command.alias) {
bot.command(alias, async (ctx) => {
await command.handler(ctx);
});
}
}
}
bot.command("start", (ctx) =>
ctx.reply("Hello!\n\n" + "Run the /help command to see what I can do!")
);
bot.catch((err) => {
const ctx = err.ctx;
console.error(`Error while handling update ${ctx.update.update_id}:`);
const e = err.error;
if (e instanceof GrammyError) {
console.error("Error in request:", e.description);
} else if (e instanceof HttpError) {
console.error("Could not contact Telegram:", e);
} else {
console.error("Unknown error:", e);
}
});
process.on("uncaughtException", (err) => {
console.error(err);
});
process.on("unhandledRejection", (err) => {
console.error(err);
});
process.on("SIGINT", () => {
console.log("Stopping...");
bot.stop();
process.exit(0);
});
console.log("Starting the bot...");
await bot.start();
}
start().catch((error) => {
console.error("Error occurred during bot startup:", error);
process.exit(1);
});