-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
87 lines (71 loc) · 1.72 KB
/
bot.js
File metadata and controls
87 lines (71 loc) · 1.72 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
78
79
80
81
82
83
84
85
86
87
const Telegraf = require("telegraf").Telegraf;
const db = require("./db");
const config = require("./config");
const logger = require("./logger");
const middlewares = require("./middlewares");
const commands = require("./commands");
const handlers = require("./handlers");
const callbacks = require("./callbacks");
module.exports = class Bot
{
constructor(token)
{
this.token = token;
this.username = null;
this.bot = new Telegraf(this.token);
this.bot.use(middlewares.update);
this.bot.start(commands.start);
for (let command of [
"on",
"off",
"onjoin",
"onleft",
"list",
"trigger",
"settings",
"edit"
]) {
this.bot.command(command, commands[command]);
}
this.bot.hears(/^\/(\d+)(@[_a-zA-Z0-9]+)?$/, commands.edit);
for (let event of [
"text",
"new_chat_members",
"left_chat_member"
]) {
this.bot.on(event, handlers[event]);
}
this.bot.action(/^lang:([^:]+)$/, callbacks.lang);
this.bot.action(/^settings:([^:]+)(:[^:]+)?$/, callbacks.settings);
this.bot.action(/^edit:(\d+)(:[^:]+)?(:[^:]+)?$/, callbacks.edit);
this.bot.action(/^auto_delete:(\d+):(\d+)$/, callbacks.auto_delete);
}
async start()
{
await db.start();
this.bot
.launch(config.bot.params)
.then(res => {
this.username = this.bot.botInfo.username;
logger.info(`Bot @${this.username} started.`);
})
.catch(err => {
logger.fatal(err);
process.exit(1);
});
}
async stop()
{
logger.info(`Stop the bot @${this.username}`);
await this.bot.stop();
await db.stop();
process.exit(0);
}
async reload()
{
logger.info(`Reload the bot @${this.username}`);
await this.bot.stop();
await db.stop();
await this.start();
}
}