[mod-base] Send help on invalid command

This commit is contained in:
Suwako Moriya 2020-04-11 11:51:33 +02:00
parent 9f29e13938
commit 9d3488d92c
Signed by: SuwakoMmh
GPG Key ID: A27482B806F13CD5
2 changed files with 91 additions and 1 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}`
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):
await self.send_help(message.channel)