2019-09-29 18:31:01 +02:00
|
|
|
try:
|
|
|
|
import config
|
|
|
|
from cogs.utils.lang import Texts
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import extras.first_run
|
|
|
|
|
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-19 01:48:52 +02:00
|
|
|
import git
|
2019-09-12 15:43:57 +02:00
|
|
|
import requests
|
2019-09-08 23:05:43 +02:00
|
|
|
|
|
|
|
from bot import TuxBot
|
2019-10-06 23:49:00 +02:00
|
|
|
from cogs.utils.database import Database
|
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')
|
2019-09-21 00:11:29 +02:00
|
|
|
fmt = logging.Formatter('[{levelname:<7}] [{asctime}]'
|
2019-09-01 23:01:43 +02:00
|
|
|
' {name}: {message}',
|
|
|
|
'%Y-%m-%d %H:%M:%S', style='{')
|
|
|
|
|
|
|
|
handler.setFormatter(fmt)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
handlers = log.handlers[:]
|
2019-09-29 18:31:01 +02:00
|
|
|
for handler in handlers:
|
|
|
|
handler.close()
|
|
|
|
log.removeHandler(handler)
|
2019-09-01 23:01:43 +02:00
|
|
|
|
|
|
|
|
2019-09-10 23:20:56 +02:00
|
|
|
def run_bot(unload: list = []):
|
2019-09-08 23:05:43 +02:00
|
|
|
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:
|
2019-10-06 23:49:00 +02:00
|
|
|
database = Database(config)
|
2019-09-19 01:48:52 +02:00
|
|
|
except socket.gaierror:
|
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
|
|
|
|
|
2019-10-06 23:49:00 +02:00
|
|
|
bot = TuxBot(unload, database)
|
2019-09-08 23:05:43 +02:00
|
|
|
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"))
|
2019-09-29 18:31:01 +02:00
|
|
|
@click.option('-u', '--update', is_flag=True,
|
|
|
|
help=Texts().get("Search for update"))
|
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-29 18:31:01 +02:00
|
|
|
@click.option('-d', '--update', is_flag=True,
|
|
|
|
help=Texts().get("Search for update"))
|
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()
|