Commit 61c191f6 authored by Allen Lantz's avatar Allen Lantz 🌟

Made it work

parent 231cb7cf
......@@ -107,21 +107,21 @@ If for some reason your module needs to access your or another feautres meta you
### Events
Events are listened using `module#on_event(event_name, listener_func, [priority=0, [ignore_canceled=False]])`, event names can be found on the [discord.py](https://discordpy.readthedocs.io/en/latest/api.html#event-reference) documentation, in addition here are the built-in events:
- `fm:on_ready` - Called when the module manager is setup
- `fm:on_done` - Called after enable process is finished (after `fn:on_all_enabled`)
- `fm:on_all_load` - Called after all modules are loaded, useful to add or modify functions in each module
- `fm:on_load` - Called once a module has been loaded, useful to add or modify a dependant module
- `fm:on_all_enabled` - Called when all modules have been enabled
- `fm:on_enabled` - Called once a module has been enabled
- `fm:on_all_disabled` - Called when all modules have been disabled
- `fm:on_disable` - Called once a module has been disabled
Events are listened using `module#on_event(event_name, listener_func, [namespace="discord"], [priority=0], [ignore_canceled=False])`, event names can be found on the [discord.py](https://discordpy.readthedocs.io/en/latest/api.html#event-reference) documentation, in addition here are the built-in events, the namespace is `buildabot`:
- `on_ready` - Called when the module manager is setup
- `on_done` - Called after enable process is finished (after `on_all_enabled`)
- `on_all_load` - Called after all modules are loaded, useful to add or modify functions in each module
- `on_load` - Called once a module has been loaded, useful to add or modify a dependant module
- `on_all_enabled` - Called when all modules have been enabled
- `on_enabled` - Called once a module has been enabled
- `on_all_disabled` - Called when all modules have been disabled
- `on_disable` - Called once a module has been disabled
_**Important:** All listener functions must be a coroutine. In order to turn a function into a coroutine they must be `async def` functions_
Each module may have their own events, you can read about them in the modules' README
To create an event, it's pretty simple, in your `on_load` define the module with `self.module_manager.define_event(event_name)` then to call the event run either `await self.module_manager._call_event(event_name, *args, **kwargs)` or `self.module_manager._call_event_sync(event_name, *args, **kwargs)`
To create an event, it's pretty simple, in your `on_load` define the module with `self.module_manager.define_event(event_name)` then to call the event run either `await self.module_manager.emit_event(namespace, event_name, *args, **kwargs)` or `self.module_manager.emit_event_sync(namespace, event_name, *args, **kwargs)`
### Using other modules
......
......@@ -2,7 +2,7 @@ name = 'buildabot'
__title__ = 'buildabot'
__author__ = 'Allen Lantz'
__copyright__ = 'Copyright 2020 Allen Lantz'
__version__ = '2.0.0'
__version__ = '2.0.3'
from .bot import Bot
from .typer import Typer
......@@ -15,4 +15,4 @@ from collections import namedtuple
VersionInfo = namedtuple('VersionInfo', 'major minor micro releaselevel serial')
version_info = VersionInfo(major=2, minor=0, micro=0, releaselevel='beta', serial=0)
version_info = VersionInfo(major=2, minor=0, micro=3, releaselevel='beta', serial=0)
class Feature(object):
class Module(object):
"""
A feature is, well, a feature. Features loaded when the program is loaded and enabled when the bot is ready
A module is, well, a module. Modules loaded when the program is loaded and enabled when the bot is ready
"""
def __init__(self, feature_manager, meta: dict):
from buildabot import Bot, FeatureManager, Logger
self.feature_manager: FeatureManager = feature_manager
self.bot: Bot = feature_manager.bot
def __init__(self, module_manager, meta: dict):
from buildabot import Bot, ModuleManager, Logger
self.module_manager: ModuleManager = module_manager
self.bot: Bot = module_manager.bot
self.meta: dict = meta
self._enabled = False
self.logger = Logger(feature=self)
self.logger = Logger(module=self)
self.events = []
self.config = {}
async def enable(self):
"""
Attempt to enable the feature
Attempt to enable the module
"""
if 'disable' in self.meta:
if self.meta['disable']:
......@@ -25,11 +25,11 @@ class Feature(object):
await self.on_enable()
self.logger.info('Enabled')
self._enabled = True
await self.feature_manager._call_event('fn:on_enabled', self)
await self.module_manager.emit_event('buildabot', 'on_enabled', self)
async def disable(self):
"""
Attempt to disable the feature
Attempt to disable the module
:return:
"""
if not self.is_enabled():
......@@ -40,16 +40,16 @@ class Feature(object):
await self.on_disable()
self.logger.info('Disabled')
self._enabled = False
await self.feature_manager._call_event('fn:on_disabled', self)
await self.module_manager.emit_event('buildabot', 'on_disabled', self)
def is_enabled(self):
"""
Get weather the feature is enabled
Get weather the module is enabled
:return: boolean
"""
return self._enabled
def on_event(self, event, func, priority=0, ignore_canceled=False):
def on_event(self, event, func, priority=0, namespace='discord', ignore_canceled=False):
"""
Add an event handler
:param event: Name of the event
......@@ -58,36 +58,36 @@ class Feature(object):
:param ignore_canceled: Ignore the event if it is canceled before the listener can be called
:return: The event handler
"""
event = self.feature_manager.on_event(self, event, func, priority=priority, ignore_canceled=ignore_canceled)
event = self.module_manager.on_event(self, event, func, namespace=namespace, priority=priority, ignore_canceled=ignore_canceled)
self.events.append(event)
return event
def unregister_all_events(self):
"""
Unregister all events associated with this feature. This is called when the feature is disabled.
Unregister all events associated with this module. This is called when the module is disabled.
:return:
"""
for event in self.events:
event.unregister(self.feature_manager.events)
event.unregister(self.module_manager.events)
self.events = []
# defaults
def on_load(self):
"""
Called when the feature is loaded
Called when the module is loaded
Note: Do not attempt to get other features in this method as they might not be loaded
Note: Do not attempt to get other modules in this method as they might not be loaded
"""
pass
async def on_enable(self):
"""
Called when the feature is enabled
Called when the module is enabled
"""
pass
async def on_disable(self):
"""
Called when the feature is disabled
Called when the module is disabled
"""
pass
......@@ -8,7 +8,6 @@ from pathlib import Path
from .module import Module
from .event_handler import EventHandler
from .bot import Bot
from .logger import Logger
from .typer import Typer
......@@ -34,10 +33,11 @@ class ModuleManager(object):
"""
def __init__(self, bot, modules_dir=None):
from .bot import Bot
self.bot: Bot = bot
self.logger = Logger()
self.modules = {}
self.loop: asyncio.BaseEventLoop = None
self.loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
self.modules_dir = modules_dir
if self.modules_dir is None:
self.modules_dir = 'modules/'
......@@ -137,7 +137,7 @@ class ModuleManager(object):
prioritys.reverse()
for priority in prioritys:
for handler in self.events[event_name][priority]:
for handler in self.events[namespace][event_name][priority]:
if canceled and not handler.ignore_canceled:
continue
......@@ -392,7 +392,7 @@ class ModuleManager(object):
if priority not in self.events[namespace][event_name]:
self.events[namespace][event_name][priority] = []
handler = EventHandler(module, event_name, func, priority, ignore_canceled)
handler = EventHandler(module, namespace, event_name, func, priority, ignore_canceled)
self.events[namespace][event_name][priority].append(handler)
return handler
......
......@@ -32,7 +32,7 @@ class Typer:
:param check: Check to convert to this type
"""
Typer.types.setdefault(name.lower(), check)
Types.serializable.setdefault(name.lower(), serializable)
Typer.serializable.setdefault(name.lower(), serializable)
@staticmethod
def is_valid_type(expected_type):
......
......@@ -20,7 +20,7 @@ with open('README.md') as f:
setup(name='BuildABot',
author='AL_1',
url='https://gitlab.com/AL1L/bot_framework',
url='https://gitlab.com/artex-development/bot_framework',
version=version,
packages=['buildabot'],
description='Build a Dicord bot with ease and flexibility',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment