From 1b7f153ec8d9415dfbbc75ccddd2bce0a48fb76b Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 20 Apr 2021 15:43:20 +0200 Subject: [PATCH] update(core): migrate to py3.10 & dpy2.0 --- .idea/dictionaries/romain.xml | 6 ++++ .idea/misc.xml | 2 +- .idea/tuxbot_bot.iml | 2 +- .idea/vcs.xml | 1 - Makefile | 1 - setup.cfg | 22 ++++++------- tuxbot/__main__.py | 8 +++-- tuxbot/__run__.py | 4 +-- tuxbot/cogs/Dev/dev.py | 1 + tuxbot/cogs/Logs/logs.py | 6 ++-- tuxbot/cogs/Utils/utils.py | 1 + tuxbot/core/bot.py | 13 +++++--- tuxbot/core/config.py | 1 + tuxbot/core/utils/console.py | 2 +- tuxbot/core/utils/functions/extra.py | 2 ++ tuxbot/logging.py | 47 ++++++++++++++-------------- tuxbot/setup.py | 2 +- 17 files changed, 69 insertions(+), 52 deletions(-) diff --git a/.idea/dictionaries/romain.xml b/.idea/dictionaries/romain.xml index 7c96624..573134c 100644 --- a/.idea/dictionaries/romain.xml +++ b/.idea/dictionaries/romain.xml @@ -1,6 +1,8 @@ + aaaa + ajout anglais anonyme appdirs @@ -9,7 +11,9 @@ commandstats ctype debian + dnskey découverte + ffff fonction francais français @@ -29,6 +33,7 @@ outoutxyz outouxyz pacman + perso postgre postgresql pred @@ -36,6 +41,7 @@ pylint releaselevel rprint + skipcq socketstats soit sondage diff --git a/.idea/misc.xml b/.idea/misc.xml index 8b38b9d..24570af 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/tuxbot_bot.iml b/.idea/tuxbot_bot.iml index 784153e..84404e0 100644 --- a/.idea/tuxbot_bot.iml +++ b/.idea/tuxbot_bot.iml @@ -6,7 +6,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index b626dae..78cface 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -7,6 +7,5 @@ - \ No newline at end of file diff --git a/Makefile b/Makefile index 696c0b2..3b5b866 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,6 @@ XGETTEXT_FLAGS := --no-wrap --language='python' --keyword=_ --from-code='UTF-8' # Init .PHONY: main main: - $(PYTHON_PATH) -m venv --clear $(VENV) $(VIRTUAL_ENV)/bin/pip install -U pip setuptools .PHONY: install diff --git a/setup.cfg b/setup.cfg index 1dcefd4..35e9ec7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,20 +15,20 @@ platforms = linux packages = find_namespace: python_requires = >=3.9 install_requires = - asyncpg==0.21.0 - Babel==2.8.0 + asyncpg>=0.21.0 + Babel>=2.8.0 discord.py @ git+https://github.com/Rapptz/discord.py - humanize==2.6.0 - ipinfo==4.1.0 - ipwhois==1.2.0 + humanize>=2.6.0 + ipinfo>=4.1.0 + ipwhois>=1.2.0 jishaku @ git+https://github.com/Gorialis/jishaku - psutil==5.7.2 - pydig==0.3.0 - ralgo @ git+https://github.com/Rom1-J/ralgo - rich==9.10.0 + psutil>=5.7.2 + pydig>=0.3.0 +; ralgo @ git+https://github.com/Rom1-J/ralgo + rich>=9.10.0 sentry_sdk>=0.20.2 - structured_config==4.12 - tortoise-orm==0.16.17 + structured_config>=4.12 + tortoise-orm>=0.16.17 [options.entry_points] console_scripts = diff --git a/tuxbot/__main__.py b/tuxbot/__main__.py index 02c7711..701c8c4 100644 --- a/tuxbot/__main__.py +++ b/tuxbot/__main__.py @@ -14,11 +14,15 @@ def main() -> None: else: raise exc except Exception: - console.print_exception(show_locals=True) + console.print_exception( + show_locals=True, word_wrap=True, extra_lines=5 + ) if __name__ == "__main__": try: main() except Exception: - console.print_exception(show_locals=True) + console.print_exception( + show_locals=True, word_wrap=True, extra_lines=5 + ) diff --git a/tuxbot/__run__.py b/tuxbot/__run__.py index b20d4c1..b2246fc 100644 --- a/tuxbot/__run__.py +++ b/tuxbot/__run__.py @@ -173,9 +173,9 @@ async def run_bot(tux: Tux, cli_flags: Namespace) -> None: try: await tux.load_packages() console.print() - await tux.start(token=token, bot=True) + await tux.start(token=token) except discord.LoginFailure: - log.critical("This token appears to be valid.") + log.critical("This token appears to be invalid.") console.print() console.print( "[prompt.invalid]This token appears to be valid. [i]exiting...[/i]" diff --git a/tuxbot/cogs/Dev/dev.py b/tuxbot/cogs/Dev/dev.py index 4124eac..e955fad 100644 --- a/tuxbot/cogs/Dev/dev.py +++ b/tuxbot/cogs/Dev/dev.py @@ -25,6 +25,7 @@ class Dev(commands.Cog, name="Dev"): if crash_type == "ZeroDivisionError": await ctx.send(str(5 / 0)) elif crash_type == "TypeError": + # noinspection PyTypeChecker await ctx.send(str(int([]))) elif crash_type == "IndexError": await ctx.send(str([0][5])) diff --git a/tuxbot/cogs/Logs/logs.py b/tuxbot/cogs/Logs/logs.py index fbfed45..0f7c86f 100644 --- a/tuxbot/cogs/Logs/logs.py +++ b/tuxbot/cogs/Logs/logs.py @@ -51,9 +51,9 @@ class Logs(commands.Cog, name="Logs"): def __init__(self, bot: Tux): self.bot = bot self.process = psutil.Process() - self._batch_lock = asyncio.Lock(loop=bot.loop) + self._batch_lock = asyncio.Lock() self._data_batch = [] - self._gateway_queue = asyncio.Queue(loop=bot.loop) + self._gateway_queue = asyncio.Queue() self.gateway_worker.start() # pylint: disable=no-member self.__config: LogsConfig = ConfigFile( @@ -88,7 +88,7 @@ class Logs(commands.Cog, name="Logs"): def webhook(self, log_type): webhook = discord.Webhook.from_url( getattr(self.__config, log_type), - adapter=discord.AsyncWebhookAdapter(self.bot.session), + session=self.bot.session, ) return webhook diff --git a/tuxbot/cogs/Utils/utils.py b/tuxbot/cogs/Utils/utils.py index d5c068b..a0c94af 100644 --- a/tuxbot/cogs/Utils/utils.py +++ b/tuxbot/cogs/Utils/utils.py @@ -239,6 +239,7 @@ class Utils(commands.Cog, name="Utils"): cmd = self.bot.get_command(name) if cmd: + # noinspection PyUnresolvedReferences src = cmd.callback.__code__ rpath = src.co_filename else: diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index 0933583..dfec248 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -89,11 +89,16 @@ class Tux(commands.AutoShardedBot): self.uptime = None self.last_on_ready = None self._app_owners_fetched = False # to prevent abusive API calls + self.loop = asyncio.get_event_loop() self.before_invoke(self._typing) super().__init__( - *args, help_command=None, intents=discord.Intents.all(), **kwargs + *args, + # help_command=None, + intents=discord.Intents.all(), + loop=self.loop, + **kwargs, ) self.session = aiohttp.ClientSession(loop=self.loop) @@ -269,7 +274,7 @@ class Tux(commands.AutoShardedBot): if not await self._is_blacklisted(message): await self.process_commands(message) - async def start(self, token, bot): # pylint: disable=arguments-differ + async def start(self, token): # pylint: disable=arguments-differ """Connect to Discord and start all connections.""" with Progress() as progress: task = progress.add_task( @@ -309,7 +314,7 @@ class Tux(commands.AutoShardedBot): start=False, ) progress.update(task_id) - await super().start(token, bot=bot) + await super().start(token) async def logout(self): """Disconnect from Discord and closes all actives connections. @@ -335,7 +340,7 @@ class Tux(commands.AutoShardedBot): task.cancel() await asyncio.gather(*pending, return_exceptions=False) - await super().logout() + await super().close() async def shutdown(self, *, restart: bool = False): """Gracefully quit. diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py index 822d049..72a87d2 100644 --- a/tuxbot/core/config.py +++ b/tuxbot/core/config.py @@ -14,6 +14,7 @@ __all__ = [ "search_for", "set_for_key", "set_for", + "set_if_none", ] log = logging.getLogger("tuxbot.core.config") diff --git a/tuxbot/core/utils/console.py b/tuxbot/core/utils/console.py index 0b5ce24..29f00f3 100644 --- a/tuxbot/core/utils/console.py +++ b/tuxbot/core/utils/console.py @@ -3,4 +3,4 @@ from rich.traceback import install console = Console() -install(console=console, show_locals=True) +install(console=console, show_locals=True, width=console.width) diff --git a/tuxbot/core/utils/functions/extra.py b/tuxbot/core/utils/functions/extra.py index ba898e3..5ce5bb1 100644 --- a/tuxbot/core/utils/functions/extra.py +++ b/tuxbot/core/utils/functions/extra.py @@ -13,6 +13,7 @@ IP_REPLACEMENT = "■" * random.randint(3, 15) class ContextPlus(commands.Context): + # noinspection PyTypedDict async def send( self, content=None, @@ -31,6 +32,7 @@ class ContextPlus(commands.Context): replace_in_list, ) + # todo: rewrite replacements if content: content = ( content.replace(self.bot.config.Core.token, TOKEN_REPLACEMENT) diff --git a/tuxbot/logging.py b/tuxbot/logging.py index 464f504..1b56586 100644 --- a/tuxbot/logging.py +++ b/tuxbot/logging.py @@ -13,6 +13,27 @@ formatter = logging.Formatter( ) +def _setup_logging(level: int, location: pathlib.Path, name: str) -> None: + logger = logging.getLogger(name) + logger.setLevel(level) + logger_file = location / f"{name}.log" + + handler = logging.handlers.RotatingFileHandler( + str(logger_file.resolve()), + maxBytes=MAX_BYTES, + backupCount=MAX_OLD_LOGS, + ) + + base_handler = logging.handlers.RotatingFileHandler( + str(logger_file.resolve()), + maxBytes=MAX_BYTES, + backupCount=MAX_OLD_LOGS, + ) + + handler.setFormatter(formatter) + base_handler.setFormatter(formatter) + + def init_logging(level: int, location: pathlib.Path) -> None: """Initialize loggers. @@ -24,30 +45,8 @@ def init_logging(level: int, location: pathlib.Path) -> None: Where to store Logs. """ - # dpy_logger = logging.getLogger("discord") - # dpy_logger.setLevel(logging.WARN) - # dpy_logger_file = location / "discord.log" - - base_logger = logging.getLogger("tuxbot") - base_logger.setLevel(level) - base_logger_file = location / "tuxbot.log" - - # dpy_handler = logging.handlers.RotatingFileHandler( - # str(dpy_logger_file.resolve()), - # maxBytes=MAX_BYTES, - # backupCount=MAX_OLD_LOGS, - # ) - base_handler = logging.handlers.RotatingFileHandler( - str(base_logger_file.resolve()), - maxBytes=MAX_BYTES, - backupCount=MAX_OLD_LOGS, - ) + _setup_logging(level, location, "discord") + _setup_logging(level, location, "tuxbot") stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) - - # dpy_handler.setFormatter(formatter) - base_handler.setFormatter(formatter) - - # dpy_logger.addHandler(dpy_handler) - base_logger.addHandler(base_handler) diff --git a/tuxbot/setup.py b/tuxbot/setup.py index 7b37b04..72e31d1 100644 --- a/tuxbot/setup.py +++ b/tuxbot/setup.py @@ -328,7 +328,7 @@ def update() -> None: ) if response.get("sha")[:6] == version_info.build: - print("Nothing to update, you can run `tuxbot` " "to start the bot") + print("Nothing to update, you can run `tuxbot` to start the bot") else: print(f"Updating to {response.get('sha')[:6]}...")