-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlauncher.py
137 lines (118 loc) · 4.37 KB
/
launcher.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
125
126
127
128
129
130
131
132
133
134
135
136
137
############
#
from asyncio import run, set_event_loop_policy, sleep
from contextlib import contextmanager
from logging import INFO, NullHandler, basicConfig, getLogger
from sys import exc_info
from traceback import format_exception
from asyncpg import Pool
from discord import Activity, ActivityType, CustomActivity, Intents
from Classes import (
Migration,
NoCommands,
NoVotes,
OnlyCommands,
ShakeBot,
config,
handler,
)
try:
from uvloop import EventLoopPolicy # type: ignore
except ImportError:
pass
else:
set_event_loop_policy(EventLoopPolicy())
########
#
@contextmanager
def setup():
def root():
logger.addHandler(NullHandler())
logger.setLevel(INFO)
*n, stream = handler(
file=False,
stream=True,
filters=(
NoCommands,
NoVotes,
),
)
file, *n = handler(
file=True,
stream=False,
filepath="./Classes/logging/latest/shake.log",
filters=(NoCommands,),
)
logger.addHandler(stream)
logger.addHandler(file)
def commands():
log = getLogger("shake.commands")
file_handler, *n = handler(
file=True,
stream=False,
filepath=f"./Classes/logging/latest/commands.log",
filters=(OnlyCommands,),
)
log.addHandler(file_handler)
def discord():
log = getLogger("discord.gateway")
for func in (root, commands, discord):
func()
yield
async def main():
def prefix(bot, msg):
return (_.format(bot.user.id) for _ in ("<@!{}> ", "<@{}> "))
async with ShakeBot(
logger=logger,
shard_count=config.client.shard_count,
command_prefix=prefix,
case_insensitive=True,
intents=Intents.all(),
description=config.bot.description,
help_command=None,
fetch_offline_members=True,
owner_ids=config.bot.owner_ids,
strip_after_prefix=True,
activity=CustomActivity(
# type=getattr(ActivityType, config.bot.presence[0], ActivityType.playing),
name=config.bot.presence[1],
),
) as bot:
bot.pool: Pool = await Migration._create_pool("bot")
bot.gpool: Pool = await Migration._create_pool("guild")
await bot.start(token=config.client.token)
if __name__ == "__main__":
logger = getLogger()
with setup():
print(
"\n"
" ▄████████▄ ▄██ ██▄ ▄████████ ▄██ ▄██ ▄████████▄ \n"
" ███▀ ███ ███ ███ ███ ███ ███ ▄███▀ ███▀ ▄██ \n"
" ███ █▀ ███ ███▄▄ ███ ███ ███▐██▀ ███ █▀ \n"
" ████▄▄▄▄▄ ███▄▄▄████▀ ███▄▄▄▄███ ▄████▀ ▄███▄▄▄ \n"
" ▀▀▀▀▀▀███▄ ▄█████▀▀▀███ ▀████▀▀▀▀███ ▀▀█████▄ ▀▀███▀▀▀ \n"
" ███ ███ ███ ███ ███ ███▐██▄ ███ █▄ \n"
" ▄█ ▄███▀ ███ ███ ███ ███ ███ ▀███▄ ███ ██▄ \n"
" ▄█████████▀ ▀██ █▀ ███ ██▀ ███ ▀██▄ ██████████ \n"
"\u200b"
)
tries = 0
while True:
try:
run(main())
except KeyboardInterrupt:
break
except Exception as e:
if tries < config.client.retries:
try:
logger.debug(
"Restarting ({}/{})".format(tries, config.client.retries)
)
tries += 1
run(sleep(5))
except:
break
else:
exc, value, tb, *_ = exc_info()
logger.warning(f"Closing: {''.join(format_exception(exc, value, tb))}")
break