API reference¶
The complete public API, generated from the source. Everything here is importable from the
top-level simcord package.
New here?
Read Core concepts first — it explains how these objects relate (builders arrange the world, actors act as users, queries assert) before you dive into signatures.
Entry point¶
simcord.run ¶
async with simcord.run(bot) as env: — attach, fake-login, READY.
On exit, if the bot raised errors the test never inspected (via
env.errors or env.raise_errors()), they are re-raised as an
ExceptionGroup so bot bugs cannot pass silently. Opt out with
simcord.run(bot, check_errors=False).
Source code in src/simcord/env.py
simcord.Env ¶
A running test environment around a single bot.
Use via :func:simcord.run::
async with simcord.run(bot) as env:
guild = env.create_guild()
...
Only one Env may be live per event loop at a time: start()
monkeypatches loop.create_task to track the bot's tasks, and nesting two
environments on one loop would corrupt each other's task bookkeeping.
Source code in src/simcord/env.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
errors
property
¶
Errors the bot raised (command handlers, app commands, listeners).
Reading this marks the errors as inspected: simcord.run then trusts the
test's own assertions instead of failing it at teardown.
http_log
property
¶
Every REST call the bot made: (method, path, json body).
restart_bot
async
¶
Simulate a bot restart while the virtual world persists.
Detaches the current bot, attaches bot (or the same instance if
omitted), and replays GUILD_CREATE for every existing guild so the
new client's cache repopulates exactly as on a first run — letting tests
prove that persistent views (bot.add_view in setup_hook)
re-attach to messages they never saw created.
Pass a freshly built client for a faithful restart: re-running the same
instance re-executes setup_hook (which typically reloads extensions
and would fail). The virtual clock is preserved — the world does not
rewind. Errors the old bot raised are preserved too: a restart does not
launder away un-inspected bot bugs.
Source code in src/simcord/env.py
settle
async
¶
Wait until the bot has finished reacting to injected events.
Waits for all tracked tasks to complete. A task that completes no work
within an idle window is only abandoned if it is genuinely parked on
a future (e.g. blocked in wait_for for a later user action) — if the
loop still has timers scheduled to fire before timeout (e.g. an
asyncio.sleep in a cooldown or backoff), we keep waiting for them.
If pending tasks neither finish nor park before timeout, a
TimeoutError with the pending tasks is raised.
Source code in src/simcord/env.py
advance_time
async
¶
Fast-forward the virtual clock, firing every timer that becomes due.
View timeouts, cooldown resets, asyncio.sleep chains — anything the
bot scheduled against the loop's clock — fire as if seconds of real
time had passed, without waiting. Timers are consumed in order (a chain
of three 60s sleeps completes within advance_time(180)), and the
bot's reactions are settled after each step.
Source code in src/simcord/env.py
create_user ¶
create_user(name: str, *, bot: bool = False, system: bool = False, global_name: str | None = None, discriminator: str = '0', public_flags: PublicUserFlags | None = None) -> UserHandle
Create a virtual user.
bot=True makes messages this user posts arrive with
message.author.bot set — the way a bot/application account, or a
webhook (see :meth:GuildHandle.create_webhook), appears to the bot
under test. system=True marks an official Discord system account.
global_name is the display name (distinct from the unique
name/username); discriminator is the legacy four-digit tag
("0" for migrated accounts); public_flags carries badge flags
such as verified_bot.
Source code in src/simcord/env.py
create_guild ¶
create_guild(name: str = 'Test Guild', *, id: int | None = None, owner: UserHandle | None = None, description: str | None = None, verification_level: VerificationLevel | None = None, notifications: NotificationLevel | None = None, content_filter: ContentFilter | None = None, preferred_locale: str | None = None, afk_timeout: int | None = None) -> GuildHandle
Create a guild.
Pass id to pin a known id — e.g. to match a bot that syncs its
commands to a hardcoded guild id, so strict_sync can stay on. Pass
owner to make a specific user the guild owner (owners bypass every
permission check); by default a fresh synthetic owner is created so the
bot never owns the guild. The remaining keywords seed guild settings the
bot can read back off discord.Guild and could later change itself via
Guild.edit (the keyword names here are friendlier aliases — e.g.
notifications for default_notifications).
Source code in src/simcord/env.py
transcript ¶
Human-readable record of everything that happened, in order.
One line per gateway event injected and REST call the bot made — the "what did the bot actually do" dump, including events DROPPED (missing intent) or CENSORED (missing message_content) by intent simulation. The pytest plugin attaches this to failing tests automatically.
Source code in src/simcord/env.py
raise_errors ¶
Re-raise everything the bot raised during the test, as a group.
Exceptions from command handlers, app-command callbacks and event
listeners are captured into :attr:errors rather than propagating into
your test (that is what lets a bot keep running after one handler
fails). Call this to assert the bot ran cleanly: it raises an
ExceptionGroup of everything captured — even a single error — and
does nothing if there were none.
Source code in src/simcord/env.py
inject_error ¶
inject_error(method: str, path: str, *, status: int = 500, code: int = 0, message: str = 'Internal Server Error (injected by test)', times: int | None = 1) -> None
Make matching REST calls fail, to test the bot's error handling.
path is an fnmatch pattern against the API path, e.g.
"/channels/*/messages"; method may be "*". times=None
keeps the fault active for the rest of the test.
Source code in src/simcord/env.py
Builders¶
Synchronous, omnipotent handles for arranging the virtual Discord. Returned by env/guild
methods. See Core concepts → Builders.
simcord.GuildHandle ¶
Source code in src/simcord/builders.py
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
create_scheduled_event ¶
create_scheduled_event(name: str, *, start_time: str, entity_type: int = 2, channel: ChannelHandle | None = None, description: str | None = None, end_time: str | None = None, location: str | None = None) -> ScheduledEvent
Create a scheduled event directly (omnipotent setup).
Source code in src/simcord/builders.py
create_webhook ¶
Create an incoming webhook bound to channel (omnipotent setup).
Use the returned handle's :meth:~WebhookHandle.send to post messages
as the webhook would — they arrive with message.webhook_id set and
message.author.bot True, which is how a webhook (a GitHub/CI
integration, another service posting an embed, etc.) appears to the bot
under test. This is the test-driven counterpart to the bot creating and
executing a webhook itself over the API.
Source code in src/simcord/builders.py
set_command_permissions ¶
set_command_permissions(command: Any, permissions: dict[RoleHandle | ChannelHandle | UserHandle | MemberActor, bool]) -> None
Set an app command's per-guild permission overrides (omnipotent setup).
command is a command id or any object with an id (e.g. a fetched
discord.app_commands.AppCommand); permissions maps role/user/
channel handles to allow (True) / deny (False). The bot then
reads these back via AppCommand.fetch_permissions.
Source code in src/simcord/builders.py
audit_log ¶
voice_states ¶
set_vanity_url ¶
Give the guild a vanity invite code so Guild.vanity_invite() resolves.
Discord backs a vanity URL with a real invite, so this stores one under
code pointing at the guild's first channel — the populated path is
genuine modelled state a test can read back, not a constant fake.
Source code in src/simcord/builders.py
remove_member ¶
simcord.ChannelHandle ¶
Source code in src/simcord/builders.py
history ¶
All messages, oldest first, as real discord.Message objects.
With viewer=, ephemeral messages not addressed to that user are
hidden — exactly what that user would see in their client.
Source code in src/simcord/builders.py
simcord.RoleHandle ¶
Source code in src/simcord/builders.py
simcord.UserHandle ¶
A virtual human user (independent of any guild).
Source code in src/simcord/builders.py
global_name
property
¶
The user's display name, if distinct from the username.
send_dm
async
¶
DM the bot as this user.
Source code in src/simcord/builders.py
Actors¶
The simulated human that drives your bot. Created by guild.add_member(...). See
Core concepts → Actors.
simcord.MemberActor ¶
A guild member that acts like a real human user.
Source code in src/simcord/actors.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
slash
async
¶
Invoke a synced slash command (use spaces for subcommands: "config set").
Source code in src/simcord/actors.py
context_menu
async
¶
context_menu(channel: ChannelHandle, name: str, target: MemberActor | MessageLike) -> InteractionResult
Invoke a user or message context-menu command on a target.
Source code in src/simcord/actors.py
autocomplete
async
¶
autocomplete(channel: ChannelHandle, name: str, option: str, value: str, /, **filled: Any) -> list[dict[str, Any]]
Type into an autocomplete option; returns the choices the bot offered.
Source code in src/simcord/actors.py
click
async
¶
click(message: MessageLike, *, label: str | None = None, custom_id: str | None = None) -> InteractionResult
Click a button on a message, exactly as a user could.
Source code in src/simcord/actors.py
select
async
¶
select(message: MessageLike, values: Sequence[Any], *, custom_id: str | None = None) -> InteractionResult
Choose values in a select menu.
For a string select, values are the option strings. For user/role/
channel/mentionable selects, values are the matching handles
(:class:UserHandle/:class:MemberActor, :class:RoleHandle,
:class:ChannelHandle) — exactly the entities a real user could pick.
Source code in src/simcord/actors.py
submit_modal
async
¶
Fill in and submit a modal the bot previously showed this user.
Source code in src/simcord/actors.py
vote
async
¶
Cast (or move) this user's vote to answer (a 1-based answer id).
Source code in src/simcord/actors.py
remove_vote
async
¶
Retract this user's vote for answer.
Source code in src/simcord/actors.py
join_voice
async
¶
Connect to a voice/stage channel (state only — no audio).
Source code in src/simcord/actors.py
leave_voice
async
¶
set_voice
async
¶
Update self-mute/self-deaf while connected.
Source code in src/simcord/actors.py
subscribe_event
async
¶
Mark interest in a scheduled event (accepts an id or a handle with .id).
Source code in src/simcord/actors.py
Results¶
Returned by the interaction verbs (slash, context_menu, click, select,
submit_modal). See Slash commands → Inspecting the result.
simcord.InteractionResult ¶
Everything that happened in response to a simulated interaction.
Source code in src/simcord/results.py
modal
property
¶
The raw modal payload, if the bot responded with a modal.
simcord.ResponseMessage ¶
A message the bot sent in response to an interaction.
Source code in src/simcord/results.py
Assertions¶
Runner-agnostic helpers whose failure messages print what the bot actually did. See Errors & diagnostics → Assertions.
simcord.assert_sent ¶
assert_sent(channel: ChannelHandle, *, content: str | None = None, contains: str | None = None, embed_title: str | None = None, viewer: MemberActor | UserHandle | None = None) -> None
Assert the channel's most recent (visible) message matches.
viewer= filters to what that user can see, hiding ephemeral messages
addressed to others — the same rule as :meth:ChannelHandle.history.
Source code in src/simcord/asserts.py
simcord.assert_responded ¶
assert_responded(result: InteractionResult, *, content: str | None = None, contains: str | None = None, embed_title: str | None = None, ephemeral: bool | None = None) -> None
Assert the interaction produced a response message matching the given fields.
On failure the message includes the interaction's repr, which shows whether it acknowledged, deferred, or opened a modal instead of sending a response.
Source code in src/simcord/asserts.py
simcord.assert_message ¶
assert_message(message: MessageLike, *, content: str | None = None, contains: str | None = None, embed_title: str | None = None, ephemeral: bool | None = None) -> None
Assert a single message matches the given fields. Each field is checked
only when provided. Accepts a ResponseMessage or a real discord.Message.
Source code in src/simcord/asserts.py
simcord.assert_error ¶
assert_error(env: Env, exc_type: type[BaseException] = BaseException, *, code: int | None = None, contains: str | None = None) -> BaseException
Assert the bot captured an error matching exc_type/code/contains.
discord.py wraps callback failures (e.g. CommandInvokeError.original), so
the type and code are matched against the error and its .original.
Returns the matched error. Reading env.errors marks errors inspected, so
this also satisfies the teardown check_errors guard.
Source code in src/simcord/asserts.py
simcord.assert_no_errors ¶
Assert the bot ran cleanly: raises an ExceptionGroup of anything it
captured, or does nothing. A clearer-named pairing for :func:assert_error
over :meth:Env.raise_errors.
Source code in src/simcord/asserts.py
Errors¶
simcord.BackendError ¶
Bases: Exception
Source code in src/simcord/backend/errors.py
simcord.SetupError ¶
simcord.RouteNotImplemented ¶
Bases: BackendError
An unimplemented route — a parity gap that must surface loudly.
Although it subclasses :class:BackendError, the transports catch it (and
:class:UnsupportedField) before the generic except BackendError that
maps backend failures onto Discord errors, so it is never disguised as an
HTTPException a broad except could swallow. For the same reason a
handler must never wrap a route lookup or :meth:RequestContext.fields in a
broad except BackendError — doing so would re-hide the parity gap.
Source code in src/simcord/http/router.py
simcord.UnsupportedField ¶
Bases: BackendError
A handler was sent a request field it does not honour.
The field-level analogue of :class:RouteNotImplemented: silently dropping
an edit field would let a bot test pass while the real edit diverges. Like
RouteNotImplemented it is not disguised as an HTTPException (the
transports re-raise it), so a broad except discord.HTTPException cannot
swallow a parity gap. It does subclass :class:BackendError, so — as with
RouteNotImplemented — a handler must never wrap :meth:RequestContext.fields
in a broad except BackendError, which would re-hide the gap.