Merge branch 'master' of moriya.zapto.org:PDBA/bot-base

This commit is contained in:
Louis Chauvet 2020-04-14 02:40:36 +02:00
commit 52c26fa9d0
Signed by: fomys
GPG Key ID: 1ECA046A9615ABA0
3 changed files with 171 additions and 77 deletions

90
config/Types.py Normal file
View File

@ -0,0 +1,90 @@
from __future__ import annotations
from typing import Dict, Any, Optional
class Config:
name: Optional[str]
parent: Optional[Config]
config: Dict[Any, Any]
def __init__(self, parent: Config = None, name: str = None, client: LBI = None):
"""Create Config
:param parent: Parent configuration
:param name: Configuration name
:type parent: Config
:type name: str"""
self.parent = parent
self.config = dict()
self.name = None
self.client = client
if self.parent:
self.name = name
self.client = self.parent.client
def init(self, config):
"""Load default configuration
:param config: Default configuration
:type config: dict
:return: None"""
# Load data from config file before initialisation
self.load()
# Get data from parent
if self.parent is not None:
self.parent.config[self.name] = self.parent.config.get(self.name) if self.parent.config.get(
self.name) is not None else self.config
self.config = self.parent.config[self.name]
# Set config only if not already defined
for k, v in config.items():
self.config[k] = self.config.get(k) if self.config.get(k) is not None else v
# Save new datas
self.save()
def _save(self):
"""Internal function for save
Must be overridden by all type of config to handle saving"""
# Call parent save if necessary
if self.parent:
self.parent.save()
def save(self):
"""Public save function
Do not override"""
self._save()
def _load(self):
"""Internal function for load
Mus be overridden by all type of config to handle loading"""
# Load parent if necessary
if self.parent:
self.parent.load()
self.config = self.parent.config.get(self.name)
# Initialize parent if necessary
if self.config is None:
self.parent.config[self.name] = {}
self.config = {}
def load(self):
"""Public load function
Do not override"""
self._load()
def __getattr__(self, item):
return self.config.get(item)
def __getitem__(self, item):
return self.config.get(item)
def __setitem__(self, key, value):
if self.parent:
self.parent[self.name][key] = value
self.config = self.parent[self.name]
else:
self.config[key] = value
self.save()

View File

@ -167,7 +167,7 @@ class BaseClass:
"""Override this function to handle all messages starting with `{prefix}{command_text}` """Override this function to handle all messages starting with `{prefix}{command_text}`
Function which is executed for all command_text doesn't match with a `com_{subcommand}` function""" Function which is executed for all command_text doesn't match with a `com_{subcommand}` function"""
pass await self.send_help(message.channel)
async def com_help(self, message, args, kwargs): async def com_help(self, message, args, kwargs):
await self.send_help(message.channel) await self.send_help(message.channel)

View File

@ -30,6 +30,9 @@ class MainClass(BaseClassPython):
self.config.register("channel", factory(config.config_types.Channel, self.client)) self.config.register("channel", factory(config.config_types.Channel, self.client))
self.history = {} self.history = {}
async def on_ready(self):
await self.fill_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: if message.author.bot:
@ -114,6 +117,7 @@ class MainClass(BaseClassPython):
async def com_stats(self, message: discord.Message, args, kwargs): async def com_stats(self, message: discord.Message, args, kwargs):
# TODO: Finir sum # TODO: Finir sum
async with message.channel.typing():
if not ((not False or (not False or not ("sum" in args))) or not True): if not ((not False or (not False or not ("sum" in args))) or not True):
if message.mentions: if message.mentions:
top = self.get_top(only_users=[mention.id for mention in message.mentions] + [message.author.id]) top = self.get_top(only_users=[mention.id for mention in message.mentions] + [message.author.id])