-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·124 lines (93 loc) · 3.11 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
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
import argparse
import asyncio
import json
import logging
import asyncpg
import coc
import disnake
from bot import BotClient
from packages.config import Settings, BotMode, init_tables, load_settings
from packages.utils.logging_setup import BotLogger
def _bot_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Process arguments for discord bot")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--live",
help="Run bot with Panther shell",
action="store_true",
dest="live_mode",
default=False)
group.add_argument(
"--dev",
help="Run in dev shell",
action="store_true",
dest="dev_mode",
default=False)
return parser.parse_args()
def _get_settings() -> Settings:
args = _bot_args()
if args.live_mode:
return load_settings(BotMode.LIVE_MODE)
else:
return load_settings(BotMode.DEV_MODE)
async def _get_pool(settings: Settings) -> asyncpg.pool.Pool:
try:
async def init(con):
"""Create custom column type, json."""
await con.set_type_codec("json", schema="pg_catalog",
encoder=json.dumps, decoder=json.loads)
pool = await asyncpg.create_pool(settings.dsn, init=init)
if pool is None:
raise Exception("Unable to create pool")
async with pool.acquire() as con:
for table in init_tables():
await con.execute(table)
return pool
except Exception:
log.critical("Pool error", exc_info=True)
async def _get_coc_client(settings: Settings) -> coc.Client:
coc_client = coc.Client(key_names="APIBOT Keys")
try:
await coc_client.login(settings.coc_email, settings.coc_password)
return coc_client
except coc.InvalidCredentials as err:
log.critical("CoC error", exc_info=True)
def _get_bot_client(settings: Settings, coc_client: coc.Client,
pool: asyncpg.Pool) -> BotClient:
intents = disnake.Intents.default()
intents.message_content = True
intents.members = True
intents.reactions = True
intents.emojis = True
intents.guilds = True
return BotClient(
settings=settings,
coc_client=coc_client,
pool=pool,
intents=intents
)
async def main(settings: Settings) -> None:
pool = await _get_pool(settings)
coc_client = await _get_coc_client(settings)
log.debug("Bot initialized, starting bot...")
bot = _get_bot_client(settings, coc_client, pool)
try:
await bot.start(settings.bot_token)
except KeyboardInterrupt:
pass # Ignore interrupts and go to clean up
finally:
# Close both pool and client sessions
await pool.close()
await coc_client.close()
if __name__ == "__main__":
_settings = _get_settings()
try:
BotLogger(_settings)
except Exception as error:
exit(error)
log = logging.getLogger(f"{_settings.log_name}.Main")
try:
asyncio.run(main(_settings))
except KeyboardInterrupt:
pass