2018-07-02 16:54:29 +08:00
|
|
|
import asyncio
|
2018-06-15 06:58:24 +08:00
|
|
|
import logging
|
2018-07-04 16:21:01 +08:00
|
|
|
from typing import Any, Optional
|
2018-06-15 06:58:24 +08:00
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
import aiocqhttp.message
|
2018-06-15 06:58:24 +08:00
|
|
|
from aiocqhttp import CQHttp
|
|
|
|
|
2018-07-02 16:54:29 +08:00
|
|
|
from .log import logger
|
2018-10-16 01:03:50 +08:00
|
|
|
from .sched import Scheduler
|
2018-08-26 10:05:42 +08:00
|
|
|
|
|
|
|
if Scheduler:
|
|
|
|
scheduler = Scheduler()
|
|
|
|
else:
|
|
|
|
scheduler = None
|
2018-06-15 06:58:24 +08:00
|
|
|
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
class NoneBot(CQHttp):
|
2018-10-16 01:03:50 +08:00
|
|
|
def __init__(self, config_object: Optional[Any] = None):
|
2018-07-04 16:21:01 +08:00
|
|
|
if config_object is None:
|
2018-07-06 14:24:18 +08:00
|
|
|
from . import default_config as config_object
|
2018-07-04 09:28:31 +08:00
|
|
|
|
2018-07-21 00:46:34 +08:00
|
|
|
config_dict = {k: v for k, v in config_object.__dict__.items()
|
|
|
|
if k.isupper() and not k.startswith('_')}
|
|
|
|
logger.debug(f'Loaded configurations: {config_dict}')
|
2018-10-16 01:03:50 +08:00
|
|
|
super().__init__(message_class=aiocqhttp.message.Message,
|
2018-07-21 00:46:34 +08:00
|
|
|
**{k.lower(): v for k, v in config_dict.items()})
|
2018-07-04 09:28:31 +08:00
|
|
|
|
2018-07-04 16:21:01 +08:00
|
|
|
self.config = config_object
|
|
|
|
self.asgi.debug = self.config.DEBUG
|
2018-06-15 06:58:24 +08:00
|
|
|
|
2018-07-04 16:21:01 +08:00
|
|
|
from .message import handle_message
|
|
|
|
from .notice_request import handle_notice_or_request
|
2018-06-15 06:58:24 +08:00
|
|
|
|
2018-07-04 16:21:01 +08:00
|
|
|
@self.on_message
|
|
|
|
async def _(ctx):
|
|
|
|
asyncio.ensure_future(handle_message(self, ctx))
|
|
|
|
|
|
|
|
@self.on_notice
|
|
|
|
async def _(ctx):
|
|
|
|
asyncio.ensure_future(handle_notice_or_request(self, ctx))
|
|
|
|
|
|
|
|
@self.on_request
|
|
|
|
async def _(ctx):
|
|
|
|
asyncio.ensure_future(handle_notice_or_request(self, ctx))
|
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
def run(self, host: Optional[str] = None, port: Optional[int] = None,
|
|
|
|
*args, **kwargs) -> None:
|
2018-08-17 23:45:43 +08:00
|
|
|
host = host or self.config.HOST
|
|
|
|
port = port or self.config.PORT
|
|
|
|
if 'debug' not in kwargs:
|
|
|
|
kwargs['debug'] = self.config.DEBUG
|
|
|
|
|
2018-07-21 00:46:34 +08:00
|
|
|
logger.info(f'Running on {host}:{port}')
|
2019-02-02 21:22:14 +08:00
|
|
|
super().run(host=host, port=port, *args, **kwargs)
|
2018-07-04 16:21:01 +08:00
|
|
|
|
|
|
|
|
2018-07-06 14:24:18 +08:00
|
|
|
_bot: Optional[NoneBot] = None
|
2018-07-04 16:21:01 +08:00
|
|
|
|
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
def init(config_object: Optional[Any] = None) -> None:
|
2018-07-04 19:50:42 +08:00
|
|
|
"""
|
|
|
|
Initialize NoneBot instance.
|
|
|
|
|
|
|
|
This function must be called at the very beginning of code,
|
|
|
|
otherwise the get_bot() function will return None and nothing
|
|
|
|
is gonna work properly.
|
|
|
|
|
|
|
|
:param config_object: configuration object
|
|
|
|
"""
|
2018-07-04 16:21:01 +08:00
|
|
|
global _bot
|
|
|
|
_bot = NoneBot(config_object)
|
2018-08-26 13:44:54 +08:00
|
|
|
|
2018-07-04 16:21:01 +08:00
|
|
|
if _bot.config.DEBUG:
|
2018-06-15 06:58:24 +08:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
2019-02-03 11:49:59 +08:00
|
|
|
_bot.server_app.before_serving(_start_scheduler)
|
|
|
|
|
|
|
|
|
2020-02-10 12:17:00 +08:00
|
|
|
async def _start_scheduler():
|
2018-08-26 13:44:54 +08:00
|
|
|
if scheduler and not scheduler.running:
|
|
|
|
scheduler.configure(_bot.config.APSCHEDULER_CONFIG)
|
|
|
|
scheduler.start()
|
2019-02-03 11:49:59 +08:00
|
|
|
logger.info('Scheduler started')
|
2018-08-26 13:44:54 +08:00
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
|
2018-07-04 16:21:01 +08:00
|
|
|
def get_bot() -> NoneBot:
|
2018-07-04 19:50:42 +08:00
|
|
|
"""
|
|
|
|
Get the NoneBot instance.
|
|
|
|
|
2018-07-21 21:59:43 +08:00
|
|
|
The result is ensured to be not None, otherwise an exception will
|
|
|
|
be raised.
|
|
|
|
|
2018-07-04 19:50:42 +08:00
|
|
|
:raise ValueError: instance not initialized
|
|
|
|
"""
|
2018-07-04 16:21:01 +08:00
|
|
|
if _bot is None:
|
|
|
|
raise ValueError('NoneBot instance has not been initialized')
|
|
|
|
return _bot
|
2018-06-15 06:58:24 +08:00
|
|
|
|
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
def run(host: Optional[str] = None, port: Optional[int] = None,
|
|
|
|
*args, **kwargs) -> None:
|
2018-07-04 19:50:42 +08:00
|
|
|
"""Run the NoneBot instance."""
|
2018-07-04 16:21:01 +08:00
|
|
|
get_bot().run(host=host, port=port, *args, **kwargs)
|
2018-06-15 06:58:24 +08:00
|
|
|
|
|
|
|
|
2018-10-16 01:03:50 +08:00
|
|
|
from .exceptions import *
|
2019-01-03 19:58:56 +08:00
|
|
|
from .plugin import (load_plugin, load_plugins, load_builtin_plugins,
|
|
|
|
get_loaded_plugins)
|
2018-10-14 22:52:37 +08:00
|
|
|
from .message import message_preprocessor, Message, MessageSegment
|
2018-07-01 11:01:24 +08:00
|
|
|
from .command import on_command, CommandSession, CommandGroup
|
2019-01-21 21:31:26 +08:00
|
|
|
from .natural_language import (on_natural_language, NLPSession, NLPResult,
|
|
|
|
IntentCommand)
|
2018-10-14 22:52:37 +08:00
|
|
|
from .notice_request import (on_notice, NoticeSession,
|
|
|
|
on_request, RequestSession)
|
2019-01-24 22:33:41 +08:00
|
|
|
from .helpers import context_id
|
2019-01-03 19:58:56 +08:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'NoneBot', 'scheduler', 'init', 'get_bot', 'run',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-03 19:58:56 +08:00
|
|
|
'CQHttpError',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-03 19:58:56 +08:00
|
|
|
'load_plugin', 'load_plugins', 'load_builtin_plugins',
|
|
|
|
'get_loaded_plugins',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-03 19:58:56 +08:00
|
|
|
'message_preprocessor', 'Message', 'MessageSegment',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-03 19:58:56 +08:00
|
|
|
'on_command', 'CommandSession', 'CommandGroup',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-21 21:31:26 +08:00
|
|
|
'on_natural_language', 'NLPSession', 'NLPResult', 'IntentCommand',
|
2019-01-03 19:58:56 +08:00
|
|
|
'on_notice', 'NoticeSession', 'on_request', 'RequestSession',
|
2019-01-25 15:29:30 +08:00
|
|
|
|
2019-01-24 22:33:41 +08:00
|
|
|
'context_id',
|
2019-01-03 19:58:56 +08:00
|
|
|
]
|