generated from PDBA/module-template
Version initiale
This commit is contained in:
parent
018781eb88
commit
1ed392b2fd
91
__init__.py
Normal file
91
__init__.py
Normal file
@ -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()
|
@ -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"
|
Loading…
Reference in New Issue
Block a user