-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.ts
223 lines (173 loc) · 6.57 KB
/
bot.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { Composer, Telegraf } from 'telegraf'
import { BotCommand, CallbackQuery, InlineQueryResultArticle } from 'typegram';
import { getCommands, getCallbacks, getInlines } from './loader';
import BaseCommand, { Command, Message } from './commands/_base';
import BaseCallback from './callbacks/_base';
import BaseInline from './inline/_base';
import { getUserPermissions, noPerm } from './permissions';
import { config } from './config';
export const tg = new Telegraf(config.tg_token)
/*
tg.command(['id', 'myid'], (ctx) => {
ctx.reply(`${ctx.from.id}`)
})
*/
/*tg.use((ctx, next) => {
if (ctx.from?.id == undefined) return;
if (![804594266, 1078771932, 1305235628].includes(ctx.from?.id)) {
if (ctx.updateType !== 'message') return;
return ctx.reply('403: access denied');
}
next()
})*/
export const searchId: {
[index: number]: string
} = {}
export const callbacks: {
[id: string]: BaseCallback
} = getCallbacks()
tg.on('callback_query', async (ctx) => {
if (ctx.updateType != 'callback_query') return;
if (ctx.callbackQuery.message == undefined) return;
const data: (CallbackQuery & { data?: string }) = ctx.callbackQuery
if (data.data === undefined) return;
const id = data.data.split('|')[0]
if (id === undefined) return;
const cb = callbacks[id]
if (cb === undefined) return;
if (cb.permission === null) return ctx.reply('Для данной команды ещё не настроены права')
const uPerms = getUserPermissions(data.from.id)
if (cb.permission !== true && !uPerms.permissions.includes(cb.permission))
return noPerm(ctx, cb.permission, 'action');
console.log(data.data);
(async () => {
const setList: {
setTimeouts: NodeJS.Timeout[],
setIntervals: NodeJS.Timeout[]
} = {
setTimeouts: [],
setIntervals: []
}
try {
await cb.handler({ tg, ctx, setList, payload: data.data! })
} catch (e: any) {
console.error(id, e)
ctx.reply(`Произошла ошибка во время выполнения Callback #${id}: ${e.toString()}`)
}
for (const timeout of setList.setTimeouts) clearTimeout(timeout)
for (const interval of setList.setIntervals) clearInterval(interval)
})();
return;
})
export const cmds: {
[id: string]: BaseCommand
} = getCommands()
tg.on('text', async (ctx) => {
let message: string = ctx.message.text
const isCommand: boolean = message.startsWith('/')
for (const id in cmds) {
const cmd = cmds[id]
for (const icmd of isCommand ? cmd.commands : cmd.messages) {
let command: string = isCommand ? message.substr(1).replace(new RegExp(`@${tg.botInfo?.username}`, 'i'),'') : message
let sysCommand: string = isCommand ? (icmd as Command).command : (icmd as Message).message
if (!icmd.caseSensitive) {
command = command.toLowerCase()
sysCommand = sysCommand.toLowerCase()
}
if (icmd.startsWith) {
command = command.split(' ')[0]
}
if (command != sysCommand) continue;
if (cmd.permission === null) return ctx.reply('Для данной команды ещё не настроены права')
const uPerms = getUserPermissions(ctx.message.from.id)
if (cmd.permission !== true && !uPerms.permissions.includes(cmd.permission)) return ctx.reply(
`Ваш аккаунт или ваша группа "${uPerms.group}" не имеет разрешения "${cmd.permission}".`
);
console.log(command);
(async () => {
const setList: {
setTimeouts: NodeJS.Timeout[],
setIntervals: NodeJS.Timeout[]
} = {
setTimeouts: [],
setIntervals: []
}
try {
await cmd.handler({ tg, ctx, setList })
} catch (e: any) {
console.error(id, e)
ctx.reply(`Произошла ошибка во время выполнения Command #${id}: ${e.toString()}`)
}
for (const timeout of setList.setTimeouts) clearTimeout(timeout)
for (const interval of setList.setIntervals) clearInterval(interval)
})()
return;
}
}
//console.log(cmds)
})
/*
const inlines: {
[id: string]: BaseInline
} = getInlines()
tg.on('inline_query', async (ctx) => {
const message = ctx.inlineQuery.query
if (message === '') {
const results: InlineQueryResultArticle[] = []
for (const id in inlines) {
const inline = inlines[id]
for (const article of inline.articles) {
if (!article.registerTG) continue;
results.push({
type: 'article',
id: `${id}|${article.text}`,
title: `${article.text} - ${article.description}`,
input_message_content: {
message_text: 'Метод не введён'
}
})
}
}
return ctx.answerInlineQuery(results)
}
for (const id in inlines) {
const inline = inlines[id]
for (const article of inline.articles) {
let sysMethod: string = article.text
let uMethod: string = message
if (!article.caseSensitive) {
sysMethod = sysMethod.toLowerCase()
uMethod = uMethod.toLowerCase()
}
if (article.startsWith) {
uMethod = uMethod.split(' ')[0]
}
if (sysMethod != uMethod) continue;
await inline.handler({ tg, ctx })
return;
}
}
})*/
async function init(): Promise<void> {
console.log('Starting bot...')
console.log('Setting commands...')
const commands: BotCommand[] = []
for (const id in cmds) {
const cmd = cmds[id]
for (const icmd of cmd.commands) {
if (!icmd.registerTG) continue;
commands.push({
command: icmd.command.toLowerCase(),
description: icmd.description
})
}
}
await tg.telegram.setMyCommands(commands)
console.log('Start listen updates...')
await tg.launch().then(async () => {
console.log('Bot started')
}, (err) => {
console.log('error:', err)
})
}
init()