forked from lulivi/mattata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
412 lines (380 loc) · 13.4 KB
/
utils.lua
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
local utils = {}
local redis = require('mattata-redis')
local configuration = require('configuration')
local mattata = {}
local api = {}
local tools = {}
function utils:init(configuration, token)
mattata = self
api = self.api
tools = self.tools
return utils
end
function utils.is_trusted_user(chat_id, user_id)
if redis:sismember('administration:' .. chat_id .. ':trusted', user_id) then
return true
end
return false
end
function utils.service_message(message)
if message.new_chat_member then
return true, 'new_chat_member'
elseif message.left_chat_member then
return true, 'left_chat_member'
elseif message.new_chat_title then
return true, 'new_chat_title'
elseif message.new_chat_photo then
return true, 'new_chat_photo'
elseif message.delete_chat_photo then
return true, 'delete_chat_photo'
elseif message.group_chat_created then
return true, 'group_chat_created'
elseif message.supergroup_chat_created then
return true, 'supergroup_chat_created'
elseif message.channel_chat_created then
return true, 'channel_chat_created'
elseif message.migrate_to_chat_id then
return true, 'migrate_to_chat_id'
elseif message.migrate_from_chat_id then
return true, 'migrate_from_chat_id'
elseif message.pinned_message then
return true, 'pinned_message'
elseif message.successful_payment then
return true, 'successful_payment'
end
return false
end
function utils.is_media(message)
if message.audio or message.document or message.game or message.photo or message.sticker or message.video or message.voice or message.video_note or message.contact or message.location or message.venue or message.invoice then
return true
end
return false
end
function utils.media_type(message)
if message.audio then
return 'audio'
elseif message.document then
return 'document'
elseif message.game then
return 'game'
elseif message.photo then
return 'photo'
elseif message.sticker then
return 'sticker'
elseif message.video then
return 'video'
elseif message.voice then
return 'voice'
elseif message.video_note then
return 'video note'
elseif message.contact then
return 'contact'
elseif message.location then
return 'location'
elseif message.venue then
return 'venue'
elseif message.invoice then
return 'invoice'
elseif message.forward_from or message.forward_from_chat then
return 'forwarded'
elseif message.text then
return message.text:match('[\216-\219][\128-\191]') and 'rtl' or 'text'
end
return ''
end
function utils.file_id(message)
if message.audio then
return message.audio.file_id
elseif message.document then
return message.document.file_id
elseif message.sticker then
return message.sticker.file_id
elseif message.video then
return message.video.file_id
elseif message.voice then
return message.voice.file_id
elseif message.video_note then
return message.video_note.file_id
end
return ''
end
function utils.get_user_count()
return #redis:keys('user:*:info')
end
function utils.get_group_count()
return #redis:keys('chat:*:info')
end
function utils.clear_broadcast_memory()
local broadcasts = redis:keys('broadcasted:*')
for k, v in pairs(broadcasts) do
if redis:get(v) then
redis:del(v)
end
end
end
function utils.get_user_language(user_id)
return redis:hget('chat:' .. user_id .. ':settings', 'language') or 'en_gb'
end
function utils.get_log_chat(chat_id)
return redis:hget('chat:' .. chat_id .. ':settings', 'log chat') or configuration.log_channel or false
end
function utils.get_missing_languages(delimiter)
local missing_languages = redis:smembers('mattata:missing_languages')
if not missing_languages then
return false
end
local output = {}
for k, v in pairs(missing_languages) do
table.insert(output, v)
end
local delimiter = delimiter or ', '
return table.concat(output, delimiter)
end
function utils.purge_user(user)
if type(user) ~= 'table' then
return false
end
user = user.from or user
redis:hdel('user:' .. user.id .. ':info', 'id')
if user.username or redis:hget('user:' .. user.id .. ':info', 'username') then
redis:hdel('user:' .. user.id .. ':info', 'username')
local all = redis:smembers('user:' .. user.id .. ':usernames')
for k, v in pairs(all) do
redis:srem('user:' .. user.id .. ':usernames', v)
end
redis:del('username:' .. user.id)
end
redis:hdel('user:' .. user.id .. ':info', 'first_name')
if user.name or redis:hget('user:' .. user.id .. ':info', 'name') then
redis:hdel('user:' .. user.id .. ':info', 'name')
end
if user.last_name or redis:hget('user:' .. user.id .. ':info', 'last_name') then
redis:hdel('user:' .. user.id .. ':info', 'last_name')
end
if user.language_code or redis:hget('user:' .. user.id .. ':info', 'language_code') then
redis:hdel('user:' .. user.id .. ':info', 'language_code')
end
return true
end
function utils.get_list(name)
name = tostring(name)
local length = redis:llen(name)
return redis:lrange(name, 0, tonumber(length) - 1)
end
function utils.get_inline_help(input, offset)
offset = offset and tonumber(offset) or 0
local inline_help = {}
local count = offset + 1
table.sort(mattata.plugin_list)
for k, v in pairs(mattata.plugin_list) do
-- The bot API only accepts a maximum of 50 results, hence we need the offset.
if k > offset and k < offset + 50 then
v = v:gsub('\n', ' ')
if v:match('^/.- %- .-$') and v:lower():match(input) then
table.insert(inline_help,
{
['type'] = 'article',
['id'] = tostring(count),
['title'] = v:match('^(/.-) %- .-$'),
['description'] = v:match('^/.- %- (.-)$'),
['input_message_content'] = {
['message_text'] = utf8.char(8226) .. ' ' .. v:match('^(/.-) %- .-$') .. ' - ' .. v:match('^/.- %- (.-)$')
}
})
count = count + 1
end
end
end
return inline_help
end
function utils.send_reply(message, text, parse_mode, disable_web_page_preview, reply_markup, token)
reply_markup = reply_markup or {
['remove_keyboard'] = true
}
parse_mode = tostring(parse_mode):lower()
if parse_mode ~= 'markdown' and parse_mode ~= 'html' then
parse_mode = nil
end
return mattata.api.send_message(message, text, parse_mode, disable_web_page_preview, false, message.message_id, reply_markup, token)
end
function utils.toggle_user_setting(chat_id, user_id, setting)
if not chat_id or not user_id or not setting then
return false
elseif not redis:hexists('user:' .. user_id .. ':' .. chat_id .. ':settings', tostring(setting)) then
local success = false
if setting == 'restrict messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), false)
elseif setting == 'restrict media messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, false)
elseif setting == 'restrict other messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, nil, false)
elseif setting == 'restrict web page previews' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, nil, nil, false)
end
if success then
if setting == 'restrict messages' then
redis:hset('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict media messages', true)
redis:hset('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict other messages', true)
redis:hset('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict web page previews', true)
end
redis:hset('user:' .. user_id .. ':' .. chat_id .. ':settings', tostring(setting), true)
end
return success
end
local success = false
if setting == 'restrict messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), true)
elseif setting == 'restrict media messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, true)
elseif setting == 'restrict other messages' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, nil, true)
elseif setting == 'restrict web page previews' then
success = mattata.api.restrict_chat_member(chat_id, user_id, os.time(), nil, nil, nil, true)
end
if success then
if setting == 'restrict messages' then
redis:hdel('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict media messages')
redis:hdel('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict other messages')
redis:hdel('user:' .. user_id .. ':' .. chat_id .. ':settings', 'restrict web page previews')
end
return redis:hdel('user:' .. user_id .. ':' .. chat_id .. ':settings', tostring(setting))
end
return success
end
function utils.get_user_setting(chat_id, user_id, setting)
if not chat_id or not user_id or not setting then
return false
elseif not redis:hexists('user:' .. user_id .. ':' .. chat_id .. ':settings', tostring(setting)) then
return false
end
return true
end
function utils.is_group(message)
if not message or not message.chat or not message.chat.type or message.chat.type == 'private' then
return false
end
return true
end
function utils.get_user_message_statistics(user_id, chat_id)
return {
['messages'] = tonumber(redis:get('messages:' .. user_id .. ':' .. chat_id)) or 0,
['name'] = redis:hget('user:' .. user_id .. ':info', 'first_name'),
['id'] = user_id
}
end
function utils.reset_message_statistics(chat_id)
if not chat_id or tonumber(chat_id) == nil then
return false
end
local messages = redis:keys('messages:*:' .. chat_id)
if not next(messages) then
return false
end
for k, v in pairs(messages) do
redis:del(v)
end
return true
end
function utils.input(s)
local mentioned_user = false
if not s then
return false
elseif type(s) == 'table' then
if s.entities and #s.entities >= 2 and s.entities[2].type == 'text_mention' then
mentioned_user = s.entities[2].user.id
end
s = s.text
end
if s:lower():match('^mattata search %a+ for .-$') then
return s:lower():match('^mattata search %a+ for (.-)$')
elseif not s:lower():match('^[%%/%%!%%$%%^%%?%%&%%%%]') then
return s
end
local input = s:find(' ')
if not input then
return false
end
s = s:sub(input + 1)
input = s:find(' ')
if mentioned_user then
s = input and mentioned_user .. ' ' .. s:sub(input + 1) or mentioned_user
end
return s
end
function utils.get_input(message, has_reason)
local input = utils.input(message)
if message.reply then
if not message.reply.from or message.reply.forward_from then
return false
elseif has_reason and input then
return message.reply.from.id, input
end
return message.reply.from.id
elseif not input then
return false
elseif has_reason and input:find(' ') then
return input:match('^(.-) '), input:match(' (.-)$')
end
return input
end
function utils.get_chat_id(chat)
if not chat then
return false
end
local success = api.get_chat(chat)
if not success or not success.result then
return false
end
return success.result.id
end
function utils.get_setting(chat_id, setting)
if not chat_id or not setting then
return false
end
return redis:hget('chat:' .. chat_id .. ':settings', tostring(setting))
end
function utils.get_value(chat_id, value)
if not chat_id or not value then
return false
end
return redis:hget('chat:' .. chat_id .. ':values', tostring(value))
end
function utils.log_error(error_message)
error_message = tostring(error_message):gsub('%%', '%%%%')
local output = string.format('%s[31m[Error] %s%s[0m', string.char(27), error_message, string.char(27))
print(output)
end
function utils.write_file(file_path, content)
file_path = tostring(file_path)
content = tostring(content)
local file = io.open(file_path, 'w+')
file:write(content)
file:close()
return file
end
_G.table.contains = function(tab, match)
for _, val in pairs(tab) do
if val == match then
return true
end
end
return false
end
_G.table.random = function(tab, seed)
if seed and tonumber(seed) ~= nil then
math.randomseed(seed)
end
tab = type(tab) == 'table' and tab or { tostring(tab) }
local total = 0
for key, chance in pairs(tab) do
total = total + chance
end
local choice = math.random() * total
for key, chance in pairs(tab) do
choice = choice - chance
if choice < 0 then
return key
end
end
end
return utils