update(core): migrate to py3.10 & dpy2.0

This commit is contained in:
Romain J 2021-04-20 15:43:20 +02:00
parent 0eca877c1c
commit 1b7f153ec8
17 changed files with 69 additions and 52 deletions

View file

@ -1,6 +1,8 @@
<component name="ProjectDictionaryState"> <component name="ProjectDictionaryState">
<dictionary name="romain"> <dictionary name="romain">
<words> <words>
<w>aaaa</w>
<w>ajout</w>
<w>anglais</w> <w>anglais</w>
<w>anonyme</w> <w>anonyme</w>
<w>appdirs</w> <w>appdirs</w>
@ -9,7 +11,9 @@
<w>commandstats</w> <w>commandstats</w>
<w>ctype</w> <w>ctype</w>
<w>debian</w> <w>debian</w>
<w>dnskey</w>
<w>découverte</w> <w>découverte</w>
<w>ffff</w>
<w>fonction</w> <w>fonction</w>
<w>francais</w> <w>francais</w>
<w>français</w> <w>français</w>
@ -29,6 +33,7 @@
<w>outoutxyz</w> <w>outoutxyz</w>
<w>outouxyz</w> <w>outouxyz</w>
<w>pacman</w> <w>pacman</w>
<w>perso</w>
<w>postgre</w> <w>postgre</w>
<w>postgresql</w> <w>postgresql</w>
<w>pred</w> <w>pred</w>
@ -36,6 +41,7 @@
<w>pylint</w> <w>pylint</w>
<w>releaselevel</w> <w>releaselevel</w>
<w>rprint</w> <w>rprint</w>
<w>skipcq</w>
<w>socketstats</w> <w>socketstats</w>
<w>soit</w> <w>soit</w>
<w>sondage</w> <w>sondage</w>

View file

@ -3,5 +3,5 @@
<component name="JavaScriptSettings"> <component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (tuxbot-bot)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (tuxbot_bot)" project-jdk-type="Python SDK" />
</project> </project>

View file

@ -6,7 +6,7 @@
<excludeFolder url="file://$MODULE_DIR$/dist" /> <excludeFolder url="file://$MODULE_DIR$/dist" />
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.9 (venv)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.10 (tuxbot_bot)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PyDocumentationSettings"> <component name="PyDocumentationSettings">

View file

@ -7,6 +7,5 @@
</component> </component>
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/tuxbot/cogs/Crypto/deps/ralgo" vcs="Git" />
</component> </component>
</project> </project>

View file

@ -15,7 +15,6 @@ XGETTEXT_FLAGS := --no-wrap --language='python' --keyword=_ --from-code='UTF-8'
# Init # Init
.PHONY: main .PHONY: main
main: main:
$(PYTHON_PATH) -m venv --clear $(VENV)
$(VIRTUAL_ENV)/bin/pip install -U pip setuptools $(VIRTUAL_ENV)/bin/pip install -U pip setuptools
.PHONY: install .PHONY: install

View file

@ -15,20 +15,20 @@ platforms = linux
packages = find_namespace: packages = find_namespace:
python_requires = >=3.9 python_requires = >=3.9
install_requires = install_requires =
asyncpg==0.21.0 asyncpg>=0.21.0
Babel==2.8.0 Babel>=2.8.0
discord.py @ git+https://github.com/Rapptz/discord.py discord.py @ git+https://github.com/Rapptz/discord.py
humanize==2.6.0 humanize>=2.6.0
ipinfo==4.1.0 ipinfo>=4.1.0
ipwhois==1.2.0 ipwhois>=1.2.0
jishaku @ git+https://github.com/Gorialis/jishaku jishaku @ git+https://github.com/Gorialis/jishaku
psutil==5.7.2 psutil>=5.7.2
pydig==0.3.0 pydig>=0.3.0
ralgo @ git+https://github.com/Rom1-J/ralgo ; ralgo @ git+https://github.com/Rom1-J/ralgo
rich==9.10.0 rich>=9.10.0
sentry_sdk>=0.20.2 sentry_sdk>=0.20.2
structured_config==4.12 structured_config>=4.12
tortoise-orm==0.16.17 tortoise-orm>=0.16.17
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =

View file

@ -14,11 +14,15 @@ def main() -> None:
else: else:
raise exc raise exc
except Exception: except Exception:
console.print_exception(show_locals=True) console.print_exception(
show_locals=True, word_wrap=True, extra_lines=5
)
if __name__ == "__main__": if __name__ == "__main__":
try: try:
main() main()
except Exception: except Exception:
console.print_exception(show_locals=True) console.print_exception(
show_locals=True, word_wrap=True, extra_lines=5
)

View file

@ -173,9 +173,9 @@ async def run_bot(tux: Tux, cli_flags: Namespace) -> None:
try: try:
await tux.load_packages() await tux.load_packages()
console.print() console.print()
await tux.start(token=token, bot=True) await tux.start(token=token)
except discord.LoginFailure: except discord.LoginFailure:
log.critical("This token appears to be valid.") log.critical("This token appears to be invalid.")
console.print() console.print()
console.print( console.print(
"[prompt.invalid]This token appears to be valid. [i]exiting...[/i]" "[prompt.invalid]This token appears to be valid. [i]exiting...[/i]"

View file

@ -25,6 +25,7 @@ class Dev(commands.Cog, name="Dev"):
if crash_type == "ZeroDivisionError": if crash_type == "ZeroDivisionError":
await ctx.send(str(5 / 0)) await ctx.send(str(5 / 0))
elif crash_type == "TypeError": elif crash_type == "TypeError":
# noinspection PyTypeChecker
await ctx.send(str(int([]))) await ctx.send(str(int([])))
elif crash_type == "IndexError": elif crash_type == "IndexError":
await ctx.send(str([0][5])) await ctx.send(str([0][5]))

View file

@ -51,9 +51,9 @@ class Logs(commands.Cog, name="Logs"):
def __init__(self, bot: Tux): def __init__(self, bot: Tux):
self.bot = bot self.bot = bot
self.process = psutil.Process() self.process = psutil.Process()
self._batch_lock = asyncio.Lock(loop=bot.loop) self._batch_lock = asyncio.Lock()
self._data_batch = [] 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.gateway_worker.start() # pylint: disable=no-member
self.__config: LogsConfig = ConfigFile( self.__config: LogsConfig = ConfigFile(
@ -88,7 +88,7 @@ class Logs(commands.Cog, name="Logs"):
def webhook(self, log_type): def webhook(self, log_type):
webhook = discord.Webhook.from_url( webhook = discord.Webhook.from_url(
getattr(self.__config, log_type), getattr(self.__config, log_type),
adapter=discord.AsyncWebhookAdapter(self.bot.session), session=self.bot.session,
) )
return webhook return webhook

View file

@ -239,6 +239,7 @@ class Utils(commands.Cog, name="Utils"):
cmd = self.bot.get_command(name) cmd = self.bot.get_command(name)
if cmd: if cmd:
# noinspection PyUnresolvedReferences
src = cmd.callback.__code__ src = cmd.callback.__code__
rpath = src.co_filename rpath = src.co_filename
else: else:

View file

@ -89,11 +89,16 @@ class Tux(commands.AutoShardedBot):
self.uptime = None self.uptime = None
self.last_on_ready = None self.last_on_ready = None
self._app_owners_fetched = False # to prevent abusive API calls self._app_owners_fetched = False # to prevent abusive API calls
self.loop = asyncio.get_event_loop()
self.before_invoke(self._typing) self.before_invoke(self._typing)
super().__init__( 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) self.session = aiohttp.ClientSession(loop=self.loop)
@ -269,7 +274,7 @@ class Tux(commands.AutoShardedBot):
if not await self._is_blacklisted(message): if not await self._is_blacklisted(message):
await self.process_commands(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.""" """Connect to Discord and start all connections."""
with Progress() as progress: with Progress() as progress:
task = progress.add_task( task = progress.add_task(
@ -309,7 +314,7 @@ class Tux(commands.AutoShardedBot):
start=False, start=False,
) )
progress.update(task_id) progress.update(task_id)
await super().start(token, bot=bot) await super().start(token)
async def logout(self): async def logout(self):
"""Disconnect from Discord and closes all actives connections. """Disconnect from Discord and closes all actives connections.
@ -335,7 +340,7 @@ class Tux(commands.AutoShardedBot):
task.cancel() task.cancel()
await asyncio.gather(*pending, return_exceptions=False) await asyncio.gather(*pending, return_exceptions=False)
await super().logout() await super().close()
async def shutdown(self, *, restart: bool = False): async def shutdown(self, *, restart: bool = False):
"""Gracefully quit. """Gracefully quit.

View file

@ -14,6 +14,7 @@ __all__ = [
"search_for", "search_for",
"set_for_key", "set_for_key",
"set_for", "set_for",
"set_if_none",
] ]
log = logging.getLogger("tuxbot.core.config") log = logging.getLogger("tuxbot.core.config")

View file

@ -3,4 +3,4 @@ from rich.traceback import install
console = Console() console = Console()
install(console=console, show_locals=True) install(console=console, show_locals=True, width=console.width)

View file

@ -13,6 +13,7 @@ IP_REPLACEMENT = "■" * random.randint(3, 15)
class ContextPlus(commands.Context): class ContextPlus(commands.Context):
# noinspection PyTypedDict
async def send( async def send(
self, self,
content=None, content=None,
@ -31,6 +32,7 @@ class ContextPlus(commands.Context):
replace_in_list, replace_in_list,
) )
# todo: rewrite replacements
if content: if content:
content = ( content = (
content.replace(self.bot.config.Core.token, TOKEN_REPLACEMENT) content.replace(self.bot.config.Core.token, TOKEN_REPLACEMENT)

View file

@ -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: def init_logging(level: int, location: pathlib.Path) -> None:
"""Initialize loggers. """Initialize loggers.
@ -24,30 +45,8 @@ def init_logging(level: int, location: pathlib.Path) -> None:
Where to store Logs. Where to store Logs.
""" """
# dpy_logger = logging.getLogger("discord") _setup_logging(level, location, "discord")
# dpy_logger.setLevel(logging.WARN) _setup_logging(level, location, "tuxbot")
# 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,
)
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter) stdout_handler.setFormatter(formatter)
# dpy_handler.setFormatter(formatter)
base_handler.setFormatter(formatter)
# dpy_logger.addHandler(dpy_handler)
base_logger.addHandler(base_handler)

View file

@ -328,7 +328,7 @@ def update() -> None:
) )
if response.get("sha")[:6] == version_info.build: 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: else:
print(f"Updating to {response.get('sha')[:6]}...") print(f"Updating to {response.get('sha')[:6]}...")