-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlutil.py
57 lines (44 loc) · 1.43 KB
/
sqlutil.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Standard imports
import sqlite3
from typing import ClassVar, NoReturn, Any
async def add_user(members: ClassVar[Any], me: ClassVar[Any]) -> NoReturn:
"""
Function to add list of users to SQLite3 database
Parameters:
members: Telegram users
Returns:
Typing.NoReturn
"""
# Connect to SQLite3 database inside the 'Users' directory
connection: ClassVar[Any] = sqlite3.connect("Users/tuser.db")
# Make a cursor for command execution
cursor: ClassVar[Any] = connection.cursor()
# Add user to database
# Note: We already made a table for users, so if you don't have table,
# make one before using this tool
# CREATE TABLE "users" (
# "chat_id" INTEGER UNIQUE,
# "username" TEXT,
# "access_hash" TEXT
# );
# Add members to database
async for member in members:
# Print status
print(f"Adding {member.first_name}", end="\r", flush=True)
# Exclude self bot (yourself)
if me.id == member.id:
continue
# Execute add command
cursor.execute(
"INSERT OR IGNORE INTO users VALUES (?, ?, ?)", (
member.id, member.username, member.access_hash
)
)
# Print done message
print("\n:: Add done.")
# Commit changes
connection.commit()
# Close connection
connection.close()