bot-base/main.py

453 lines
14 KiB
Python
Raw Normal View History

2019-04-28 18:59:08 +02:00
#!/usr/bin/python3
2019-05-13 04:06:17 +02:00
import asyncio
import concurrent
2019-04-28 18:59:08 +02:00
import importlib
import json
import logging
import logging.config
import os
2019-06-01 19:24:45 +02:00
import signal
import socket
2019-04-28 18:59:08 +02:00
import traceback
2019-05-13 04:06:17 +02:00
from concurrent.futures.process import ProcessPoolExecutor
from concurrent.futures.thread import ThreadPoolExecutor
2019-04-28 18:59:08 +02:00
from typing import Dict
import discord
from packaging.version import Version
2019-08-10 17:58:54 +02:00
from config.FileSystem import FSConfig
2019-04-28 18:59:08 +02:00
from errors import IncompatibleModule
__version__ = "0.1.0"
class Module:
name: str
def __init__(self, name: str):
"""
Init module
:param name: Module name
:type name: str
"""
self.name = name
MODULES.update({self.name: self})
2019-06-10 17:46:33 +02:00
@property
def type(self) -> str:
"""
Return module type. It can be python or lua
:return: Module type
:rtype: str
"""
if not os.path.exists(os.path.join("modules", self.name, "version.json")):
return ""
with open(os.path.join("modules", self.name, "version.json")) as file:
versions = json.load(file)
return versions["type"]
2019-04-28 18:59:08 +02:00
@property
def exists(self) -> bool:
"""
Check if module exists
:return: True if module is present in modules folders
:rtype: bool
"""
if not os.path.isdir(os.path.join("modules", self.name)):
return False
return True
@property
def complete(self) -> bool:
"""
Check if module is complete
:return: True if module is compatible
:rtype: Boolean
"""
# Check if version.json exists
if not os.path.exists(os.path.join("modules", self.name, "version.json")):
return False
with open(os.path.join("modules", self.name, "version.json")) as file:
versions = json.load(file)
2019-08-11 18:07:58 +02:00
print(versions)
2019-04-28 18:59:08 +02:00
if "version" not in versions.keys():
return False
if "dependencies" not in versions.keys():
return False
if "bot_version" not in versions.keys():
return False
2019-06-10 17:46:33 +02:00
if "type" not in versions.keys():
return False
2019-04-28 18:59:08 +02:00
return True
@property
def version(self) -> Version:
"""
Returns module version
:return: current module version
:rtype: Version
"""
with open(os.path.join("modules", self.name, "version.json")) as file:
versions = json.load(file)
return Version(versions["version"])
@property
def bot_version(self) -> dict:
"""
returns the min and max version of the bot that is compatible with the module
:return: Min and max version for bot
:rtype: dict
:raises IncompatibleModule: If bot_version is not properly formated (there must be min and max keys)
"""
with open(os.path.join("modules", self.name, "version.json")) as file:
versions = json.load(file)
try:
return {"min": Version(versions["bot_version"]["min"]),
"max": Version(versions["bot_version"]["max"])}
except KeyError:
raise IncompatibleModule(f"Module {self.name} is not compatible with bot (version.json does not "
f"contain bot_version.max or bot_version.min item)")
@property
def dependencies(self) -> dict:
"""
return list of dependencies version
:return: list of dependencies version
:rtype: dict
:raises IncompatibleModule: If bot_version is not properly formated (there must be min and max keys for each
dependencies)
"""
with open(os.path.join("modules", self.name, "version.json")) as file:
versions = json.load(file)
try:
deps = {}
for name, dep in versions["dependencies"].items():
dep_ver = {"min": Version(dep["min"]),
"max": Version(dep["max"])}
deps.update({name: dep_ver})
return deps
except KeyError:
raise IncompatibleModule(f"Module {self.name} is not compatible with bot (version.json does not "
f"contain dependencies.modulename.max or dependencies.modulename.min item)")
@property
def compatible(self) -> bool:
"""
Check if module is compatible with current installation
:return: True if all dependencies are okays
:rtype: bool
"""
# Check bot version
bot_ver = Version(__version__)
if bot_ver < self.bot_version["min"]:
return False
if bot_ver > self.bot_version["max"]:
return False
for name, dep in self.dependencies.items():
if name not in MODULES.keys():
Module(name)
if MODULES[name].version < dep["min"]:
return False
if MODULES[name].version > dep["max"]:
return False
return True
MODULES: Dict[str, Module] = {}
def setup_logging(default_path='config/log_config.json', default_level=logging.INFO, env_key='LBI_LOG_CONFIG'):
"""Setup logging configuration
"""
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
def modules_edit(func):
def wrapper(self, *args, **kwargs):
if self.reloading:
return func(self, *args, **kwargs)
else:
self.reloading = True
a = func(self, *args, **kwargs)
self.reloading = False
return a
return wrapper
def event(func):
def wrapper(self, *args, **kwargs):
if self.reloading:
return lambda: None
else:
return func(self, *args, **kwargs)
return wrapper
setup_logging()
log_discord = logging.getLogger('discord')
log_LBI = logging.getLogger('LBI')
2019-05-22 21:30:06 +02:00
log_communication = logging.getLogger('communication')
2019-04-28 18:59:08 +02:00
2019-06-01 19:24:45 +02:00
2019-04-28 18:59:08 +02:00
def load_modules_info():
for mod in os.listdir("modules"):
Module(mod)
class LBI(discord.Client):
2019-08-08 22:43:46 +02:00
base_path = "data"
2019-04-28 18:59:08 +02:00
debug = log_LBI.debug
info = log_LBI.info
warning = log_LBI.warning
error = log_LBI.error
critical = log_LBI.critical
2019-08-10 20:29:04 +02:00
def __init__(self, config=None, *args, **kwargs):
2019-04-28 18:59:08 +02:00
super().__init__(*args, **kwargs)
2019-08-10 20:29:04 +02:00
if config is None:
config = FSConfig(path="data/config.yml")
2019-04-28 18:59:08 +02:00
self.reloading = False
self.id = ClientById(self)
self.ready = False
# Content: {"module_name": {"module": imported module, "class": initialized class}}
self.modules = {}
2019-08-10 20:29:04 +02:00
self.config = config
2019-08-10 17:58:54 +02:00
self.config["modules"] = self.config["modules"] if self.config["modules"] is not None else ["modules","errors"]
self.config["prefix"] = self.config["prefix"] or "%"
self.config["owners"] = self.config["owners"] or []
2019-04-28 18:59:08 +02:00
self.load_modules()
@modules_edit
def load_modules(self):
2019-06-01 19:24:45 +02:00
self.info("Starts to load modules...")
2019-04-28 18:59:08 +02:00
e = {}
for module in self.config["modules"]:
e.update({module: self.load_module(module)})
2019-06-01 19:24:45 +02:00
self.info("Finished to load all modules.")
2019-04-28 18:59:08 +02:00
return e
@modules_edit
def load_module(self, module):
"""
Status codes:
- 0: Module loaded
- 1: Module not in modules folder
- 2: Module incomplete
- 3: Module incompatible
:param module: Module name
:return: Status code
"""
# Check module compatibility
load_modules_info()
if not MODULES.get(module):
return 1
if not MODULES[module].exists:
return 1
if not MODULES[module].complete:
return 2
if not MODULES[module].compatible:
return 3
deps = MODULES[module].dependencies
for dep in deps.keys():
if dep not in self.modules.keys():
2019-08-10 09:30:46 +02:00
if dep != "base":
self.load_module(dep)
2019-06-10 17:46:33 +02:00
if MODULES[module].type == "python":
try:
self.info("Start loading module {module}...".format(module=module))
imported = importlib.import_module('modules.' + module)
importlib.reload(imported)
initialized_class = imported.MainClass(self)
self.modules.update({module: {"imported": imported, "initialized_class": initialized_class}})
self.info("Module {module} successfully imported.".format(module=module))
initialized_class.dispatch("load")
2019-08-10 17:58:54 +02:00
2019-06-10 17:46:33 +02:00
if module not in self.config["modules"]:
self.config["modules"].append(module)
2019-08-10 17:58:54 +02:00
self.config.save()
2019-06-10 17:46:33 +02:00
except AttributeError as e:
self.error("Module {module} doesn't have MainClass.".format(module=module))
2019-08-10 17:58:54 +02:00
raise
2019-06-10 17:46:33 +02:00
return e
return 0
elif MODULES[module].type == "lua":
self.info(f"Start loading module {module}...")
imported = importlib.import_module('modules.base.BaseLua')
2019-04-28 18:59:08 +02:00
importlib.reload(imported)
2019-06-10 17:46:33 +02:00
initialized_class = imported.BaseClassLua(self, path=f"modules/{module}/main")
2019-04-28 18:59:08 +02:00
self.modules.update({module: {"imported": imported, "initialized_class": initialized_class}})
2019-06-10 17:46:33 +02:00
self.info(f"Module {module} successfully imported.")
2019-06-05 03:37:50 +02:00
initialized_class.dispatch("load")
2019-04-28 18:59:08 +02:00
if module not in self.config["modules"]:
self.config["modules"].append(module)
2019-06-10 17:46:33 +02:00
return 0
2019-04-28 18:59:08 +02:00
@modules_edit
def unload_module(self, module):
2019-06-01 19:24:45 +02:00
self.info("Start unload module {module}...".format(module=module))
2019-04-28 18:59:08 +02:00
try:
if module in self.config["modules"]:
self.config["modules"].remove(module)
2019-08-10 17:58:54 +02:00
self.config.save()
2019-04-28 18:59:08 +02:00
self.unload_all()
self.load_modules()
except KeyError as e:
2019-06-01 19:24:45 +02:00
self.error("Module {module} not loaded.").format(module=module)
2019-04-28 18:59:08 +02:00
return e
@modules_edit
def reload(self):
del self.modules
self.load_modules()
@modules_edit
def unload_all(self):
del self.modules
self.modules = {}
@event
2019-06-01 19:24:45 +02:00
def dispatch(self, event, *args, **kwargs):
2019-08-08 22:43:46 +02:00
# Dispatch to handle wait_* commands
2019-05-22 21:30:06 +02:00
super().dispatch(event, *args, **kwargs)
2019-08-08 22:43:46 +02:00
# Dispatch to modules
2019-04-28 18:59:08 +02:00
for module in self.modules.values():
2019-06-01 19:24:45 +02:00
module["initialized_class"].dispatch(event, *args, **kwargs)
2019-05-13 04:06:17 +02:00
2019-08-08 22:43:46 +02:00
@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)
2019-04-28 18:59:08 +02:00
class ClientById:
client: LBI
def __init__(self, client_):
self.client = client_
async def fetch_message(self, id_, *args, **kwargs):
"""Find a message by id
:param id_: Id of message to find
:type id_: int
:raises discord.NotFound: This exception is raised when a message is not found (or not accessible by bot)
:rtype: discord.Message
:return: discord.Message instance if message is found.
"""
msg = None
for channel in self.client.get_all_channels():
try:
return await channel.fetch_message(id_, *args, **kwargs)
except discord.NotFound:
continue
if msg is None:
raise discord.NotFound(None, "Message not found")
async def edit_message(self, id_, *args, **kwargs):
"""Edit message by id_
:param id_: Id of the message to edit
:type id_: int"""
message = await self.fetch_message(id_)
return await message.edit(**kwargs)
async def remove_reaction(self, id_message, *args, **kwargs):
"""Remove reaction from message by id
:param id_message: Id of message
:type id_message: int"""
message = await self.fetch_message(id_message)
return await message.remove_reaction(*args, **kwargs)
async def send_message(self, id_, *args, **kwargs):
"""Send message by channel id
:param id_: Id of channel where to send message
:type id_: int"""
channel = self.client.get_channel(id_)
return channel.send(*args, **kwargs)
2019-05-22 21:30:06 +02:00
async def get_role(self, id_):
for guild in self.client.guilds:
role = discord.utils.get(guild.roles, id=id_)
if role:
return role
return None
2019-06-01 19:24:45 +02:00
2019-08-10 17:58:54 +02:00
client1 = LBI(max_messages=500000)
2019-06-01 19:24:45 +02:00
class Communication(asyncio.Protocol):
2019-05-22 21:30:06 +02:00
debug = log_communication.debug
info = log_communication.info
warning = log_communication.warning
2019-06-01 19:24:45 +02:00
error = log_communication.error
2019-05-22 21:30:06 +02:00
critical = log_communication.critical
2019-06-01 19:24:45 +02:00
name = "Communication"
def __init__(self, client=client1):
2019-05-22 21:30:06 +02:00
self.client = client
2019-06-01 19:24:45 +02:00
self.transport = None
2019-05-22 21:30:06 +02:00
2019-06-01 19:24:45 +02:00
def connection_made(self, transport):
print('%s: connection made' % self.name)
self.transport = transport
def data_received(self, data):
print('%s: data received: %r' % (self.name, data))
def eof_received(self):
pass
def connection_lost(self, exc):
print('%s: connection lost: %s' % (self.name, exc))
2019-05-22 21:30:06 +02:00
2019-06-01 19:24:45 +02:00
communication = Communication(client1)
2019-04-28 18:59:08 +02:00
2019-05-13 04:06:17 +02:00
async def start_bot():
2019-08-10 17:58:54 +02:00
await client1.start(os.environ.get("DISCORD_TOKEN"))
2019-05-13 04:06:17 +02:00
print(os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__))) + ".sock")
2019-05-13 04:06:17 +02:00
2019-06-01 19:24:45 +02:00
loop = asyncio.get_event_loop()
2019-06-05 02:23:11 +02:00
#loop.add_signal_handler(signal.SIGINT, loop.stop)
2019-08-08 22:43:46 +02:00
#loop.set_exception_handler(execption_handler)
2019-06-01 19:24:45 +02:00
t = loop.create_unix_server(Communication,
path=os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__)) + ".sock"))
loop.run_until_complete(t)
loop.create_task(start_bot())
loop.run_forever()
2019-05-13 04:06:17 +02:00
2019-06-01 19:24:45 +02:00
# loop = asyncio.get_event_loop()
# loop.run_forever()