-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
61 lines (41 loc) · 1.2 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
#!/usr/bin/env python
import logging
from discord_base import create_bot, get_bots
FORMAT = '%(asctime)-15s:%(levelname)s:%(name)s: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8')
handler.setFormatter(logging.Formatter(FORMAT))
logger.addHandler(handler)
import asyncio
import settings
from bots import SQFBot
def wakeup():
"""Dummy loop that allows using Ctrl+C in Windows"""
loop = asyncio.get_event_loop()
loop.call_later(0.3, wakeup)
tasks = []
async def on_shutdown():
global tasks
logger.info('Stopping bot...')
for task in tasks:
task.cancel()
for bot in get_bots():
logger.info('Closing client')
await bot.logout()
def run_discord_bots():
global tasks
tasks.extend(create_bot(SQFBot, settings.SQF_BOT))
def main():
loop = asyncio.get_event_loop()
wakeup()
try:
run_discord_bots()
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(on_shutdown())
finally:
loop.close()
if __name__ == '__main__':
main()