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-10 23:20:56 +02:00
|
|
|
import git
|
|
|
|
import requests
|
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-10 23:20:56 +02:00
|
|
|
def run_bot(unload: list = []):
|
2019-09-08 23:05:43 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
2019-09-10 23:44:44 +02:00
|
|
|
print(gettext('Stating...'))
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2019-09-10 23:20:56 +02:00
|
|
|
@click.command()
|
|
|
|
@click.option('-d', '--unload', multiple=True, type=str, help=gettext('Launch without loading the <TEXT> module'))
|
|
|
|
@click.option('-u', '--update', help=gettext('Search for update'), is_flag=True)
|
|
|
|
def main(**kwargs):
|
|
|
|
if kwargs.get('update'):
|
|
|
|
_update()
|
|
|
|
|
|
|
|
with setup_logging():
|
|
|
|
run_bot(kwargs.get('unload'))
|
|
|
|
|
|
|
|
|
|
|
|
@click.option('-d', '--update', help=gettext('Search for update'), is_flag=True)
|
|
|
|
def _update():
|
|
|
|
print(gettext('Checking for update...'))
|
|
|
|
|
|
|
|
local = git.Repo(search_parent_directories=True)
|
|
|
|
current = local.head.object.hexsha
|
|
|
|
|
|
|
|
origin = requests.get('https://git.gnous.eu/api/v1/repos/gnouseu/tuxbot-bot/branches/master')
|
|
|
|
last = origin.json().get('commit').get('id')
|
|
|
|
|
|
|
|
if current != last:
|
|
|
|
print(gettext('A new version is available !'))
|
2019-09-10 23:44:44 +02:00
|
|
|
check = None
|
|
|
|
|
|
|
|
while check not in ['', 'y', 'n', 'o']:
|
|
|
|
check = input(gettext('Update ? [Y/n] ')).lower().strip()
|
|
|
|
print(check)
|
|
|
|
|
|
|
|
if check in ['y', '', 'o']:
|
|
|
|
print(gettext('Downloading...'))
|
2019-09-10 23:20:56 +02:00
|
|
|
|
2019-09-10 23:44:44 +02:00
|
|
|
origin = git.Repo(search_parent_directories=True).remotes['origin']
|
|
|
|
origin.pull()
|
2019-09-10 23:20:56 +02:00
|
|
|
|
|
|
|
with setup_logging():
|
|
|
|
run_bot()
|
|
|
|
else:
|
|
|
|
with setup_logging():
|
|
|
|
run_bot()
|
|
|
|
else:
|
|
|
|
print(gettext('Tuxbot is up to date') + '\n')
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
with setup_logging():
|
2019-09-10 23:20:56 +02:00
|
|
|
run_bot()
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-09-08 23:05:43 +02:00
|
|
|
main()
|