mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
Change some logic
This commit is contained in:
parent
89a11ab6e9
commit
1f4e5f3435
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,3 +6,5 @@ venv
|
|||||||
build
|
build
|
||||||
dist
|
dist
|
||||||
*.egg-info
|
*.egg-info
|
||||||
|
|
||||||
|
none_demo/data
|
@ -3,7 +3,7 @@ import importlib
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from typing import Any
|
from typing import Any, Optional
|
||||||
|
|
||||||
from aiocqhttp import CQHttp
|
from aiocqhttp import CQHttp
|
||||||
from aiocqhttp.message import Message
|
from aiocqhttp.message import Message
|
||||||
@ -13,47 +13,88 @@ from .log import logger
|
|||||||
|
|
||||||
|
|
||||||
class NoneBot(CQHttp):
|
class NoneBot(CQHttp):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, config_object: Any = None):
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.config = default_config
|
|
||||||
|
|
||||||
|
|
||||||
def create_bot(config_object: Any = None) -> NoneBot:
|
|
||||||
if config_object is None:
|
if config_object is None:
|
||||||
config_object = default_config
|
config_object = default_config
|
||||||
|
|
||||||
kwargs = {k.lower(): v for k, v in config_object.__dict__.items()
|
super_kwargs = {k.lower(): v for k, v in config_object.__dict__.items()
|
||||||
if k.isupper() and not k.startswith('_')}
|
if k.isupper() and not k.startswith('_')}
|
||||||
|
super().__init__(message_class=Message, **super_kwargs)
|
||||||
|
|
||||||
bot = NoneBot(message_class=Message, **kwargs)
|
self.config = config_object
|
||||||
bot.config = config_object
|
self.asgi.debug = self.config.DEBUG
|
||||||
if bot.config.DEBUG:
|
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
else:
|
|
||||||
logger.setLevel(logging.INFO)
|
|
||||||
bot.asgi.debug = bot.config.DEBUG
|
|
||||||
|
|
||||||
from .message import handle_message
|
from .message import handle_message
|
||||||
from .notice_request import handle_notice_or_request
|
from .notice_request import handle_notice_or_request
|
||||||
|
|
||||||
@bot.on_message
|
@self.on_message
|
||||||
async def _(ctx):
|
async def _(ctx):
|
||||||
asyncio.ensure_future(handle_message(bot, ctx))
|
asyncio.ensure_future(handle_message(self, ctx))
|
||||||
|
|
||||||
@bot.on_notice
|
@self.on_notice
|
||||||
async def _(ctx):
|
async def _(ctx):
|
||||||
asyncio.ensure_future(handle_notice_or_request(bot, ctx))
|
asyncio.ensure_future(handle_notice_or_request(self, ctx))
|
||||||
|
|
||||||
@bot.on_request
|
@self.on_request
|
||||||
async def _(ctx):
|
async def _(ctx):
|
||||||
asyncio.ensure_future(handle_notice_or_request(bot, ctx))
|
asyncio.ensure_future(handle_notice_or_request(self, ctx))
|
||||||
|
|
||||||
return bot
|
def run(self, host=None, port=None, *args, **kwargs):
|
||||||
|
super().run(host=host, port=port, loop=asyncio.get_event_loop(),
|
||||||
|
*args, **kwargs)
|
||||||
|
|
||||||
|
def get_data_folder(self,
|
||||||
|
*sub_folder: str) -> Optional[str]:
|
||||||
|
folder = self.config.DATA_FOLDER
|
||||||
|
if not folder:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if sub_folder:
|
||||||
|
folder = os.path.join(folder, *sub_folder)
|
||||||
|
|
||||||
|
if not os.path.isdir(folder):
|
||||||
|
os.makedirs(folder, 0o755, exist_ok=True)
|
||||||
|
return folder
|
||||||
|
|
||||||
|
def get_data_file(self, path: str, *others: str) -> Optional[str]:
|
||||||
|
rel_path = os.path.join(path, *others)
|
||||||
|
parent = self.get_data_folder(os.path.dirname(rel_path))
|
||||||
|
if not parent:
|
||||||
|
return None
|
||||||
|
return os.path.join(parent, os.path.basename(rel_path))
|
||||||
|
|
||||||
|
|
||||||
|
_bot = None
|
||||||
|
|
||||||
|
|
||||||
|
def init(config_object: Any = None) -> NoneBot:
|
||||||
|
global _bot
|
||||||
|
_bot = NoneBot(config_object)
|
||||||
|
if _bot.config.DEBUG:
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
else:
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
return _bot
|
||||||
|
|
||||||
|
|
||||||
|
def get_bot() -> NoneBot:
|
||||||
|
if _bot is None:
|
||||||
|
raise ValueError('NoneBot instance has not been initialized')
|
||||||
|
# noinspection PyTypeChecker
|
||||||
|
return _bot
|
||||||
|
|
||||||
|
|
||||||
|
def run(host: str = None, port: int = None, *args, **kwargs) -> None:
|
||||||
|
get_bot().run(host=host, port=port, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
_plugins = set()
|
_plugins = set()
|
||||||
|
|
||||||
|
|
||||||
|
def clear_plugins() -> None:
|
||||||
|
_plugins.clear()
|
||||||
|
|
||||||
|
|
||||||
def load_plugins(plugin_dir: str, module_prefix: str) -> None:
|
def load_plugins(plugin_dir: str, module_prefix: str) -> None:
|
||||||
for name in os.listdir(plugin_dir):
|
for name in os.listdir(plugin_dir):
|
||||||
path = os.path.join(plugin_dir, name)
|
path = os.path.join(plugin_dir, name)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
API_ROOT = ''
|
API_ROOT = ''
|
||||||
@ -12,3 +13,5 @@ NICKNAME = ''
|
|||||||
COMMAND_START = {'/', '!', '/', '!'}
|
COMMAND_START = {'/', '!', '/', '!'}
|
||||||
COMMAND_SEP = {'/', '.'}
|
COMMAND_SEP = {'/', '.'}
|
||||||
SESSION_EXPIRE_TIMEOUT = timedelta(minutes=5)
|
SESSION_EXPIRE_TIMEOUT = timedelta(minutes=5)
|
||||||
|
|
||||||
|
DATA_FOLDER = os.path.join(os.getcwd(), 'data')
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
from os import path
|
||||||
|
|
||||||
|
import none
|
||||||
|
from none_demo import config
|
||||||
|
|
||||||
|
bot = none.init(config)
|
||||||
|
app = bot.asgi
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
none.load_builtin_plugins()
|
||||||
|
none.load_plugins(path.join(path.dirname(__file__), 'plugins'),
|
||||||
|
'none_demo.plugins')
|
||||||
|
none.run(host=config.HOST, port=config.PORT)
|
@ -1,6 +1,4 @@
|
|||||||
from none import (
|
from none import on_natural_language, NLPSession, NLPResult
|
||||||
on_natural_language, NLPSession, NLPResult
|
|
||||||
)
|
|
||||||
|
|
||||||
_last_session = None
|
_last_session = None
|
||||||
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
from os import path
|
|
||||||
|
|
||||||
import none
|
|
||||||
from none_demo import config
|
|
||||||
|
|
||||||
bot = none.create_bot(config)
|
|
||||||
|
|
||||||
none.load_builtin_plugins()
|
|
||||||
plugin_dir = path.join(path.dirname(__file__), 'plugins')
|
|
||||||
none.load_plugins(plugin_dir, 'none_demo.plugins')
|
|
||||||
|
|
||||||
app = bot.asgi
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
bot.run(host=config.HOST, port=config.PORT)
|
|
3
setup.py
3
setup.py
@ -14,7 +14,8 @@ setup(
|
|||||||
description='A QQ bot framework',
|
description='A QQ bot framework',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
install_requires=['aiocqhttp>=0.5', 'aiocache'],
|
install_requires=['aiocqhttp>=0.5', 'aiocache',
|
||||||
|
'apscheduler', 'sqlalchemy'],
|
||||||
python_requires='>=3.6',
|
python_requires='>=3.6',
|
||||||
platforms='any',
|
platforms='any',
|
||||||
classifiers=(
|
classifiers=(
|
||||||
|
Loading…
Reference in New Issue
Block a user