-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (91 loc) · 3.08 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import config from './config.js';
import Telegram from 'node-telegram-bot-api';
import JsonDB from './database.js';
import Cron from './cron.js';
import { ping, start, id, help, commit, status } from './commands/utils.js';
import { book, bookings, removebooking, copyfromlastweek } from './commands/bookings.js';
import { setname, adduser, addfirstuser, removeuser, users, disablereminder, enablereminder, setremindertime } from './commands/users.js';
import { previeweeklymessage, setannouncementchannel, sendweeklymessage, setweeklymessagetime, forceupdateweeklymessage } from './commands/weeklyMessage.js';
export const chatState = {};
export const client = new Telegram(config.telegramApiKey, {polling: true});
export const db = new JsonDB({ path: './db.json' });
export const cron = new Cron();
export const commands = {
// utils
ping,
start,
id,
help,
commit,
status,
// bookings
book,
bookings,
removebooking,
copyfromlastweek,
// users
setname,
adduser,
addfirstuser,
removeuser,
users,
disablereminder,
enablereminder,
setremindertime,
// weekly message
previeweeklymessage,
setannouncementchannel,
sendweeklymessage,
setweeklymessagetime,
forceupdateweeklymessage,
}
client.on('message', async (msg) => {
if (msg.from.is_bot) return;
let isPrivate = msg.chat.type == 'private';
let user = db.getUser(msg.from.id);
if (user != undefined && user.username == undefined) {
db.updateUser(msg.from.id, {username: msg.from.username, name: msg.from.first_name});
}
if (chatState[msg.chat.id] != undefined) {
if (msg.text == 'cancel' || msg.text == '/cancel') {
delete chatState[msg.chat.id];
client.sendMessage(msg.chat.id, 'Operation cancelled!', {message_thread_id: msg.message_thread_id});
return;
} else {
commands[chatState[msg.chat.id].command].execute(msg);
}
} else {
if (!msg.text || msg.text.indexOf('/') !== 0) return;
const args = msg.text.slice(1).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmdHandler = commands[command];
if (cmdHandler != undefined) {
if (cmdHandler.requireAdmin && user == undefined) {
if (isPrivate) {
client.sendMessage(msg.chat.id, 'You are not authorized to use this command!', {message_thread_id: msg.message_thread_id});
return;
} // else ignore, might be trying to use another bot
}
if (isPrivate && !cmdHandler.canRunPrivate) {
client.sendMessage(msg.chat.id, 'This command cannot be run in private!', {message_thread_id: msg.message_thread_id});
return;
}
if (!isPrivate && !cmdHandler.canRunPublic) {
client.sendMessage(msg.chat.id, 'This command cannot be run in public!', {message_thread_id: msg.message_thread_id});
return;
}
cmdHandler.execute(msg, args);
}
}
});
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err + '\n' + err.stack);
});
// disabled for now cause it makes the bot never shut down
/*process.on('SIGINT', () => { // attempt to save the database when the process is terminated
db.save();
client.close();
});*/
client.getMyName().then(name => {
console.log('Logged in as ' + name.name);
});