-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (59 loc) · 2.21 KB
/
main.py
File metadata and controls
79 lines (59 loc) · 2.21 KB
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
import os
import threading
from dotenv import load_dotenv
from infrastructure.db.account_repository_sqlite import SqliteAccountRepository
from infrastructure.db.identity_repository_sqlite import SqliteIdentityRepository
from infrastructure.db.table_repository_sqlite import SqliteTableRepository
from infrastructure.db.user_repository_sqlite import SqliteUserRepository
from interfaces.discord.handlers import create_discord_bot
from interfaces.telegram.handlers import create_telegram_bot
load_dotenv()
BOT_TOKEN = os.environ.get("BOT_TOKEN")
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN")
DB_PATH = os.environ.get("DB_PATH", "poker.db")
def _run_telegram_bot(
user_repo: SqliteUserRepository,
identity_repo: SqliteIdentityRepository,
account_repo: SqliteAccountRepository,
table_repo: SqliteTableRepository,
) -> None:
if not BOT_TOKEN:
raise RuntimeError("BOT_TOKEN environment variable is not set.")
bot = create_telegram_bot(BOT_TOKEN, user_repo, identity_repo, account_repo, table_repo)
bot.infinity_polling()
def _run_discord_bot(
user_repo: SqliteUserRepository,
identity_repo: SqliteIdentityRepository,
account_repo: SqliteAccountRepository,
) -> None:
# Discord is optional – only start it if a token is configured.
if not DISCORD_TOKEN:
return
table_repo = SqliteTableRepository(DB_PATH)
bot = create_discord_bot(user_repo, identity_repo, account_repo, table_repo)
bot.run(DISCORD_TOKEN)
def main() -> None:
user_repo = SqliteUserRepository(DB_PATH)
identity_repo = SqliteIdentityRepository(DB_PATH, user_repo)
account_repo = SqliteAccountRepository(DB_PATH)
table_repo = SqliteTableRepository(DB_PATH)
threads = [
threading.Thread(
target=_run_telegram_bot,
args=(user_repo, identity_repo, account_repo, table_repo),
name="telegram-bot",
daemon=False,
),
threading.Thread(
target=_run_discord_bot,
args=(user_repo, identity_repo, account_repo),
name="discord-bot",
daemon=False,
),
]
for t in threads:
t.start()
for t in threads:
t.join()
if __name__ == "__main__":
main()