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
|
2019-09-12 15:43:57 +02:00
|
|
|
import requests
|
2019-09-08 23:05:43 +02:00
|
|
|
|
|
|
|
from bot import TuxBot
|
|
|
|
from cogs.utils.db import Table
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import config
|
2019-09-12 15:43:57 +02:00
|
|
|
from cogs.utils.lang import Texts
|
2019-09-01 23:01:43 +02:00
|
|
|
except ModuleNotFoundError:
|
2019-09-18 12:30:49 +02:00
|
|
|
import extras.first_run
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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-15 02:09:36 +02:00
|
|
|
print(Texts().get('Starting...'))
|
2019-09-10 23:44:44 +02:00
|
|
|
|
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-12 15:43:57 +02:00
|
|
|
click.echo(Texts().get("Could not set up PostgreSQL..."),
|
|
|
|
file=sys.stderr)
|
|
|
|
log.exception(Texts().get("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()
|
2019-09-12 15:43:57 +02:00
|
|
|
@click.option('-d', '--unload', multiple=True, type=str,
|
|
|
|
help=Texts().get("Launch without loading the <TEXT> module"))
|
|
|
|
@click.option('-u', '--update', help=Texts().get("Search for update"),
|
|
|
|
is_flag=True)
|
2019-09-10 23:20:56 +02:00
|
|
|
def main(**kwargs):
|
|
|
|
if kwargs.get('update'):
|
|
|
|
_update()
|
|
|
|
|
|
|
|
with setup_logging():
|
|
|
|
run_bot(kwargs.get('unload'))
|
|
|
|
|
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
@click.option('-d', '--update', help=Texts().get("Search for update"),
|
|
|
|
is_flag=True)
|
2019-09-10 23:20:56 +02:00
|
|
|
def _update():
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("Checking for update..."))
|
2019-09-10 23:20:56 +02:00
|
|
|
|
|
|
|
local = git.Repo(search_parent_directories=True)
|
|
|
|
current = local.head.object.hexsha
|
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
gitea = 'https://git.gnous.eu/api/v1/'
|
|
|
|
origin = requests.get(gitea + 'repos/gnouseu/tuxbot-bot/branches/rewrite')
|
2019-09-10 23:20:56 +02:00
|
|
|
last = origin.json().get('commit').get('id')
|
|
|
|
|
|
|
|
if current != last:
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("A new version is available !"))
|
2019-09-10 23:44:44 +02:00
|
|
|
check = None
|
|
|
|
|
|
|
|
while check not in ['', 'y', 'n', 'o']:
|
2019-09-12 15:43:57 +02:00
|
|
|
check = input(Texts().get("Update ? [Y/n] ")).lower().strip()
|
2019-09-10 23:44:44 +02:00
|
|
|
print(check)
|
|
|
|
|
|
|
|
if check in ['y', '', 'o']:
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("Downloading..."))
|
2019-09-10 23:20:56 +02:00
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
origin = git.Repo(search_parent_directories=True) \
|
|
|
|
.remotes['origin']
|
2019-09-10 23:44:44 +02:00
|
|
|
origin.pull()
|
2019-09-10 23:20:56 +02:00
|
|
|
|
|
|
|
with setup_logging():
|
|
|
|
run_bot()
|
|
|
|
else:
|
|
|
|
with setup_logging():
|
|
|
|
run_bot()
|
|
|
|
else:
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("Tuxbot is up to date") + '\n')
|
2019-09-10 23:20:56 +02:00
|
|
|
|
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()
|