Bot
The Bot class is the central object of every FastAPI Interactions app. It owns the FastAPI instance, the command registry, and the interactions endpoint.
Usage
from fastapi_interactions import Bot
bot = Bot(
app_id=123456789,
public_key="your_public_key",
bot_token="Bot your_token",
)
bot.load_commands("commands")
app = bot.app
Reference
fastapi_interactions.bot.Bot
Source code in fastapi_interactions/bot.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
__init__
Initialize a new Discord HTTP webhook bot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_id
|
int
|
The Discord Application ID. |
required |
public_key
|
str
|
Public key used to validate incoming webhook signatures from Discord. |
required |
bot_token
|
str
|
Bot token used for authenticated API calls to Discord (command registration, message sending, etc.). |
required |
interactions_path
|
str
|
The route path where the bot listens for interaction webhooks. Defaults to "/interactions". |
'/interactions'
|
Source code in fastapi_interactions/bot.py
__load_routers_from_module
Discover and register routers from a module.
Checks for an explicit routers list first. If not found, scans the module for all CommandRouter instances and registers them automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType
|
The module to scan for routers. |
required |
Source code in fastapi_interactions/bot.py
__put__commands
Send a batch of commands to Discord's API.
Makes a PUT request to the specified Discord endpoint with the command payload. Handles both global and guild-scoped command registration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The Discord API endpoint (global or guild-scoped). |
required |
payload
|
list
|
List of command definitions to register. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If the HTTP response status is not 200. |
Source code in fastapi_interactions/bot.py
attach_router
Attach a CommandRouter to the bot.
Registers a CommandRouter with this Discord webhook bot, enabling it to handle slash commands, message components, modals, and other interactions defined within the router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
CommandRouter
|
The router instance containing command registrations and handler mappings. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided router is not an instance of CommandRouter. |
Source code in fastapi_interactions/bot.py
delete_all_commands
Delete all application commands by replacing the command set with an empty list.
This performs a bulk overwrite operation. If guild_id is not
provided, all global commands are deleted. Otherwise, all commands
registered for the specified guild are deleted.
Parameters
guild_id : Snowflake, optional
The guild ID to target. If None, the global command scope
is used.
Returns
None
Source code in fastapi_interactions/bot.py
dispatch
async
Invoke a command and return its response.
Looks up the command by name and invokes its callback with the provided context. Option values are automatically bound to the callback's parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_name
|
str
|
The name of the command to invoke. |
required |
ctx
|
Context
|
The interaction context. |
required |
Returns:
| Type | Description |
|---|---|
InteractionResponse
|
An InteractionResponse instance |
Source code in fastapi_interactions/bot.py
load_extension
Load extension(s) from a module or package.
Dynamically imports the given Python path and registers all CommandRouter
instances by calling the internal __load_routers_from_module method.
Behavior
- If
pathpoints to a module: Loads routers from that single module. - If
pathpoints to a package: Recursively discovers and loads all non-package modules within it (and its subpackages).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Dot-separated import path to a module or package. |
required |
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If the module or package does not exist. |
ImportError
|
If an error occurs while importing any module. |
Examples:
Load a single module:
Load all modules from a package(recursive):Source code in fastapi_interactions/bot.py
sync_commands
Sync all registered commands with Discord's API.
Sends a bulk overwrite of the bot's slash commands using the Discord Application Commands endpoint.
Raises:
| Type | Description |
|---|---|
Exception
|
If the API request fails. |
Source code in fastapi_interactions/bot.py
fastapi_interactions.bot.call_with_options
async
Invoke a command callback with option values bound to parameters.
Inspects the callback's signature and matches each parameter name to a registered option in the interaction context. Values are passed as keyword arguments to the callback. Parameters with default values are optional; missing required parameters raise an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
callable
|
The async command function to invoke. |
required |
ctx
|
Context
|
The interaction context containing option values. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The awaited result of the callback invocation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a required parameter has no matching option value in the context. |