1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from telegram.ext import MessageHandler, Filters |
4
|
|
|
from datetime import datetime, timedelta |
5
|
|
|
import sqlite3 |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def module_init(gd): |
9
|
|
|
global c, conn |
10
|
|
|
path = gd.config["path"] |
11
|
|
|
conn = sqlite3.connect(path+"rikka.db", check_same_thread=False) |
12
|
|
|
c = conn.cursor() |
13
|
|
|
gd.dp.add_handler(MessageHandler(Filters.all, get_chats), group=1) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def create_table(name, columns): |
17
|
|
|
c.execute("CREATE TABLE IF NOT EXISTS "+name+"("+columns+")") |
18
|
|
|
conn.commit() |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def data_entry(table, entry_columns, values): |
22
|
|
|
values_count = ("?, "*len(values))[:-2] |
23
|
|
|
c.execute("INSERT INTO "+table+" ("+entry_columns+") VALUES ("+values_count+")", (values)) |
24
|
|
|
conn.commit() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def check_entry(chat_id, table): |
28
|
|
|
c.execute("SELECT chat_id FROM "+table+" WHERE chat_id = %s" %(chat_id)) |
29
|
|
|
if c.fetchone() is not None: |
30
|
|
|
return True |
31
|
|
|
else: |
32
|
|
|
return False |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def delete_old(table, date): |
36
|
|
|
span = timedelta(days=7) |
37
|
|
|
c.execute("SELECT chat_id FROM "+table) |
38
|
|
|
for row in c.fetchall(): |
39
|
|
|
c.execute("SELECT date FROM "+table+" WHERE chat_id = %s" %(row)) |
40
|
|
|
old_date = c.fetchone()[0] |
41
|
|
|
old_date_time = datetime.strptime(old_date, "%d.%m.%Y %H:%M:%S") |
42
|
|
|
if date - old_date_time > span: |
43
|
|
|
c.execute("DELETE FROM "+table+" WHERE chat_id = %s" %(row)) |
44
|
|
|
conn.commit() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def get_chat_info(bot, update): |
48
|
|
|
if update.callback_query is not None: |
49
|
|
|
user_name = update.callback_query.message.chat.username |
50
|
|
|
chat_id = update.callback_query.message.chat_id |
51
|
|
|
user_id = update.callback_query.from_user.id |
52
|
|
|
else: |
53
|
|
|
user_name = update.effective_message.from_user.name[1:] |
54
|
|
|
chat_id = update.effective_message.chat_id |
55
|
|
|
user_id = update.effective_message.from_user.id |
56
|
|
|
chat = bot.getChat(chat_id) |
57
|
|
|
if chat.type == "private": |
58
|
|
|
owner = user_name |
59
|
|
|
else: |
60
|
|
|
admins = chat.get_administrators(CREATOR = "creator") |
61
|
|
|
for admin in admins: |
62
|
|
|
if admin.status == "creator": |
63
|
|
|
owner = admin.user.username |
64
|
|
|
return chat_id, chat.type, chat.title, chat.username, chat.description, chat.get_members_count(), owner, user_id, user_name |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def get_chats(bot, update): |
68
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
69
|
|
|
current_time_obj = datetime.strptime(current_time, "%d.%m.%Y %H:%M:%S") |
70
|
|
|
table_name = "chats" |
71
|
|
|
creation_columns = "date TEXT, chat_id INTEGER, chat_type TEXT, chat_title TEXT, chat_username TEXT, chat_desc TEXT, chat_members INTEGER, owner TEXT" |
72
|
|
|
entry_columns = "date, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner" |
73
|
|
|
create_table(table_name, creation_columns) |
74
|
|
|
delete_old(table_name, current_time_obj) |
75
|
|
|
chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner, _, _ = get_chat_info(bot, update) |
76
|
|
|
if check_entry(chat_id, table_name): |
77
|
|
|
return |
78
|
|
|
values = [current_time, chat_id, chat_type, chat_title, chat_username, chat_desc, chat_members, owner] |
79
|
|
|
data_entry(table_name, entry_columns, values) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def log_command(bot, update, date, command): |
83
|
|
|
table_name = "commands" |
84
|
|
|
creation_columns = "date TEXT, user_id INTEGER, user TEXT, command TEXT, chat_id INTEGER, chat_title TEXT" |
85
|
|
|
entry_columns = "date, user_id, user, command, chat_id, chat_title" |
86
|
|
|
create_table(table_name, creation_columns) |
87
|
|
|
ci = get_chat_info(bot, update) |
88
|
|
|
chat_id, chat_title, user_id, user = ci[0], ci[2], ci[7], ci[8] |
89
|
|
|
values = [date, user_id, user, command, chat_id, chat_title] |
90
|
|
|
data_entry(table_name, entry_columns, values) |
91
|
|
|
|