Faut bien commit de temps en temps

This commit is contained in:
Fomys 2019-08-08 22:43:46 +02:00
parent c99bc0a723
commit b22744b802
Signed by: fomys
GPG Key ID: 1ECA046A9615ABA0
9 changed files with 82 additions and 59 deletions

2
.gitignore vendored
View File

@ -67,3 +67,5 @@ temp.zip
config/* config/*
.idea/* .idea/*
venv/*
data/*

12
main.py
View File

@ -215,7 +215,7 @@ def load_modules_info():
class LBI(discord.Client): class LBI(discord.Client):
base_path = "storage" base_path = "data"
debug = log_LBI.debug debug = log_LBI.debug
info = log_LBI.info info = log_LBI.info
warning = log_LBI.warning warning = log_LBI.warning
@ -344,10 +344,18 @@ class LBI(discord.Client):
@event @event
def dispatch(self, event, *args, **kwargs): def dispatch(self, event, *args, **kwargs):
# Dispatch to handle wait_* commands
super().dispatch(event, *args, **kwargs) super().dispatch(event, *args, **kwargs)
# Dispatch to modules
for module in self.modules.values(): for module in self.modules.values():
module["initialized_class"].dispatch(event, *args, **kwargs) module["initialized_class"].dispatch(event, *args, **kwargs)
@event
async def on_error(self, event_method, *args, **kwargs):
# This event is special because it is call directly
for module in self.modules.values():
await module["initialized_class"].on_error(event_method, *args, **kwargs)
class ClientById: class ClientById:
client: LBI client: LBI
@ -453,7 +461,7 @@ print(os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__))) + ".sock
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
#loop.add_signal_handler(signal.SIGINT, loop.stop) #loop.add_signal_handler(signal.SIGINT, loop.stop)
loop.set_exception_handler(execption_handler) #loop.set_exception_handler(execption_handler)
t = loop.create_unix_server(Communication, t = loop.create_unix_server(Communication,
path=os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__)) + ".sock")) path=os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__)) + ".sock"))
loop.run_until_complete(t) loop.run_until_complete(t)

View File

@ -35,8 +35,9 @@ class BaseClass:
self.client = client self.client = client
self.storage = FSStorage(path.join(self.client.base_path, self.name)) self.storage = FSStorage(path.join(self.client.base_path, self.name))
self.objects = FSObjects(self.storage) self.objects = FSObjects(self.storage)
if not self.storage.isdir(path.join("storage", self.name)): # Non necessaire car géré par fsstorage
self.storage.makedirs(path.join("storage", self.name), exist_ok=True) #if not self.storage.isdir(path.join("storage", self.name)):
# self.storage.makedirs(path.join("storage", self.name), exist_ok=True)
async def send_help(self, channel): async def send_help(self, channel):
embed = discord.Embed( embed = discord.Embed(
@ -50,7 +51,9 @@ class BaseClass:
inline=False) inline=False)
await channel.send(embed=embed) await channel.send(embed=embed)
async def auth(self, user, role_list): async def auth(self, user, role_list=None):
if role_list is None:
role_list = self.authorized_roles
if type(role_list) == list: if type(role_list) == list:
if user.id in self.client.owners: if user.id in self.client.owners:
return True return True
@ -88,10 +91,13 @@ class BaseClass:
self.client.config["prefix"] + (self.command_text if self.command_text else "")) self.client.config["prefix"] + (self.command_text if self.command_text else ""))
sub_command, args, kwargs = self._parse_command_content(content) sub_command, args, kwargs = self._parse_command_content(content)
sub_command = "com_" + sub_command sub_command = "com_" + sub_command
if await self.auth(message.author):
if sub_command in dir(self): if sub_command in dir(self):
await self.__getattribute__(sub_command)(message, args, kwargs) await self.__getattribute__(sub_command)(message, args, kwargs)
else: else:
await self.command(message, [sub_command[4:]] + args, kwargs) await self.command(message, [sub_command[4:]] + args, kwargs)
else:
await self.unauthorized(message, [sub_command[4:]] + args, kwargs)
@staticmethod @staticmethod
def _parse_command_content(content): def _parse_command_content(content):
@ -160,7 +166,8 @@ class BaseClass:
Function which is executed for all command_text doesn't match with a `com_{subcommand}` function""" Function which is executed for all command_text doesn't match with a `com_{subcommand}` function"""
pass pass
async def unauthorized(self, message):
await message.channel.send("Vous n'êtes pas autorisé à effectuer cette commande")
def dispatch(self, event, *args, **kwargs): def dispatch(self, event, *args, **kwargs):
# Method to call # Method to call
@ -172,22 +179,7 @@ class BaseClass:
pass pass
else: else:
# Run event # Run event
asyncio.ensure_future(self._run_event(coro, method, *args, **kwargs), loop=self.client.loop) asyncio.ensure_future(self.client._run_event(coro, method, *args, **kwargs), loop=self.client.loop)
async def _run_event(self, coro, event_name, *args, **kwargs):
# Run event
try:
await coro(*args, **kwargs)
except asyncio.CancelledError:
# If function is cancelled pass silently
pass
except Exception:
try:
# Call error function
await self.on_error(event_name, *args, **kwargs)
except asyncio.CancelledError:
# If error event is canceled pass silently
pass
async def on_error(self, event_method, *args, **kwargs): async def on_error(self, event_method, *args, **kwargs):
# Basic error handler # Basic error handler

View File

@ -41,35 +41,48 @@ class BaseClassLua(BaseClass):
print(os.path.abspath(path)) print(os.path.abspath(path))
self.luaMethods = self.lua.require(path) self.luaMethods = self.lua.require(path)
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)
def dispatch(self, event, *args, **kwargs): def dispatch(self, event, *args, **kwargs):
method = "on_"+event method = "on_"+event
if self.luaMethods[method] is not None: if self.luaMethods[method] is not None:
self.luaMethods[method](asyncio.ensure_future, self, *args, **kwargs) async def coro(*args, **kwargs):
else: # If lua methods not found, dispatch to python methods 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)
else: # If lua method not found, pass
super().dispatch(event, *args, **kwargs) super().dispatch(event, *args, **kwargs)
async def _run_event(self, coro, event_name, *args, **kwargs): async def parse_command(self, message):
# Overide here to execute lua on_error if it exists """Parse a command_text from received message and execute function
# Run event %git update
try: com_update(m..)
await coro(*args, **kwargs) Parse message like `{prefix}{command_text} subcommand` and call class method `com_{subcommand}`.
except asyncio.CancelledError:
# If function is cancelled pass silently :param message: message to parse
pass :type message: discord.Message"""
except Exception: if message.content.startswith(self.client.config["prefix"] + (self.command_text if self.command_text else "")):
try:
# Call error function content = message.content.lstrip(
if self.luaMethods["on_error"] is not None: self.client.config["prefix"] + (self.command_text if self.command_text else ""))
self.luaMethods["on_error"](self, asyncio.ensure_future, discord, *args, **kwargs) sub_command, args, kwargs = self._parse_command_content(content)
sub_command = "com_" + sub_command
if await self.auth(message.user):
if self.luaMethods[sub_command] is not None:
self.luaMethods[sub_command](self, asyncio.ensure_future, discord, message, args, kwargs)
else: else:
await self.on_error(event_name, *args, **kwargs) if self.luaMethods["command"] is not None:
except asyncio.CancelledError: self.luaMethods["command"](self, asyncio.ensure_future, discord, message, [sub_command[4:]] + args, kwargs)
# If error event is canceled pass silently else:
pass await self.unautorized(message, [sub_command[4:]] + args, kwargs)
async def on_error(self, event_method, *args, **kwargs):
# Base on_error event, executed if lua not provide it
# Basic error handler
print('Ignoring exception in {}'.format(event_method), file=sys.stderr)
traceback.print_exc()

View File

@ -44,10 +44,10 @@ class MainClass(BaseClassPython):
await delete_message.delete() await delete_message.delete()
except: except:
raise raise
self.objects.save_object(self.errorsDeque, 'errorsDeque') self.objects.save_object('errorsDeque', self.errorsDeque)
async def command(self, message, args, kwargs): async def command(self, message, args, kwargs):
raise Exception("Si cette erreur apparait, alors tout est normal") raise Exception("KERNEL PANIC!!!")
async def on_error(self, event, *args, **kwargs): async def on_error(self, event, *args, **kwargs):
embed = discord.Embed(title="Aïe :/", description="```PYTHON\n{0}```".format(traceback.format_exc()), embed = discord.Embed(title="Aïe :/", description="```PYTHON\n{0}```".format(traceback.format_exc()),
@ -72,7 +72,7 @@ class MainClass(BaseClassPython):
embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.icon)) embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.icon))
except: except:
pass pass
self.objects.save_object(self.errorsDeque, 'errorsDeque') self.objects.save_object('errorsDeque', self.errorsDeque)
await asyncio.sleep(60) await asyncio.sleep(60)
try: try:
channel = self.client.get_channel(message_list[0]) channel = self.client.get_channel(message_list[0])
@ -85,4 +85,4 @@ class MainClass(BaseClassPython):
self.errorsDeque.remove(message_list) self.errorsDeque.remove(message_list)
except ValueError: except ValueError:
pass pass
self.objects.save_object(self.errorsDeque, 'errorsDeque') self.objects.save_object('errorsDeque', self.errorsDeque)

View File

@ -19,6 +19,9 @@ class MainClass(BaseClassPython):
"`{prefix}{command} disable <module>`": "Disable module `<module>`.", "`{prefix}{command} disable <module>`": "Disable module `<module>`.",
"`{prefix}{command} reload <module>`": "Reload module `<module>`", "`{prefix}{command} reload <module>`": "Reload module `<module>`",
"`{prefix}{command} web_list`": "List all available modules from repository", "`{prefix}{command} web_list`": "List all available modules from repository",
# "`{prefix}{command} web_source`": "List all source repositories",
# "`{prefix}{command} web_source remove <url>`": "Remove url from repository list",
# "`{prefix}{command} web_source add <url>`": "Add url to repository list",
} }
} }
@ -27,7 +30,8 @@ class MainClass(BaseClassPython):
self.storage.mkdir("modules", exist_ok=True) self.storage.mkdir("modules", exist_ok=True)
self.api = Api() self.api = Api()
def get_all_modules(self): @staticmethod
def get_all_modules():
all_items = os.listdir("modules") all_items = os.listdir("modules")
modules = [] modules = []
for item in all_items: for item in all_items:

View File

@ -2,7 +2,7 @@ main = {}
function main.on_message(self, await, discord, message) function main.on_message(self, await, discord, message)
if message.author.bot == false then if message.author.bot == false then
-- message.channel.send("Test")
end end
end end

View File

@ -15,6 +15,7 @@ class FSStorage(Storage):
def _topath(self, path): def _topath(self, path):
"""Transform a path to a full path""" """Transform a path to a full path"""
return os.path.join(self.base_path, path) # TODO: Modifier ca
if path.startswith("/"): if path.startswith("/"):
return os.path.join(self.base_path, # Always add baspath to avoid going outside protected zone return os.path.join(self.base_path, # Always add baspath to avoid going outside protected zone
os.path.abspath(os.path.join(self.base_path, os.path.abspath(os.path.join(self.base_path,
@ -65,10 +66,10 @@ class FSStorage(Storage):
os.sync() os.sync()
def open(self, path, mode): def open(self, path, mode):
return open(path, mode) return open(self._topath(path), mode)
def exists(self, path): def exists(self, path):
return os.path.exists(path) return os.path.exists(self._topath(path))
def isdir(self, path): def isdir(self, path):
return os.path.isdir(self._topath(path)) return os.path.isdir(self._topath(path))

View File

@ -122,8 +122,11 @@ class Storage:
class Objects: class Objects:
def __init__(self, storage): storage: Storage
def __init__(self, storage: Storage):
self.storage = storage self.storage = storage
self.storage.makedirs("objects", exist_ok=True)
def save_object(self, object_name, object_instance): def save_object(self, object_name, object_instance):
"""Save object into pickle file""" """Save object into pickle file"""