2019-09-08 23:05:43 +02:00
|
|
|
import asyncio
|
2019-09-01 23:01:43 +02:00
|
|
|
import contextlib
|
2019-09-08 23:05:43 +02:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
import sys
|
2019-09-01 23:01:43 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
import click
|
|
|
|
|
|
|
|
from bot import TuxBot
|
|
|
|
from cogs.utils.db import Table
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import config
|
2019-09-09 22:40:17 +02:00
|
|
|
from cogs.utils.lang import gettext
|
2019-09-01 23:01:43 +02:00
|
|
|
except ModuleNotFoundError:
|
|
|
|
import first_run
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def setup_logging():
|
|
|
|
try:
|
|
|
|
logging.getLogger('discord').setLevel(logging.INFO)
|
|
|
|
logging.getLogger('discord.http').setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
handler = logging.FileHandler(filename='logs/tuxbot.log',
|
|
|
|
encoding='utf-8', mode='w')
|
|
|
|
fmt = logging.Formatter('[{asctime}] [{levelname:<7}]'
|
|
|
|
' {name}: {message}',
|
|
|
|
'%Y-%m-%d %H:%M:%S', style='{')
|
|
|
|
|
|
|
|
handler.setFormatter(fmt)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
handlers = log.handlers[:]
|
|
|
|
for hdlr in handlers:
|
|
|
|
hdlr.close()
|
|
|
|
log.removeHandler(hdlr)
|
|
|
|
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
def run_bot(unload):
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
try:
|
|
|
|
pool = loop.run_until_complete(
|
|
|
|
Table.create_pool(config.postgresql, command_timeout=60)
|
|
|
|
)
|
|
|
|
except socket.gaierror as e:
|
2019-09-09 22:40:17 +02:00
|
|
|
click.echo(gettext('Could not set up PostgreSQL...'), file=sys.stderr)
|
|
|
|
log.exception(gettext('Could not set up PostgreSQL...'))
|
2019-09-08 23:05:43 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
bot = TuxBot(unload)
|
|
|
|
bot.pool = pool
|
|
|
|
bot.run()
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(invoke_without_command=True, options_metavar='[options]')
|
|
|
|
@click.option('-u', '--unload',
|
|
|
|
multiple=True, type=str,
|
2019-09-09 22:40:17 +02:00
|
|
|
help=gettext('Launch without loading the <TEXT> module'))
|
2019-09-08 23:05:43 +02:00
|
|
|
@click.pass_context
|
|
|
|
def main(ctx, unload):
|
|
|
|
if ctx.invoked_subcommand is None:
|
|
|
|
with setup_logging():
|
|
|
|
run_bot(unload)
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-09-08 23:05:43 +02:00
|
|
|
main()
|