mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 00:55:07 +08:00
Add some docstrings
This commit is contained in:
parent
1f4e5f3435
commit
e639889944
@ -67,17 +67,30 @@ class NoneBot(CQHttp):
|
||||
_bot = None
|
||||
|
||||
|
||||
def init(config_object: Any = None) -> NoneBot:
|
||||
def init(config_object: Any = None) -> None:
|
||||
"""
|
||||
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
|
||||
"""
|
||||
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:
|
||||
"""
|
||||
Get the NoneBot instance.
|
||||
|
||||
:raise ValueError: instance not initialized
|
||||
"""
|
||||
if _bot is None:
|
||||
raise ValueError('NoneBot instance has not been initialized')
|
||||
# noinspection PyTypeChecker
|
||||
@ -85,6 +98,7 @@ def get_bot() -> NoneBot:
|
||||
|
||||
|
||||
def run(host: str = None, port: int = None, *args, **kwargs) -> None:
|
||||
"""Run the NoneBot instance."""
|
||||
get_bot().run(host=host, port=port, *args, **kwargs)
|
||||
|
||||
|
||||
|
@ -6,9 +6,7 @@ from . import NoneBot, expression
|
||||
|
||||
|
||||
def context_id(ctx: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Calculate a unique id representing the current user.
|
||||
"""
|
||||
"""Calculate a unique id representing the current user."""
|
||||
src = ''
|
||||
if ctx.get('group_id'):
|
||||
src += f'/group/{ctx["group_id"]}'
|
||||
@ -22,9 +20,7 @@ def context_id(ctx: Dict[str, Any]) -> str:
|
||||
async def send(bot: NoneBot, ctx: Dict[str, Any],
|
||||
message: Union[str, Dict[str, Any], List[Dict[str, Any]]],
|
||||
*, ignore_failure: bool = True) -> None:
|
||||
"""
|
||||
Send a message ignoring failure by default.
|
||||
"""
|
||||
"""Send a message ignoring failure by default."""
|
||||
try:
|
||||
if ctx.get('post_type') == 'message':
|
||||
await bot.send(ctx, message)
|
||||
@ -46,7 +42,5 @@ async def send(bot: NoneBot, ctx: Dict[str, Any],
|
||||
async def send_expr(bot: NoneBot, ctx: Dict[str, Any],
|
||||
expr: Union[str, Sequence[str], Callable],
|
||||
**kwargs):
|
||||
"""
|
||||
Sending a expression message ignoring failure by default.
|
||||
"""
|
||||
"""Sending a expression message ignoring failure by default."""
|
||||
return await send(bot, ctx, expression.render(expr, **kwargs))
|
||||
|
@ -14,16 +14,12 @@ class BaseSession:
|
||||
async def send(self,
|
||||
message: Union[str, Dict[str, Any], List[Dict[str, Any]]],
|
||||
*, ignore_failure: bool = True) -> None:
|
||||
"""
|
||||
Send a message ignoring failure by default.
|
||||
"""
|
||||
"""Send a message ignoring failure by default."""
|
||||
return await send(self.bot, self.ctx, message,
|
||||
ignore_failure=ignore_failure)
|
||||
|
||||
async def send_expr(self,
|
||||
expr: Union[str, Sequence[str], Callable],
|
||||
**kwargs):
|
||||
"""
|
||||
Sending a expression message ignoring failure by default.
|
||||
"""
|
||||
"""Sending a expression message ignoring failure by default."""
|
||||
return await send_expr(self.bot, self.ctx, expr, **kwargs)
|
||||
|
@ -3,8 +3,8 @@ from os import path
|
||||
import none
|
||||
from none_demo import config
|
||||
|
||||
bot = none.init(config)
|
||||
app = bot.asgi
|
||||
none.init(config)
|
||||
app = none.get_bot().asgi
|
||||
|
||||
if __name__ == '__main__':
|
||||
none.load_builtin_plugins()
|
||||
|
Loading…
Reference in New Issue
Block a user