From 1ed392b2fdaa9d32ed7525021db68109b6f85a42 Mon Sep 17 00:00:00 2001 From: Louis Chauvet Date: Mon, 27 Apr 2020 18:38:08 +0200 Subject: [PATCH] Version initiale --- __init__.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ infos.toml | 7 +++-- 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..864c887 --- /dev/null +++ b/__init__.py @@ -0,0 +1,91 @@ +from mod_base import BaseModule +from config.config_types import factory +import config.config_types as c_t +from config import Config +import os + + +class BaseCommand(BaseModule): + def __init__(self, client): + super().__init__(client) + self.com_config = Config(path=os.path.join(client.config["data_folder"], "commands", "config.toml")) + self.com_config.register("prefix", factory(c_t.Str)) + self.config.register("command_text", factory(c_t.Str)) + + self.com_config.set({"prefix": "!"}, no_save=True) + self.config.set({"command_text": self.name.lower()}, no_save=True) + + self.com_config.load() + self.config.load() + + @staticmethod + def _parse_command_content(content): + if not len(content.split()): + return "", [], [] + # Sub_command + sub_command = content.split()[0] + args_ = [sub_command] + kwargs = [] + if len(content.split()) > 1: + # Remove subcommand + content = content.lstrip(sub_command) + # Take the other part of command_text + content = content.lstrip().replace("\"", "\"\"") + # Splitting around quotes + quotes = [element.split("\" ") for element in content.split(" \"")] + # Split all sub chains but raw chains and flat the resulting list + args = [item.split() if item[0] != "\"" else [item, ] for sublist in quotes for item in sublist] + # Second plating + args = [item for sublist in args for item in sublist] + # args_ are arguments, kwargs are options with arguments + i = 0 + while i < len(args): + if args[i].startswith("\""): + args_.append(args[i][1:-1]) + elif args[i].startswith("--"): + if i + 1 >= len(args): + kwargs.append((args[i].lstrip("-"), None)) + break + if args[i + 1][0] != "-": + kwargs.append((args[i].lstrip("-"), args[i + 1].strip("\""))) + i += 1 + else: + kwargs.append((args[i].lstrip("-"), None)) + elif args[i].startswith("-"): + if len(args[i]) == 2: + if i + 1 >= len(args): + break + if args[i + 1][0] != "-": + kwargs.append((args[i].lstrip("-"), args[i + 1].strip("\""))) + i += 1 + else: + kwargs.append((args[i].lstrip("-"), None)) + else: + kwargs.extend([(arg, None) for arg in args[i][1:]]) + else: + args_.append(args[i]) + i += 1 + return sub_command, args_, kwargs + + async def parse_command(self, message): + command = self.com_config["prefix"] + self.config["command_text"] + if message.content.startswith(command): + content = message.content.split(command, 1)[1] + sub_command, args, kwargs = self._parse_command_content(content) + sub_command_method = "com_" + sub_command + if sub_command_method in dir(self): + await self.__getattribute__(sub_command_method)(message, args, kwargs) + else: + await self.command(message, args, kwargs) + + async def on_message(self, message): + if message.author.bot: + return + await self.parse_command(message) + + async def command(self, message, args, kwargs): + pass + + async def on_config_update(self): + super().on_config_update() + self.com_config.load() diff --git a/infos.toml b/infos.toml index 1b4ca60..eaaa2c1 100644 --- a/infos.toml +++ b/infos.toml @@ -1,9 +1,10 @@ -name = "{{REPO_NAME_TITLE}}" -description = "{{REPO_DESCRIPTION}}" +name = "Commands" +description = "Base class to handle commands" version = "0.1.0" bot_version = "~=0.1.0" -metamodule = false +metamodule = true [dependencies] +mod_base = "~=1.0.0" \ No newline at end of file