-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
46 lines (37 loc) · 1.3 KB
/
main.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
import asyncio
import os
import asyncpg # https://pypi.org/project/asyncpg/
from bot import Bot
async def main() -> None:
if os.environ.get("ENV") != "docker":
from dotenv import load_dotenv # https://pypi.org/project/python-dotenv/
dotenv_path: str = ".env"
abs_dotenv_path: str = os.path.join(os.path.dirname(__file__), dotenv_path)
load_dotenv(abs_dotenv_path)
try:
db: asyncpg.Pool = await get_db_connection()
except Exception:
print("\x1b[31mError: unable to connect to the database because:\x1b[0m")
raise
bot = Bot()
bot.db = db
token = os.environ["DISCORD_BOT_TOKEN"]
async with bot:
await bot.start(token, reconnect=True)
async def get_db_connection() -> asyncpg.Pool:
"""Connects to the PostgreSQL database"""
host = os.environ.get("POSTGRES_HOST", "localhost")
database = os.environ.get("POSTGRES_DB", "postgres")
port = os.environ.get("POSTGRES_PORT", "5432")
user = os.environ.get("POSTGRES_USER", "postgres")
password = os.environ["POSTGRES_PASSWORD"]
return await asyncpg.create_pool(
host=host,
database=database,
port=port,
user=user,
password=password,
command_timeout=60,
)
if __name__ == "__main__":
asyncio.run(main())