nonebot2/little_shit.py

105 lines
2.8 KiB
Python
Raw Normal View History

import importlib
2016-12-02 22:24:19 +08:00
import os
2017-01-13 21:03:44 +08:00
import functools
2016-12-02 22:24:19 +08:00
from config import config
2016-12-03 17:16:16 +08:00
class SkipException(Exception):
2016-12-02 22:24:19 +08:00
pass
def _mkdir_if_not_exists_and_return_path(path):
os.makedirs(path, exist_ok=True)
return path
def get_root_dir():
return os.path.split(os.path.realpath(__file__))[0]
def get_plugin_dir(plugin_dir_name):
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), plugin_dir_name))
2016-12-08 21:58:49 +08:00
def load_plugins(plugin_dir_name, module_callback=None):
plugin_dir = get_plugin_dir(plugin_dir_name)
plugin_files = filter(
lambda filename: filename.endswith('.py') and not filename.startswith('_'),
os.listdir(plugin_dir)
)
plugins = [os.path.splitext(file)[0] for file in plugin_files]
for mod_name in plugins:
mod = importlib.import_module(plugin_dir_name + '.' + mod_name)
if module_callback:
module_callback(mod)
2016-12-02 22:24:19 +08:00
def get_db_dir():
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'data', 'db'))
def get_default_db_path():
2016-12-03 17:16:16 +08:00
return os.path.join(get_db_dir(), 'default.sqlite')
2016-12-02 22:24:19 +08:00
def get_tmp_dir():
return _mkdir_if_not_exists_and_return_path(os.path.join(get_root_dir(), 'data', 'tmp'))
def get_source(ctx_msg):
from msg_src_adapter import get_adapter_by_ctx
return get_adapter_by_ctx(ctx_msg).get_source(ctx_msg)
2016-12-02 22:24:19 +08:00
def get_target(ctx_msg):
from msg_src_adapter import get_adapter_by_ctx
return get_adapter_by_ctx(ctx_msg).get_target(ctx_msg)
2016-12-02 22:24:19 +08:00
2017-01-13 21:03:44 +08:00
def check_target(func):
"""
This decorator checks whether there is a target value, and prevent calling the function if not.
"""
@functools.wraps(func)
def wrapper(args_text, ctx_msg, *args, **kwargs):
from msg_src_adapter import get_adapter_by_ctx
adapter = get_adapter_by_ctx(ctx_msg)
target = adapter.get_target(ctx_msg)
2017-01-13 21:03:44 +08:00
if not target:
adapter.send_message(ctx_msg, '当前语境无法使用这个命令,请尝试发送私聊消息或稍后再试吧~')
2017-01-13 21:03:44 +08:00
return
else:
return func(args_text, ctx_msg, *args, **kwargs)
return wrapper
2016-12-02 22:24:19 +08:00
def get_command_start_flags():
2017-01-11 15:20:00 +08:00
return tuple(sorted(config.get('command_start_flags', ('',)), reverse=True))
2016-12-02 22:24:19 +08:00
def get_command_name_separators():
2017-01-11 15:20:00 +08:00
return tuple(sorted(('\.',) + config.get('command_name_separators', ()), reverse=True))
2016-12-02 22:24:19 +08:00
def get_command_args_start_flags():
return tuple(sorted(('[ \\t\\n]+',) + config.get('command_args_start_flags', ()), reverse=True))
2017-01-11 15:20:00 +08:00
def get_command_args_separators():
return tuple(sorted(('[ \\t\\n]+',) + config.get('command_args_separators', ()), reverse=True))
def get_fallback_command():
return config.get('fallback_command')
def get_fallback_command_after_nl_processors():
return config.get('fallback_command_after_nl_processors')
def get_message_sources():
return config.get('message_sources', [])