[base-storage] hophophop on supprime cte merde
This commit is contained in:
parent
9beffc9e39
commit
26aa88e836
19
main.py
19
main.py
@ -292,7 +292,7 @@ class LBI(discord.Client):
|
|||||||
self.config.save()
|
self.config.save()
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
self.error("Module {module} doesn't have MainClass.".format(module=module))
|
self.error("Module {module} doesn't have MainClass.".format(module=module))
|
||||||
return e
|
raise e
|
||||||
return 0
|
return 0
|
||||||
elif MODULES[module].type == "lua":
|
elif MODULES[module].type == "lua":
|
||||||
self.info(f"Start loading module {module}...")
|
self.info(f"Start loading module {module}...")
|
||||||
@ -395,11 +395,18 @@ class ClientById:
|
|||||||
channel = self.client.get_channel(id_)
|
channel = self.client.get_channel(id_)
|
||||||
return channel.send(*args, **kwargs)
|
return channel.send(*args, **kwargs)
|
||||||
|
|
||||||
async def get_role(self, id_):
|
async def get_role(self, id_=None, name=None):
|
||||||
for guild in self.client.guilds:
|
if id_:
|
||||||
role = discord.utils.get(guild.roles, id=id_)
|
for guild in self.client.guilds:
|
||||||
if role:
|
role = discord.utils.get(guild.roles, id=id_)
|
||||||
return role
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
"""Base class for module, never use directly !!!"""
|
"""Base class for module, never use directly !!!"""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import os
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
from config import Config
|
from config import Config
|
||||||
from storage import FSStorage, FSObjects
|
from storage import Objects
|
||||||
import storage.path as path
|
|
||||||
|
|
||||||
|
|
||||||
class BaseClass:
|
class BaseClass:
|
||||||
@ -32,9 +32,8 @@ class BaseClass:
|
|||||||
:param client: client instance
|
:param client: client instance
|
||||||
:type client: LBI"""
|
:type client: LBI"""
|
||||||
self.client = client
|
self.client = client
|
||||||
self.storage = FSStorage(path.join(self.client.base_path, self.name))
|
self.objects = Objects(path=os.path.join("data", self.name))
|
||||||
self.objects = FSObjects(self.storage)
|
self.config = Config(parent=self.client.config, name="mod-" + 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})
|
self.config.init({"authorized_roles": self.authorized_roles, "authorized_users": self.authorized_users})
|
||||||
|
|
||||||
async def send_help(self, channel):
|
async def send_help(self, channel):
|
||||||
|
@ -1,16 +1,10 @@
|
|||||||
"""Base class for module, never use directly !!!"""
|
"""Base class for module, never use directly !!!"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import pickle
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
import lupa
|
import lupa
|
||||||
|
|
||||||
from modules.base.Base import BaseClass
|
from modules.base.Base import BaseClass
|
||||||
from storage import FSStorage
|
|
||||||
import storage.path as path
|
|
||||||
|
|
||||||
|
|
||||||
class BaseClassLua(BaseClass):
|
class BaseClassLua(BaseClass):
|
||||||
@ -27,7 +21,7 @@ class BaseClassLua(BaseClass):
|
|||||||
command_text = None
|
command_text = None
|
||||||
authorized_users = []
|
authorized_users = []
|
||||||
authorized_roles = []
|
authorized_roles = []
|
||||||
command_text = "lua"
|
command_text = ""
|
||||||
|
|
||||||
def __init__(self, client, path):
|
def __init__(self, client, path):
|
||||||
"""Initialize module class
|
"""Initialize module class
|
||||||
|
@ -28,8 +28,8 @@ class MainClass(BaseClassPython):
|
|||||||
self.errorsList = None
|
self.errorsList = None
|
||||||
|
|
||||||
async def on_load(self):
|
async def on_load(self):
|
||||||
if await self.objects.save_exists('errorsList'):
|
if self.objects.save_exists('errorsList'):
|
||||||
self.errorsList = await self.objects.load_object('errorsList')
|
self.errorsList = self.objects.load_object('errorsList')
|
||||||
else:
|
else:
|
||||||
self.errorsList = []
|
self.errorsList = []
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ class MainClass(BaseClassPython):
|
|||||||
msg_id = {"channel_id": message.channel.id, "msg_id": message.id}
|
msg_id = {"channel_id": message.channel.id, "msg_id": message.id}
|
||||||
self.errorsList.append(msg_id)
|
self.errorsList.append(msg_id)
|
||||||
# Save message in errorsList now to keep them if a reboot happend during next 60 seconds
|
# 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
|
# Wait 60 seconds and delete message
|
||||||
await asyncio.sleep(60)
|
await asyncio.sleep(60)
|
||||||
|
@ -28,7 +28,7 @@ class MainClass(BaseClassPython):
|
|||||||
|
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
super().__init__(client)
|
super().__init__(client)
|
||||||
self.storage.mkdir("modules", exist_ok=True)
|
os.makedirs("modules", exist_ok=True)
|
||||||
self.api = Api()
|
self.api = Api()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -26,13 +26,15 @@ class MainClass(BaseClassPython):
|
|||||||
self.config.init({"channel": 0, "lost_role": 0, "min_delta": datetime.timedelta(minutes=26).total_seconds()})
|
self.config.init({"channel": 0, "lost_role": 0, "min_delta": datetime.timedelta(minutes=26).total_seconds()})
|
||||||
|
|
||||||
async def on_load(self):
|
async def on_load(self):
|
||||||
if await self.objects.save_exists('history'):
|
if self.objects.save_exists('history'):
|
||||||
self.history = await self.objects.load_object('history')
|
self.history = self.objects.load_object('history')
|
||||||
else:
|
else:
|
||||||
self.history = {}
|
self.history = {}
|
||||||
|
|
||||||
async def on_message(self, message: discord.Message):
|
async def on_message(self, message: discord.Message):
|
||||||
# Fill history
|
# Fill history
|
||||||
|
if message.author.bot:
|
||||||
|
return
|
||||||
if message.channel.id == self.config.channel:
|
if message.channel.id == self.config.channel:
|
||||||
if message.author.id not in self.history.keys():
|
if message.author.id not in self.history.keys():
|
||||||
# Add new user if not found
|
# Add new user if not found
|
||||||
@ -44,7 +46,7 @@ class MainClass(BaseClassPython):
|
|||||||
delta = message.created_at - self.history[message.author.id][-1][0]
|
delta = message.created_at - self.history[message.author.id][-1][0]
|
||||||
if delta.total_seconds() >= self.config.min_delta:
|
if delta.total_seconds() >= self.config.min_delta:
|
||||||
self.history[message.author.id].append((message.created_at, 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)
|
await self.parse_command(message)
|
||||||
|
|
||||||
async def fill_history(self):
|
async def fill_history(self):
|
||||||
@ -58,7 +60,7 @@ class MainClass(BaseClassPython):
|
|||||||
delta = self.history[message.author.id][-1][0] - message.created_at
|
delta = self.history[message.author.id][-1][0] - message.created_at
|
||||||
if delta.total_seconds() >= self.config.min_delta:
|
if delta.total_seconds() >= self.config.min_delta:
|
||||||
self.history[message.author.id].append((message.created_at, 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)):
|
def get_top(self, top=10, since=datetime.datetime(year=1, month=1, day=1)):
|
||||||
"""Return [(userid, [(date, delta), (date,delta), ...]), ... ]"""
|
"""Return [(userid, [(date, delta), (date,delta), ...]), ... ]"""
|
||||||
|
@ -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
|
|
@ -1,2 +1 @@
|
|||||||
from storage.FileSystem import FSStorage
|
from .objects import Objects
|
||||||
from storage.FileSystem import FSObjects
|
|
||||||
|
141
storage/base.py
141
storage/base.py
@ -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"))
|
|
@ -1,5 +1,5 @@
|
|||||||
import json
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class Encoder(json.JSONEncoder):
|
class Encoder(json.JSONEncoder):
|
25
storage/objects.py
Normal file
25
storage/objects.py
Normal file
@ -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)
|
@ -1,7 +0,0 @@
|
|||||||
def join(*args):
|
|
||||||
"""
|
|
||||||
Join list of path
|
|
||||||
:param args: List of path
|
|
||||||
:return: Joined path
|
|
||||||
"""
|
|
||||||
return "/".join(args)
|
|
Loading…
Reference in New Issue
Block a user