diff --git a/main.py b/main.py index 09e12d3..d71f4e4 100644 --- a/main.py +++ b/main.py @@ -292,7 +292,7 @@ class LBI(discord.Client): self.config.save() except AttributeError as e: self.error("Module {module} doesn't have MainClass.".format(module=module)) - return e + raise e return 0 elif MODULES[module].type == "lua": self.info(f"Start loading module {module}...") @@ -395,11 +395,18 @@ class ClientById: channel = self.client.get_channel(id_) return channel.send(*args, **kwargs) - async def get_role(self, id_): - for guild in self.client.guilds: - role = discord.utils.get(guild.roles, id=id_) - if role: - return role + async def get_role(self, id_=None, name=None): + if id_: + for guild in self.client.guilds: + role = discord.utils.get(guild.roles, id=id_) + if role: + return role + if name: + for guild in self.client.guilds: + print(list(r.name for r in guild.roles)) + role = discord.utils.get(guild.roles, name=name) + if role: + return role return None diff --git a/modules/base/Base.py b/modules/base/Base.py index 584c6ef..32269d0 100644 --- a/modules/base/Base.py +++ b/modules/base/Base.py @@ -1,12 +1,12 @@ """Base class for module, never use directly !!!""" import asyncio +import os from typing import List import discord from config import Config -from storage import FSStorage, FSObjects -import storage.path as path +from storage import Objects class BaseClass: @@ -32,9 +32,8 @@ class BaseClass: :param client: client instance :type client: LBI""" self.client = client - self.storage = FSStorage(path.join(self.client.base_path, self.name)) - self.objects = FSObjects(self.storage) - self.config = Config(parent=self.client.config, name="mod-"+self.name) + self.objects = Objects(path=os.path.join("data", self.name)) + self.config = Config(parent=self.client.config, name="mod-" + self.name) self.config.init({"authorized_roles": self.authorized_roles, "authorized_users": self.authorized_users}) async def send_help(self, channel): diff --git a/modules/base/BaseLua.py b/modules/base/BaseLua.py index 3bf7053..187d223 100644 --- a/modules/base/BaseLua.py +++ b/modules/base/BaseLua.py @@ -1,16 +1,10 @@ """Base class for module, never use directly !!!""" import asyncio -import os -import sys -import pickle -import traceback import discord import lupa from modules.base.Base import BaseClass -from storage import FSStorage -import storage.path as path class BaseClassLua(BaseClass): @@ -27,7 +21,7 @@ class BaseClassLua(BaseClass): command_text = None authorized_users = [] authorized_roles = [] - command_text = "lua" + command_text = "" def __init__(self, client, path): """Initialize module class diff --git a/modules/errors/__init__.py b/modules/errors/__init__.py index 541bf04..9fa3961 100644 --- a/modules/errors/__init__.py +++ b/modules/errors/__init__.py @@ -28,8 +28,8 @@ class MainClass(BaseClassPython): self.errorsList = None async def on_load(self): - if await self.objects.save_exists('errorsList'): - self.errorsList = await self.objects.load_object('errorsList') + if self.objects.save_exists('errorsList'): + self.errorsList = self.objects.load_object('errorsList') else: self.errorsList = [] @@ -87,7 +87,7 @@ class MainClass(BaseClassPython): 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 - await self.objects.save_object('errorsList', self.errorsList) + self.objects.save_object('errorsList', self.errorsList) # Wait 60 seconds and delete message await asyncio.sleep(60) diff --git a/modules/modules/__init__.py b/modules/modules/__init__.py index 96f6b04..4ecc1d9 100644 --- a/modules/modules/__init__.py +++ b/modules/modules/__init__.py @@ -28,7 +28,7 @@ class MainClass(BaseClassPython): def __init__(self, client): super().__init__(client) - self.storage.mkdir("modules", exist_ok=True) + os.makedirs("modules", exist_ok=True) self.api = Api() @staticmethod diff --git a/modules/perdu/__init__.py b/modules/perdu/__init__.py index 0a04ee3..6312dab 100644 --- a/modules/perdu/__init__.py +++ b/modules/perdu/__init__.py @@ -26,13 +26,15 @@ class MainClass(BaseClassPython): self.config.init({"channel": 0, "lost_role": 0, "min_delta": datetime.timedelta(minutes=26).total_seconds()}) async def on_load(self): - if await self.objects.save_exists('history'): - self.history = await self.objects.load_object('history') + if self.objects.save_exists('history'): + self.history = self.objects.load_object('history') else: self.history = {} async def on_message(self, message: discord.Message): # Fill history + if message.author.bot: + return if message.channel.id == self.config.channel: if message.author.id not in self.history.keys(): # Add new user if not found @@ -44,7 +46,7 @@ class MainClass(BaseClassPython): delta = message.created_at - self.history[message.author.id][-1][0] if delta.total_seconds() >= self.config.min_delta: self.history[message.author.id].append((message.created_at, delta)) - await self.objects.save_object("history", self.history) + self.objects.save_object("history", self.history) await self.parse_command(message) async def fill_history(self): @@ -58,7 +60,7 @@ class MainClass(BaseClassPython): delta = self.history[message.author.id][-1][0] - message.created_at if delta.total_seconds() >= self.config.min_delta: self.history[message.author.id].append((message.created_at, delta)) - await self.objects.save_object("history", self.history) + self.objects.save_object("history", self.history) def get_top(self, top=10, since=datetime.datetime(year=1, month=1, day=1)): """Return [(userid, [(date, delta), (date,delta), ...]), ... ]""" diff --git a/storage/FileSystem.py b/storage/FileSystem.py deleted file mode 100644 index 9a9f913..0000000 --- a/storage/FileSystem.py +++ /dev/null @@ -1,78 +0,0 @@ -import os - -from storage.base import Storage, Objects - -class FSStorage(Storage): - """ - Simple filesystem storage - """ - - def __init__(self, base_path="storage"): - super().__init__() - self.base_path = os.path.abspath(base_path) - os.makedirs(self.base_path, exist_ok=True) - self.current_dir = "/" - - def _topath(self, path): - """Transform a path to a full path""" - return os.path.join(self.base_path, path) # TODO: Modifier ca - if path.startswith("/"): - return os.path.join(self.base_path, # Always add baspath to avoid going outside protected zone - os.path.abspath(os.path.join(self.base_path, - os.path.normpath(path))).lstrip(self.base_path)) - else: - return os.path.join(self.base_path, # Always add baspath to avoid going outside protected zone - os.path.abspath(os.path.join(self.base_path, - self.current_dir, - os.path.normpath(path))).lstrip(self.base_path)) - - def access(self, path): - # Normalize path and transform it to absolute path, remove base_path part and add it again to avoid going - # outside protected folder - return os.access(self._topath(path)) - - def chdir(self, path): - self.current_dir = self._topath(path) - return self.current_dir - - def getcwd(self): - return self.current_dir - - def listdir(self, path="."): - return os.listdir(self._topath(path)) - - def mkdir(self, path, exist_ok=False): - if exist_ok: - if self.exists(path): - return self._topath(path) - os.mkdir(self._topath(path)) - return self._topath(path) - - def makedirs(self, path, exist_ok=False): - os.makedirs(self._topath(path), exist_ok=exist_ok) - return self._topath(path) - - def remove(self, path): - os.remove(self._topath(path)) - - def rename(self, src, dst): - os.rename(self._topath(src), self._topath(dst)) - return self._topath(dst) - - def rmdir(self, path): - os.rmdir(self._topath(path)) - - def sync(self): - os.sync() - - def open(self, path, mode): - return open(self._topath(path), mode) - - def exists(self, path): - return os.path.exists(self._topath(path)) - - def isdir(self, path): - return os.path.isdir(self._topath(path)) - -class FSObjects(Objects): - pass \ No newline at end of file diff --git a/storage/__init__.py b/storage/__init__.py index d6d9c7d..3366166 100644 --- a/storage/__init__.py +++ b/storage/__init__.py @@ -1,2 +1 @@ -from storage.FileSystem import FSStorage -from storage.FileSystem import FSObjects \ No newline at end of file +from .objects import Objects diff --git a/storage/base.py b/storage/base.py deleted file mode 100644 index a868e6e..0000000 --- a/storage/base.py +++ /dev/null @@ -1,141 +0,0 @@ -import json -import storage.jsonenc -from storage import path as pth - - -class Storage: - """Basic class for storage interface - - All path are on unix format (`/folder1/folder2/file`) - """ - - def __init__(self): - pass - - async def access(self, path): - """ - Return if path is accessible - :param path: Path to check - :return: Boolean - """ - pass - - async def chdir(self, path): - """ - Change working directory for this storage module - :param path: Path to go - :return: New path - """ - pass - - async def getcwd(self): - """ - Get current working directory - :return: Current working directory - """ - pass - - async def listdir(self, path="."): - """ - List all files and folders in directory `path` - :param path: Folder to list - :return: List of filename - """ - pass - - async def mkdir(self, path): - """ - Create directory `path` - :param path: directory to create - :return: Path to new directory - """ - pass - - async def makedirs(self, path, exist_ok=False): - """ - Create directory `path` - :param exist_ok: Not return error if dir exists - :param path: directory to create - :return: Path to new directory - """ - pass - - async def remove(self, path): - """ - Remove file `path` - :param path: File to remove - :return: None - """ - pass - - async def rename(self, src, dst): - """ - Rename file `src` to `dst` - :param src: Source file - :param dst: Destination file - :return: New path - """ - pass - - async def rmdir(self, path): - """ - Remove dir `path` - :param path: Directory to remove - :return: None - """ - pass - - async def sync(self): - """ - Force writing everything on disk (or storage support) - :return: None - """ - pass - - async def open(self, path, mode): - """ - Return a file object - :param path: Path of file - :param mode: mode to open file - :return: file object - """ - pass - - async def exists(self, path): - """ - Return if a file or a folder exists - :param path: Path to test - :return: True if file exists - """ - pass - - async def isdir(self, path): - """ - Return if path is a directory - :param path: Path to test - :return: True if path is a directory - """ - pass - - -class Objects: - storage: Storage - - def __init__(self, storage: Storage): - self.storage = storage - self.storage.makedirs("objects", exist_ok=True) - - async def save_object(self, object_name, object_instance): - """Save object into json file""" - with self.storage.open(pth.join("objects", object_name + ".json"), "w") as f: - json.dump([object_instance], f, cls=storage.jsonenc.Encoder) - - async def load_object(self, object_name): - """Load object from json file""" - if await self.save_exists(object_name): - with self.storage.open(pth.join("objects", object_name + ".json"), "r") as f: - return json.load(f, object_hook=storage.jsonenc.hook)[0] - - async def save_exists(self, object_name): - """Check if json file exists""" - return self.storage.exists(pth.join("objects", object_name + ".json")) diff --git a/storage/jsonenc.py b/storage/jsonencoder.py similarity index 100% rename from storage/jsonenc.py rename to storage/jsonencoder.py index 40ebd64..a4dff78 100644 --- a/storage/jsonenc.py +++ b/storage/jsonencoder.py @@ -1,5 +1,5 @@ -import json import datetime +import json class Encoder(json.JSONEncoder): diff --git a/storage/objects.py b/storage/objects.py new file mode 100644 index 0000000..01856e1 --- /dev/null +++ b/storage/objects.py @@ -0,0 +1,25 @@ +import json +import os + +from storage import jsonencoder + + +class Objects: + def __init__(self, path: str): + self.path = os.path.abspath(path) + os.makedirs(os.path.join(self.path, "objects"), exist_ok=True) + + def save_object(self, object_name, object_instance): + """Save object into json file""" + with open(os.path.join(self.path, "objects", object_name + ".json"), "w") as file: + json.dump([object_instance], file, cls=jsonencoder.Encoder) + + def load_object(self, object_name): + """Load object from json file""" + if self.save_exists(object_name): + with open(os.path.join(self.path, "objects", object_name + ".json"), "r") as f: + return json.load(f, object_hook=jsonencoder.hook)[0] + + def save_exists(self, object_name): + """Check if json file exists""" + return os.access(os.path.join(self.path, "objects", object_name + ".json"), os.R_OK | os.W_OK) diff --git a/storage/path.py b/storage/path.py deleted file mode 100644 index ccf5837..0000000 --- a/storage/path.py +++ /dev/null @@ -1,7 +0,0 @@ -def join(*args): - """ - Join list of path - :param args: List of path - :return: Joined path - """ - return "/".join(args)