2019-06-05 02:15:29 +02:00
|
|
|
"""Base class for module, never use directly !!!"""
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import discord
|
|
|
|
import lupa
|
|
|
|
|
2019-06-10 17:46:33 +02:00
|
|
|
from modules.base.Base import BaseClass
|
2019-06-05 02:15:29 +02:00
|
|
|
|
|
|
|
|
2019-06-10 17:46:33 +02:00
|
|
|
class BaseClassLua(BaseClass):
|
2019-06-05 02:15:29 +02:00
|
|
|
"""Base class for all modules, Override it to make submodules"""
|
|
|
|
name = ""
|
|
|
|
help = {
|
|
|
|
"description": "",
|
|
|
|
"commands": {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
help_active = False
|
|
|
|
color = 0x000000
|
|
|
|
command_text = None
|
2019-08-10 09:30:46 +02:00
|
|
|
authorized_users = []
|
2019-06-05 02:15:29 +02:00
|
|
|
authorized_roles = []
|
2020-03-21 22:43:14 +01:00
|
|
|
command_text = ""
|
2019-06-05 02:15:29 +02:00
|
|
|
|
2019-06-10 17:46:33 +02:00
|
|
|
def __init__(self, client, path):
|
2019-06-05 02:15:29 +02:00
|
|
|
"""Initialize module class
|
|
|
|
|
|
|
|
Initialize module class, always call it to set self.client when you override it.
|
|
|
|
|
|
|
|
:param client: client instance
|
|
|
|
:type client: NikolaTesla"""
|
2019-06-10 17:46:33 +02:00
|
|
|
super().__init__(client)
|
2019-06-05 02:15:29 +02:00
|
|
|
# Get lua globals
|
|
|
|
self.lua = lupa.LuaRuntime(unpack_returned_tuples=True)
|
2019-06-10 17:46:33 +02:00
|
|
|
self.luaMethods = self.lua.require(path)
|
2019-06-05 02:15:29 +02:00
|
|
|
|
2019-08-08 22:43:46 +02:00
|
|
|
def call(self, method, *args, **kwargs):
|
|
|
|
# Try to run lua method then python one
|
|
|
|
if self.luaMethods[method] is not None:
|
|
|
|
async def coro(*args, **kwargs):
|
|
|
|
self.luaMethods[method](self, asyncio.ensure_future, discord, *args, *kwargs)
|
|
|
|
asyncio.ensure_future(self.client._run_event(coro, method, *args, **kwargs), loop=self.client.loop)
|
|
|
|
try:
|
|
|
|
coro = getattr(self, method)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
asyncio.ensure_future(self.client._run_event(coro, method, *args, **kwargs), loop=self.client.loop)
|
|
|
|
|
2019-06-05 02:15:29 +02:00
|
|
|
def dispatch(self, event, *args, **kwargs):
|
2019-06-10 17:46:33 +02:00
|
|
|
method = "on_"+event
|
2019-08-08 22:55:07 +02:00
|
|
|
self.call(method, *args, **kwargs)
|
2019-06-10 17:46:33 +02:00
|
|
|
|
2019-08-08 22:43:46 +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}`.
|
2019-06-10 17:46:33 +02:00
|
|
|
|
2019-08-08 22:43:46 +02:00
|
|
|
:param message: message to parse
|
|
|
|
:type message: discord.Message"""
|
|
|
|
if message.content.startswith(self.client.config["prefix"] + (self.command_text if self.command_text else "")):
|
|
|
|
|
|
|
|
content = message.content.lstrip(
|
|
|
|
self.client.config["prefix"] + (self.command_text if self.command_text else ""))
|
|
|
|
sub_command, args, kwargs = self._parse_command_content(content)
|
|
|
|
sub_command = "com_" + sub_command
|
2020-04-05 20:48:00 +02:00
|
|
|
if self.auth(message.author):
|
|
|
|
self.call(sub_command, args, kwargs)
|
2019-08-08 22:43:46 +02:00
|
|
|
else:
|
2019-08-08 22:55:07 +02:00
|
|
|
await self.unauthorized(message)
|
2019-06-05 02:15:29 +02:00
|
|
|
|