-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanimeultrabot.js
208 lines (178 loc) · 6.55 KB
/
animeultrabot.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { createReadStream } from 'fs';
import { Telegraf } from 'telegraf';
import IS_DEV from './util/is-dev.js';
import LogMessageOrError from './util/log.js';
import { GetUsername } from './util/common.js';
import { MarkSentPost } from './util/marking-posts.js';
import { LoadTelegramConfig } from './util/load-configs.js';
import CheckCommandAvailability from './util/check-command-availability.js';
import CheckMessageForLinks from './util/check-message-for-links.js';
import DeleteCommand from './commands/delete-command.js';
import KhaleesiCommand from './commands/khaleesi-command.js';
import ChebotarbCommand from './commands/chebotarb-command.js';
import LoadCommand from './commands/load-command.js';
import SpoilerCommand from './commands/spoiler-command.js';
import SendingWrapper from './util/sending-wrapper.js';
const TELEGRAM_CONFIG = LoadTelegramConfig();
const { BOT_TOKEN, BOT_USERNAME, CHATS_LIST, SPECIAL_PHRASE, LOCAL_BOT_API_SERVER } = TELEGRAM_CONFIG;
const telegraf = new Telegraf(
BOT_TOKEN,
IS_DEV
? {}
: {
telegram: {
apiRoot: `http://${LOCAL_BOT_API_SERVER.hostname}:${LOCAL_BOT_API_SERVER.port}`,
},
}
);
const { telegram } = telegraf;
/** @type {import('./types/commands').CommandsStorage} */
const COMMANDS = {
help: LoadCommand('help', TELEGRAM_CONFIG),
start: LoadCommand('help', TELEGRAM_CONFIG),
aboutpicker: LoadCommand('aboutpicker', TELEGRAM_CONFIG),
aboutlist: LoadCommand('aboutlist', TELEGRAM_CONFIG),
aboutspoiler: LoadCommand('aboutspoiler', TELEGRAM_CONFIG),
aboutdelete: LoadCommand('aboutdelete', TELEGRAM_CONFIG),
delete: (ctx) => DeleteCommand(ctx),
spoiler: (ctx) => SpoilerCommand(ctx),
khaleesi: (ctx) => KhaleesiCommand(ctx),
chebotarb: (ctx) => ChebotarbCommand(ctx, telegram),
testcommand: `I'm just a test command that returns string`,
};
/**
* @param {import('./types/telegraf').NewMemberContext} ctx
* @returns {void}
*/
const HandleNewChatMembers = (ctx) => {
const { chat, message } = ctx;
if (chat.type === 'private') return;
const knownChat = CHATS_LIST.find((chatFromConfig) => chatFromConfig.id === chat.id);
if (!knownChat) {
LogMessageOrError(`New group. ID: ${chat.id}, title: ${chat.title}, type: ${chat.type}`);
return;
}
if (!knownChat.enabled) return;
const { welcome } = knownChat;
if (!welcome) return;
if (welcome.type === 'text') {
ctx
.sendMessage(
welcome.message.replace('__USERNAME__', GetUsername(message.new_chat_member || message.new_chat_members[0])),
{
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_to_message_id: message.message_id,
allow_sending_without_reply: true,
disable_notification: true,
}
)
.catch(LogMessageOrError);
} else if (welcome.type === 'gif') {
ctx
.sendAnimation(
welcome.message.filename ? { source: createReadStream(welcome.message.filename) } : welcome.message.file_id,
{
caption: welcome.message.caption
? welcome.message.caption.replace(
'__USERNAME__',
GetUsername(message.new_chat_member || message.new_chat_members[0])
)
: '',
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_to_message_id: message.message_id,
allow_sending_without_reply: true,
disable_notification: true,
}
)
.catch(LogMessageOrError);
}
};
/**
* @param {import('./types/telegraf').DefaultContext} ctx
* @returns {void}
*/
const HandleTextOrCaptionable = (ctx) => {
const { chat, from, message } = ctx;
const textMessage = 'text' in message;
const mediaMessage = 'caption' in message;
const text = (textMessage ? message.text : mediaMessage ? message.caption : '').trim();
const knownChat = CHATS_LIST.find((chatFromConfig) => chatFromConfig.id === chat.id);
if (chat.type !== 'private') {
if (!knownChat) return;
if (!knownChat.enabled) return;
MarkSentPost(message, from.id, false);
}
if (!text) return;
const commandMatch = text.match(
new RegExp(`^/(?<commandName>\\w+)(?:@${BOT_USERNAME})${chat.type === 'private' ? '?' : ''}\\b`, 'i')
);
/** @type {import('./types/commands').CommandName} */
const commandName = commandMatch?.groups?.commandName || '';
const commandAction = COMMANDS[commandName];
if (commandAction) {
if (!CheckCommandAvailability(from, commandName)) return;
if (typeof commandAction === 'string') {
SendingWrapper(() =>
ctx.sendMessage(commandAction, {
disable_web_page_preview: true,
parse_mode: 'HTML',
})
).catch(LogMessageOrError);
return;
}
if (chat.type === 'private') return;
if (typeof commandAction === 'function') {
commandAction(ctx);
return;
}
}
if (chat.type === 'private') return;
if (new RegExp(SPECIAL_PHRASE.regexp, 'i').test(text)) {
if (!CheckCommandAvailability(from)) return;
const chance = Math.random();
if (chance > 1 / 2) return;
if (chance < 1 / 8)
ctx
.sendMessage('<i>…как Орлов, порхай как бабочка!</i>', {
parse_mode: 'HTML',
reply_to_message_id: message.message_id,
allow_sending_without_reply: true,
disable_notification: true,
})
.catch(LogMessageOrError);
else if (chance < 1 / 4)
ctx
.sendSticker(SPECIAL_PHRASE.sticker, {
reply_to_message_id: message.message_id,
allow_sending_without_reply: true,
disable_notification: true,
})
.catch(LogMessageOrError);
else
ctx
.sendAnimation(SPECIAL_PHRASE.gif, {
reply_to_message_id: message.message_id,
allow_sending_without_reply: true,
disable_notification: true,
})
.catch(LogMessageOrError);
return;
}
CheckMessageForLinks(ctx, message, !mediaMessage);
};
const botStartedTime = Date.now();
telegraf.on('message', (ctx) => {
if (Date.now() - botStartedTime < 1000 * 15 && !IS_DEV) return;
const { message } = ctx;
if ('new_chat_members' in message) HandleNewChatMembers(ctx);
if ('text' in message || 'photo' in message || 'video' in message || 'animation' in message || 'caption' in message)
HandleTextOrCaptionable(ctx);
});
telegraf.launch();
process.on('unhandledRejection', (reason, promise) =>
LogMessageOrError('Unhandled Rejection at: Promise', promise, 'reason:', reason)
);
process.once('SIGINT', () => telegraf.stop('SIGINT'));
process.once('SIGTERM', () => telegraf.stop('SIGTERM'));