diff --git a/demo/__init__.py b/demo/__init__.py index 06e71871..f2c1ef23 100644 --- a/demo/__init__.py +++ b/demo/__init__.py @@ -4,7 +4,18 @@ import none from demo import config none.init(config) -app = none.get_bot().asgi + + +@none.scheduler.scheduled_job('interval', seconds=3) +async def cb(): + bot_ = none.get_bot() + try: + await bot_.send_private_msg(self_id=3281334718, + user_id=1002647525, + message='哇') + except Exception as e: + none.logger.exception(e) + if __name__ == '__main__': none.load_builtin_plugins() diff --git a/none/__init__.py b/none/__init__.py index a302d18d..5690f9b0 100644 --- a/none/__init__.py +++ b/none/__init__.py @@ -9,6 +9,12 @@ from aiocqhttp import CQHttp from aiocqhttp.message import Message from .log import logger +from .scheduler import Scheduler + +if Scheduler: + scheduler = Scheduler() +else: + scheduler = None class NoneBot(CQHttp): @@ -40,6 +46,9 @@ class NoneBot(CQHttp): async def _(ctx): asyncio.ensure_future(handle_notice_or_request(self, ctx)) + if scheduler: + scheduler.configure(self.config.APSCHEDULER_CONFIG) + def run(self, host: str = None, port: int = None, *args, **kwargs): host = host or self.config.HOST port = port or self.config.PORT @@ -88,6 +97,8 @@ def get_bot() -> NoneBot: def run(host: str = None, port: int = None, *args, **kwargs) -> None: """Run the NoneBot instance.""" + if scheduler and not scheduler.running: + scheduler.start() get_bot().run(host=host, port=port, *args, **kwargs) diff --git a/none/default_config.py b/none/default_config.py index 87b9a93c..575667aa 100644 --- a/none/default_config.py +++ b/none/default_config.py @@ -30,3 +30,7 @@ COMMAND_SEP = {'/', '.'} SESSION_EXPIRE_TIMEOUT = timedelta(minutes=5) SESSION_RUNNING_EXPRESSION = '您有命令正在执行,请稍后再试' SHORT_MESSAGE_MAX_LENGTH = 50 + +APSCHEDULER_CONFIG = { + 'apscheduler.timezone': 'Asia/Shanghai' +} diff --git a/none/scheduler.py b/none/scheduler.py new file mode 100644 index 00000000..377d4251 --- /dev/null +++ b/none/scheduler.py @@ -0,0 +1,11 @@ +try: + from apscheduler.schedulers.asyncio import AsyncIOScheduler +except ImportError: + # APScheduler is not installed + AsyncIOScheduler = None + +if AsyncIOScheduler: + class Scheduler(AsyncIOScheduler): + pass +else: + Scheduler = None diff --git a/setup.py b/setup.py index 311406b9..849e75da 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,9 @@ setup( long_description=long_description, long_description_content_type='text/markdown', install_requires=['aiocqhttp>=0.6', 'aiocache>=0.10'], + extras_require={ + 'Scheduler': ['apscheduler>=1.2'], + }, python_requires='>=3.6', platforms='any', classifiers=(