Options
The Option class provides type-safe decorators for registering command options. Each static method corresponds to a Discord option type and ensures type correctness at the Python level.
Using options
Options are registered using Option static methods as decorators on command callbacks:
from fastapi_interactions.commands import CommandRouter, Option
@router.command(name="greet", description="Greet someone")
@Option.string(name="name", description="Who to greet", required=True)
async def greet(ctx, name: str):
return f"Hello, {name}!"
The parameter name in your function must match the option name. The framework automatically binds option values to function parameters.
Available options
String options
Accepts any text input. Default option type if no specific type is needed.
Integer options
Accepts whole numbers only. Discord enforces integer validation client-side.
Number options
Accepts decimal numbers (floats). Use for prices, ratings, percentages, or any fractional value.
Boolean options
Presents a true/false toggle to the user.
User options
Discord's user picker. The parameter receives a snowflake ID (string) of the selected user.
Channel options
Discord's channel picker. The parameter receives a snowflake ID (string) of the selected channel.
Role options
Discord's role picker. The parameter receives a snowflake ID (string) of the selected role.
Mentionable options
Combines user and role pickers. The parameter receives a snowflake ID (string) that can be either a user or role.
Reference
fastapi_interactions.commands.Option
Source code in fastapi_interactions/commands.py
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
boolean
staticmethod
Register a boolean option on a command.
Decorates a command callback to add a boolean option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="ban", description="Ban a user")
@Option.user(name="target", description="User to ban", required=True)
@Option.boolean(name="soft", description="Is this a softban", required=True)
async def warn(ctx, target: Snowflake, soft: bool):
if soft:
...
else:
...
return 'Command executed'
Source code in fastapi_interactions/commands.py
channel
staticmethod
Register a channel option on a command.
Decorates a command callback to add a channel option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="purge", description="Purge a channel")
@Option.user(name="channel", description="Target channel to purge", required=True)
async def purge(ctx, channel: Snowflake):
...
return 'Channel purged'
Source code in fastapi_interactions/commands.py
integer
staticmethod
Register an integer option on a command.
Decorates a command callback to add an integer option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="echo", description="Echo integer")
@Option.integer(name="number", description="Integer to echo", required=True)
async def echo(ctx, number: int):
return int
Source code in fastapi_interactions/commands.py
mentionable
staticmethod
Register a mentionable option on a command.
Decorates a command callback to add a channel option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="warn", description="Warn a user or role")
@Option.mentionable(name="target", description="User or role to warn", required=True)
async def warn(ctx, target: Snowflake):
return f"⚠️ Warning issued to <@&{target}>"
Source code in fastapi_interactions/commands.py
number
staticmethod
Register a number option on a command.
Decorates a command callback to add a number option parameter. The option name must match a parameter name in the callback for automatic value binding. This is a float in python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="rate", description="Rate something")
@Option.number(name="score", description="Rating from 0 to 10", required=True)
async def rate(ctx, score: float):
return f"Rating: {score}/10 ⭐"
Source code in fastapi_interactions/commands.py
role
staticmethod
Register a role option on a command.
Decorates a command callback to add a channel option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="rmrole", description="Delete a role")
@Option.role(name="role", description="role to delete", required=True)
async def purge(ctx, role: Snowflake):
...
return 'Role purged'
Source code in fastapi_interactions/commands.py
string
staticmethod
Register a string option on a command.
Decorates a command callback to add a string option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="echo", description="Echo text")
@Option.string(name="text", description="Text to echo", required=True)
async def echo(ctx, text: str):
return text
Source code in fastapi_interactions/commands.py
user
staticmethod
Register a user option on a command.
Decorates a command callback to add a user option parameter. The option name must match a parameter name in the callback for automatic value binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The option name. Must match a callback parameter name. |
required |
description
|
str
|
Human-readable description shown to Discord users. |
required |
required
|
bool
|
Whether the option is required. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
|
A decorator that attaches the option metadata to the function. |
Example:
@router.command(name="kick", description="Kick a user")
@Option.user(name="user", description="Target user to kick", required=True)
async def kick(ctx, user: Snowflake):
...
return 'User kicked'