or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

adapters-drivers.mdbot-management.mdconfiguration.mddependencies-parameters.mdevent-handlers.mdframework-control.mdindex.mdmessage-matching.mdplugin-system.md

bot-management.mddocs/

0

# Bot Management

1

2

Functions for accessing and managing bot instances connected to the NoneBot2 framework. Bots represent connections to specific messaging platforms and handle message sending/receiving.

3

4

## Capabilities

5

6

### Bot Access

7

8

Get specific bot instances by ID or retrieve any available bot.

9

10

```python { .api }

11

def get_bot(self_id: Optional[str] = None) -> Bot:

12

"""

13

Get a Bot object connected to NoneBot.

14

15

Parameters:

16

- self_id: Bot identifier (Bot.self_id attribute), optional

17

18

Returns:

19

Bot: Bot object instance

20

21

Raises:

22

KeyError: If specified self_id bot doesn't exist

23

ValueError: If no self_id provided and no bots available

24

ValueError: If global Driver object not initialized

25

"""

26

```

27

28

Usage example:

29

30

```python

31

import nonebot

32

33

# Get any available bot

34

bot = nonebot.get_bot()

35

36

# Get specific bot by ID

37

bot = nonebot.get_bot("12345")

38

39

# Use bot to send messages

40

await bot.send_private_msg(user_id="67890", message="Hello!")

41

```

42

43

### Multiple Bot Management

44

45

Access all connected bot instances.

46

47

```python { .api }

48

def get_bots() -> dict[str, Bot]:

49

"""

50

Get all Bot objects connected to NoneBot.

51

52

Returns:

53

dict[str, Bot]: Dictionary with Bot.self_id as keys and Bot objects as values

54

55

Raises:

56

ValueError: If global Driver object not initialized

57

"""

58

```

59

60

Usage example:

61

62

```python

63

import nonebot

64

65

# Get all bots

66

bots = nonebot.get_bots()

67

68

# Iterate through all bots

69

for bot_id, bot in bots.items():

70

print(f"Bot {bot_id}: {bot.type}")

71

72

# Send message to all bots

73

for bot in bots.values():

74

await bot.send_private_msg(user_id="user123", message="Broadcast message")

75

```

76

77

### Adapter Access

78

79

Get registered adapter instances by name or type.

80

81

```python { .api }

82

def get_adapter(name: Union[str, type[Adapter]]) -> Adapter:

83

"""

84

Get registered Adapter instance.

85

86

Parameters:

87

- name: Adapter name (str) or Adapter class type

88

89

Returns:

90

Adapter: Specified Adapter object

91

92

Raises:

93

ValueError: If specified Adapter not registered

94

ValueError: If global Driver object not initialized

95

"""

96

```

97

98

Usage example:

99

100

```python

101

import nonebot

102

from nonebot.adapters.console import Adapter as ConsoleAdapter

103

104

# Get adapter by name

105

adapter = nonebot.get_adapter("Console")

106

107

# Get adapter by type

108

adapter = nonebot.get_adapter(ConsoleAdapter)

109

110

# Use adapter for advanced operations

111

print(f"Adapter name: {adapter.get_name()}")

112

```

113

114

### All Adapters Access

115

116

Retrieve all registered adapter instances.

117

118

```python { .api }

119

def get_adapters() -> dict[str, Adapter]:

120

"""

121

Get all registered Adapter instances.

122

123

Returns:

124

dict[str, Adapter]: Dictionary of all Adapter instances

125

126

Raises:

127

ValueError: If global Driver object not initialized

128

"""

129

```

130

131

Usage example:

132

133

```python

134

import nonebot

135

136

# Get all adapters

137

adapters = nonebot.get_adapters()

138

139

# List all available adapters

140

for adapter_name, adapter in adapters.items():

141

print(f"Adapter: {adapter_name}")

142

143

# Check if specific adapter is available

144

if "Console" in adapters:

145

console_adapter = adapters["Console"]

146

```

147

148

## Types

149

150

### Base Types

151

152

```python { .api }

153

class Bot(abc.ABC):

154

"""Base class for bot implementations."""

155

156

def __init__(self, adapter: Adapter, self_id: str): ...

157

158

async def call_api(self, api: str, **data: Any) -> Any:

159

"""Call bot API method."""

160

161

@property

162

def type(self) -> str:

163

"""Get adapter name."""

164

165

@property

166

def config(self) -> Config:

167

"""Get global configuration."""

168

169

class Adapter(abc.ABC):

170

"""Base class for protocol adapters."""

171

172

def get_name(self) -> str:

173

"""Get adapter name."""

174

175

def bot_connect(self, bot: Bot) -> None:

176

"""Handle bot connection."""

177

178

def bot_disconnect(self, bot: Bot) -> None:

179

"""Handle bot disconnection."""

180

```