2019-06-10 17:46:33 +02:00
|
|
|
"""Base class for module, never use directly !!!"""
|
|
|
|
import asyncio
|
2020-03-21 22:43:14 +01:00
|
|
|
import os
|
2019-08-10 09:30:46 +02:00
|
|
|
from typing import List
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
import discord
|
|
|
|
|
2019-08-10 20:29:04 +02:00
|
|
|
from config import Config
|
2020-03-21 22:43:14 +01:00
|
|
|
from storage import Objects
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BaseClass:
|
|
|
|
"""Base class for all modules, Override it to make submodules"""
|
|
|
|
name = ""
|
|
|
|
help = {
|
|
|
|
"description": "",
|
|
|
|
"commands": {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, client):
|
|
|
|
"""Initialize module class
|
|
|
|
|
|
|
|
Initialize module class, always call it to set self.client when you override it.
|
|
|
|
|
|
|
|
:param client: client instance
|
2019-08-16 19:11:21 +02:00
|
|
|
:type client: LBI"""
|
2019-06-10 17:46:33 +02:00
|
|
|
self.client = client
|
2020-04-05 13:09:16 +02:00
|
|
|
self.objects = Objects(path=os.path.join("data", self.name.lower()))
|
|
|
|
self.config = Config(parent=self.client.config, name="mod-" + self.name.lower())
|
|
|
|
self.config.init({"help_active": True, "color": 0x000000, "auth_everyone": False, "authorized_roles": [],
|
2020-04-05 14:30:10 +02:00
|
|
|
"authorized_users": [], "command_text": self.name.lower(), "configured": False})
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
async def send_help(self, channel):
|
|
|
|
embed = discord.Embed(
|
|
|
|
title="[{nom}] - Aide".format(nom=self.name),
|
2020-04-05 02:50:46 +02:00
|
|
|
description="*" + self.help["description"].format(prefix=self.client.config['prefix']) + "*",
|
2020-04-05 13:09:16 +02:00
|
|
|
color=self.config.color
|
2019-06-10 17:46:33 +02:00
|
|
|
)
|
|
|
|
for command, description in self.help["commands"].items():
|
2020-04-05 13:09:16 +02:00
|
|
|
embed.add_field(name=command.format(prefix=self.client.config['prefix'], command=self.config.command_text),
|
|
|
|
value="-> " + description.format(prefix=self.client.config['prefix'],
|
|
|
|
command=self.config.command_text),
|
2019-06-10 17:46:33 +02:00
|
|
|
inline=False)
|
|
|
|
await channel.send(embed=embed)
|
|
|
|
|
2020-04-05 12:23:02 +02:00
|
|
|
async def auth(self, user: discord.User, role_list: List[int] = None, user_list: List[int] = None,
|
|
|
|
guild: int = None):
|
2019-08-10 09:30:46 +02:00
|
|
|
"""
|
|
|
|
Return True if user is an owner of the bot or in authorized_users or he have a role in authorized_roles.
|
|
|
|
|
2020-04-05 12:23:02 +02:00
|
|
|
:param user: User to check
|
2019-08-10 09:30:46 +02:00
|
|
|
:param user_list: List of authorized users, if not specified use self.authorized_users
|
|
|
|
:param role_list: list of authorized roles, if not specified use self.authorized_roles
|
2020-04-05 12:23:02 +02:00
|
|
|
:param guild: Specific guild to search role
|
2019-08-10 09:30:46 +02:00
|
|
|
:type user_list: List[Int]
|
2019-08-10 20:29:04 +02:00
|
|
|
:type role_list: List[Int]
|
2020-04-05 12:23:02 +02:00
|
|
|
:type guild: Int
|
2019-08-10 09:30:46 +02:00
|
|
|
:type user: discord.User
|
|
|
|
"""
|
2020-04-05 13:09:16 +02:00
|
|
|
if self.config.auth_everyone:
|
|
|
|
return True
|
2019-08-10 09:30:46 +02:00
|
|
|
if user_list is None:
|
2020-04-05 12:23:02 +02:00
|
|
|
user_list = self.config.authorized_users + self.client.config.admin_users
|
2019-08-10 09:30:46 +02:00
|
|
|
if user.id in user_list:
|
|
|
|
return True
|
2020-04-05 12:23:02 +02:00
|
|
|
if role_list is None:
|
|
|
|
role_list = self.config.authorized_roles + self.client.config.admin_roles
|
|
|
|
if guild is None:
|
|
|
|
guilds = self.client.guilds
|
|
|
|
else:
|
|
|
|
guilds = [guild]
|
|
|
|
for guild in guilds:
|
2019-08-10 09:30:46 +02:00
|
|
|
if guild.get_member(user.id):
|
|
|
|
for role_id in role_list:
|
|
|
|
if role_id in [r.id for r in guild.get_member(user.id).roles]:
|
|
|
|
return True
|
|
|
|
return False
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
async def parse_command(self, message):
|
|
|
|
"""Parse a command_text from received message and execute function
|
|
|
|
%git update
|
|
|
|
com_update(m..)
|
|
|
|
Parse message like `{prefix}{command_text} subcommand` and call class method `com_{subcommand}`.
|
|
|
|
|
|
|
|
:param message: message to parse
|
|
|
|
:type message: discord.Message"""
|
2020-04-05 13:09:16 +02:00
|
|
|
if message.content.startswith(
|
|
|
|
self.client.config["prefix"] + (self.config.command_text if self.config.command_text else "")):
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
content = message.content.lstrip(
|
2020-04-05 13:09:16 +02:00
|
|
|
self.client.config["prefix"] + (self.config.command_text if self.config.command_text else ""))
|
2019-06-10 17:46:33 +02:00
|
|
|
sub_command, args, kwargs = self._parse_command_content(content)
|
|
|
|
sub_command = "com_" + sub_command
|
2019-08-08 22:43:46 +02:00
|
|
|
if await self.auth(message.author):
|
|
|
|
if sub_command in dir(self):
|
|
|
|
await self.__getattribute__(sub_command)(message, args, kwargs)
|
|
|
|
else:
|
|
|
|
await self.command(message, [sub_command[4:]] + args, kwargs)
|
2019-06-10 17:46:33 +02:00
|
|
|
else:
|
2019-08-10 09:30:46 +02:00
|
|
|
await self.unauthorized(message)
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _parse_command_content(content):
|
|
|
|
"""Parse string
|
|
|
|
|
|
|
|
Parse string like `subcommand argument "argument with spaces" -o -shortwaytopassoncharacteroption --longoption
|
|
|
|
-o "option with argument"`. You can override this function to change parsing.
|
|
|
|
|
|
|
|
:param content: content to parse
|
|
|
|
:type content: str
|
|
|
|
|
|
|
|
:return: parsed arguments: [subcommand, [arg1, arg2, ...], [(option1, arg1), (option2, arg2), ...]]
|
|
|
|
:rtype: list[str, list, list]"""
|
|
|
|
if not len(content.split()):
|
|
|
|
return "", [], []
|
|
|
|
# Sub_command
|
|
|
|
sub_command = content.split()[0]
|
|
|
|
args_ = []
|
|
|
|
kwargs = []
|
|
|
|
if len(content.split()) > 1:
|
|
|
|
# Take the other part of command_text
|
|
|
|
content = content.split(" ", 1)[1].replace("\"", "\"\"")
|
|
|
|
# Splitting around quotes
|
|
|
|
quotes = [element.split("\" ") for element in content.split(" \"")]
|
|
|
|
# Split all sub chains but brute 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
|
|
|
|
|
2020-03-19 18:29:58 +01:00
|
|
|
async def on_message(self, message: discord.Message):
|
2019-06-10 17:46:33 +02:00
|
|
|
"""Override this function to deactivate command_text parsing"""
|
2020-04-05 14:30:10 +02:00
|
|
|
if message.author.bot:
|
|
|
|
return
|
2019-06-10 17:46:33 +02:00
|
|
|
await self.parse_command(message)
|
|
|
|
|
|
|
|
async def command(self, message, args, kwargs):
|
|
|
|
"""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
|
|
|
|
|
2020-04-01 12:39:40 +02:00
|
|
|
async def com_help(self, message, args, kwargs):
|
|
|
|
await self.send_help(message.channel)
|
|
|
|
|
2019-08-08 22:43:46 +02:00
|
|
|
async def unauthorized(self, message):
|
|
|
|
await message.channel.send("Vous n'êtes pas autorisé à effectuer cette commande")
|
2019-06-10 17:46:33 +02:00
|
|
|
|
|
|
|
def dispatch(self, event, *args, **kwargs):
|
|
|
|
# Method to call
|
|
|
|
method = 'on_' + event
|
|
|
|
try:
|
|
|
|
# Try to get coro, if not exists pass without raise an error
|
|
|
|
coro = getattr(self, method)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Run event
|
2019-08-08 22:43:46 +02:00
|
|
|
asyncio.ensure_future(self.client._run_event(coro, method, *args, **kwargs), loop=self.client.loop)
|
2019-08-11 18:07:58 +02:00
|
|
|
|
|
|
|
async def on_error(self, event_method, *args, **kwargs):
|
|
|
|
pass
|