add(laucnher|update): add bot.version and --update

This commit is contained in:
Romain J 2019-09-10 23:20:56 +02:00
parent 712339968c
commit 02d279b6c4
8 changed files with 71 additions and 12 deletions

View file

@ -10,8 +10,8 @@
## Launcher requirements :
- [ ] Can install the bot
- [ ] Can launch the bot
- [x] Can install the bot
- [x] Can launch the bot
- [ ] Can propose updates
## New commands :

5
bot.py
View file

@ -6,11 +6,13 @@ from collections import deque
import aiohttp
import discord
import git
from discord.ext import commands
import config
from cogs.utils.config import Config
from cogs.utils.lang import gettext
from cogs.utils.version import Version
description = """
Je suis TuxBot, le bot qui vit de l'OpenSource ! ;)
@ -47,6 +49,8 @@ class TuxBot(commands.AutoShardedBot):
self.prefixes = Config('prefixes.json')
self.blacklist = Config('blacklist.json')
self.version = Version(10, 0, 0, pre_release='a18', build=git.Repo(search_parent_directories=True).head.object.hexsha)
for extension in l_extensions:
if extension not in unload:
try:
@ -95,6 +99,7 @@ class TuxBot(commands.AutoShardedBot):
self.uptime = datetime.datetime.utcnow()
print(gettext('Ready:') + f' {self.user} (ID: {self.user.id})')
print(self.version)
presence: dict = dict(status=discord.Status.dnd)
if self.config.activity is not None:

12
cogs/utils/version.py Normal file
View file

@ -0,0 +1,12 @@
class Version:
def __init__(self, major: int, minor: int, patch: int, **kwargs):
self.major: int = major
self.minor: int = minor
self.patch: int = patch
self.pre_release = kwargs.get('pre_release', '')
self.build = kwargs.get('build', '')
def __str__(self) -> str:
build = self.build[:10]
return f'v{self.major}.{self.minor}.{self.patch}{self.pre_release}-{build}'

View file

@ -3,6 +3,8 @@ import contextlib
import logging
import socket
import sys
import git
import requests
import click
@ -42,7 +44,7 @@ def setup_logging():
log.removeHandler(hdlr)
def run_bot(unload):
def run_bot(unload: list = []):
loop = asyncio.get_event_loop()
log = logging.getLogger()
@ -60,15 +62,46 @@ def run_bot(unload):
bot.run()
@click.group(invoke_without_command=True, options_metavar='[options]')
@click.option('-u', '--unload',
multiple=True, type=str,
help=gettext('Launch without loading the <TEXT> module'))
@click.pass_context
def main(ctx, unload):
if ctx.invoked_subcommand is None:
@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 !'))
check = input(gettext('Update ? [Y/n]')).lower().strip()
while check not in ['', 'y', 'n']:
check = input(gettext('Update ? [Y/n]'))
if check == 'y':
local.remotes.origin.pull()
with setup_logging():
run_bot()
else:
with setup_logging():
run_bot()
else:
print(gettext('Tuxbot is up to date') + '\n')
with setup_logging():
run_bot(unload)
run_bot()
if __name__ == '__main__':

View file

@ -23,6 +23,10 @@ msgstr ""
msgid "Launch without loading the <TEXT> module"
msgstr ""
#: launcher.py:73
msgid "Search for update"
msgstr ""
#: bot.py:52
msgid "Failed to load extension : "
msgstr ""

Binary file not shown.

View file

@ -23,6 +23,10 @@ msgstr "Impossible de lancer PostgreSQL..."
msgid "Launch without loading the <TEXT> module"
msgstr "Lancer sans charger le module <TEXT>"
#: launcher.py:73
msgid "Search for update"
msgstr "Rechercher les mises à jour"
#: bot.py:52
msgid "Failed to load extension : "
msgstr "Impossible de charger l'extension : "

View file

@ -3,3 +3,4 @@ jishaku
lxml
click
asyncpg>=0.12.0
gitpython