From bca15ac34db1d72325c322e1f526a085db9ecbea Mon Sep 17 00:00:00 2001 From: Louis Chauvet Date: Mon, 20 Apr 2020 19:54:03 +0200 Subject: [PATCH] =?UTF-8?q?[apero]=20J'ai=20ap=C3=A9ro=20pour=20l'aniv=20d?= =?UTF-8?q?e=20ma=20maman?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/base.py | 10 +++++++--- main.py | 2 ++ modules/base/Base.py | 22 ++++++++++++---------- modules/errors/__init__.py | 10 +++++----- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/config/base.py b/config/base.py index a17af81..cd9f331 100644 --- a/config/base.py +++ b/config/base.py @@ -87,7 +87,7 @@ class Config: with open(self.path, 'w') as file: toml.dump({k: v.to_save() for k, v in self.fields.items()}, file) - def load(self) -> None: + def load(self, create: bool = False) -> None: """ Load config from ``self.file`` @@ -105,10 +105,14 @@ class Config: 3 + :param create: Create config file if not exists :return: None """ - with open(self.path, 'r') as file: - self.set(toml.load(file)) + try: + with open(self.path, 'r') as file: + self.set(toml.load(file)) + except FileNotFoundError: + self.save() def __getitem__(self, item: str) -> Any: """ diff --git a/main.py b/main.py index 4dff686..b19ff73 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ import locale import logging import logging.config import os +import traceback from typing import Dict import discord @@ -369,6 +370,7 @@ class LBI(discord.Client): # @async_event async def on_error(self, event_method, *args, **kwargs): # This event is special because it is call directly + self.error(traceback.format_exc()) for module in self.modules.values(): await module["initialized_class"].on_error(event_method, *args, **kwargs) diff --git a/modules/base/Base.py b/modules/base/Base.py index 7d0d84f..d5c1cef 100644 --- a/modules/base/Base.py +++ b/modules/base/Base.py @@ -41,19 +41,21 @@ class BaseClass: self.config.register("command_text", factory(config_types.Str)) self.config.register("configured", factory(config_types.Bool)) self.config.set({"help_active": True, "color": 0x000000, "auth_everyone": False, "authorized_roles": [], - "authorized_users": [], "command_text": self.name.lower(), "configured": False}) + "authorized_users": [], "command_text": self.name.lower(), "configured": True}) + self.config.load(create=True) async def send_help(self, channel): embed = discord.Embed( title="[{nom}] - Aide".format(nom=self.name), description="*" + self.help["description"].format(prefix=self.client.config['prefix']) + "*", - color=self.config.color + color=self.config["color"] ) for command, description in self.help["commands"].items(): - embed.add_field(name=command.format(prefix=self.client.config['prefix'], command=self.config.command_text), - value="-> " + description.format(prefix=self.client.config['prefix'], - command=self.config.command_text), - inline=False) + embed.add_field( + name=command.format(prefix=self.client.config['prefix'], command=self.config["command_text"]), + value="-> " + description.format(prefix=self.client.config['prefix'], + command=self.config["command_text"]), + inline=False) await channel.send(embed=embed) def auth(self, user: discord.User, role_list: List[int] = None, user_list: List[int] = None, @@ -70,14 +72,14 @@ class BaseClass: :type guild: Int :type user: discord.User """ - if self.config.auth_everyone: + if self.config["auth_everyone"]: return True if user_list is None: - user_list = self.config.authorized_users + self.client.config.admin_users + user_list = self.config["authorized_users"] + self.client.config['admin_users'] if user.id in user_list: return True if role_list is None: - role_list = self.config.authorized_roles + self.client.config.admin_roles + role_list = self.config["authorized_roles"] + self.client.config['admin_roles'] if guild is None: guilds = self.client.guilds else: @@ -95,7 +97,7 @@ class BaseClass: :param message: message to parse :type message: discord.Message""" - command = self.client.config["prefix"] + (self.config.command_text if self.config.command_text else "") + command = self.client.config["prefix"] + (self.config["command_text"] if self.config["command_text"] else "") if message.content.startswith(command): content = message.content.split(" ", 1)[1 if " " in message.content else 0] sub_command, args, kwargs = self._parse_command_content(content) diff --git a/modules/errors/__init__.py b/modules/errors/__init__.py index 51b85a5..751bec3 100644 --- a/modules/errors/__init__.py +++ b/modules/errors/__init__.py @@ -72,21 +72,21 @@ class MainClass(BaseClassPython): embed = discord.Embed( title="[Erreur] Aïe :/", description="```python\n{0}```".format(traceback.format_exc()), - color=self.config.color) - embed.set_image(url=random.choice(self.config.memes)) + color=self.config["color"]) + embed.set_image(url=random.choice(self.config["memes"])) message_list = None # Send message to dev channels - for chanid in self.config.dev_chan: + for chanid in self.config["dev_chan"]: try: await self.client.get_channel(chanid).send( - embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.config.icon)) + embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.config["icon"])) except BaseException as e: raise e # Send message to current channel if exists if channel is not None: message = await channel.send(embed=embed.set_footer(text="Ce message va s'autodétruire dans une minute", - icon_url=self.config.icon)) + icon_url=self.config["icon"])) msg_id = {"channel_id": message.channel.id, "msg_id": message.id} self.errorsList.append(msg_id) # Save message in errorsList now to keep them if a reboot happend during next 60 seconds