0
# Framework Control
1
2
Core functions for initializing, configuring, and running the NoneBot2 framework. These functions manage the global framework state and lifecycle.
3
4
## Capabilities
5
6
### Framework Initialization
7
8
Initialize the NoneBot2 framework with configuration options.
9
10
```python { .api }
11
def init(*, _env_file: Optional[DOTENV_TYPE] = None, **kwargs: Any) -> None:
12
"""
13
Initialize NoneBot and global Driver object.
14
15
Parameters:
16
- _env_file: Configuration file name, defaults to .env.{env_name}
17
- **kwargs: Additional variables stored in Driver.config object
18
19
Raises:
20
- ValueError: If initialization fails
21
"""
22
```
23
24
Usage example:
25
26
```python
27
import nonebot
28
29
# Basic initialization
30
nonebot.init()
31
32
# With custom configuration
33
nonebot.init(
34
driver="~fastapi+~httpx+~websockets",
35
host="0.0.0.0",
36
port=8080,
37
log_level="DEBUG"
38
)
39
40
# With custom environment file
41
nonebot.init(_env_file=".env.development")
42
```
43
44
### Framework Execution
45
46
Start the NoneBot2 framework and run the event loop.
47
48
```python { .api }
49
def run(*args: Any, **kwargs: Any) -> None:
50
"""
51
Start NoneBot by running the global Driver object.
52
53
Parameters:
54
- *args: Positional arguments passed to Driver.run()
55
- **kwargs: Keyword arguments passed to Driver.run()
56
"""
57
```
58
59
Usage example:
60
61
```python
62
import nonebot
63
64
# Initialize framework
65
nonebot.init()
66
67
# Run with default settings
68
nonebot.run()
69
70
# Run with custom arguments (driver-specific)
71
nonebot.run(host="127.0.0.1", port=8080)
72
```
73
74
### Driver Access
75
76
Get access to the global Driver instance for advanced operations.
77
78
```python { .api }
79
def get_driver() -> Driver:
80
"""
81
Get the global Driver instance.
82
83
Returns:
84
Driver: Global Driver object
85
86
Raises:
87
ValueError: If NoneBot has not been initialized (init() not called)
88
"""
89
```
90
91
Usage example:
92
93
```python
94
import nonebot
95
96
# Must initialize first
97
nonebot.init()
98
99
# Get driver instance
100
driver = nonebot.get_driver()
101
102
# Use driver for advanced operations
103
@driver.on_startup
104
async def startup():
105
print("NoneBot is starting up...")
106
107
@driver.on_shutdown
108
async def shutdown():
109
print("NoneBot is shutting down...")
110
```
111
112
### ASGI Support
113
114
Access ASGI-related functionality for web framework integration.
115
116
```python { .api }
117
def get_app() -> Any:
118
"""
119
Get the Server App object for ASGIMixin drivers.
120
121
Returns:
122
Any: Server App object (e.g., FastAPI app, Quart app)
123
124
Raises:
125
AssertionError: If global Driver is not ASGIMixin type
126
ValueError: If global Driver object is not initialized
127
"""
128
```
129
130
```python { .api }
131
def get_asgi() -> Any:
132
"""
133
Get the ASGI application object for ASGIMixin drivers.
134
135
Returns:
136
Any: ASGI application object
137
138
Raises:
139
AssertionError: If global Driver is not ASGIMixin type
140
ValueError: If global Driver object is not initialized
141
"""
142
```
143
144
Usage example:
145
146
```python
147
import nonebot
148
149
# Initialize with ASGI-compatible driver
150
nonebot.init(driver="~fastapi")
151
152
# Get app for custom routes
153
app = nonebot.get_app()
154
155
@app.get("/health")
156
async def health_check():
157
return {"status": "ok"}
158
159
# Get ASGI app for deployment
160
asgi_app = nonebot.get_asgi()
161
```
162
163
## Types
164
165
```python { .api }
166
DOTENV_TYPE = Union[Path, str, list[Union[Path, str]], tuple[Union[Path, str], ...]]
167
```