Backend

Module for ‘server-side’ state during testing. This module should contain methods for altering said server-side state, which then are responsible for triggering a parse_* call in the configured client state to inform the bot of the change.

This setup matches discord’s actual setup, where an HTTP call triggers a change on the server, which is then sent back to the bot as an event which is parsed and dispatched.

class BackendState(messages: Dict[int, List[Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]]], state: FakeState)

The dpytest backend, with all the state it needs to hold to be able to pretend to be discord. Generally only used internally, but exposed through get_state()

messages: Dict[int, List[Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]]]

Alias for field number 0

state: FakeState

Alias for field number 1

__slots__ = ()
class FakeRequest(status: int, reason: str)

A fake web response, for use with discord HTTPExceptions

status: int

Alias for field number 0

reason: str

Alias for field number 1

__slots__ = ()
class FakeHttp(loop: AbstractEventLoop | None = None)

A mock implementation of an HTTPClient. Instead of actually sending requests to discord, it triggers a runner callback and calls the dpytest backend to update any necessary state and trigger any necessary fake messages to the client.

fileno: ClassVar[int] = 0
__init__(loop: AbstractEventLoop | None = None) None
state: FakeState
async request(*args: Any, **kwargs: Any) NoReturn

Overloaded to raise a NotImplemented error informing the user that the requested operation isn’t yet supported by dpytest. To fix this, the method call that triggered this error should be overloaded below to instead trigger a callback and call the appropriate backend function.

Parameters:
  • args – Arguments provided to the request

  • kwargs – Keyword arguments provided to the request

async create_channel(guild_id: int, channel_type: discord.ChannelType, *, reason: str | None = None, **options: Any) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async delete_channel(channel_id: int, *, reason: str | None = None) None
async get_channel(channel_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async start_private_message(user_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async send_message(channel_id: int, *, params: MultipartParameters) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async send_typing(channel_id: int) None
async delete_message(channel_id: int, message_id: int, *, reason: str | None = None) None
async edit_message(channel_id: int, message_id: int, **fields: MultipartParameters) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async add_reaction(channel_id: int, message_id: int, emoji: str) None
async remove_reaction(channel_id: int, message_id: int, emoji: str, member_id: int) None
async remove_own_reaction(channel_id: int, message_id: int, emoji: str) None
async clear_reactions(channel_id: int, message_id: int) None
async get_message(channel_id: int, message_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async logs_from(channel_id: int, limit: int, before: int | None = None, after: int | None = None, around: int | None = None) List[Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]]
async kick(user_id: int, guild_id: int, reason: str | None = None) None
async ban(user_id: int, guild_id: int, delete_message_days: int = 1, reason: str | None = None) None
async change_my_nickname(guild_id: int, nickname: str, *, reason: str | None = None) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async edit_member(guild_id: int, user_id: int, *, reason: str | None = None, **fields: Any) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async get_member(guild_id: int, member_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async edit_role(guild_id: int, role_id: int, *, reason: str | None = None, **fields: Any) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async delete_role(guild_id: int, role_id: int, *, reason: str | None = None) None
async create_role(guild_id: int, *, reason: str | None = None, **fields: Any) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async move_role_position(guild_id: int, positions: List[Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]], *, reason: str | None = None) None
async add_role(guild_id: int, user_id: int, role_id: int, *, reason: str | None = None) None
async remove_role(guild_id: int, user_id: int, role_id: int, *, reason: str | None = None) None
async application_info() Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async delete_channel_permissions(channel_id: int, target_id: int, *, reason: str | None = None) None
async edit_channel_permissions(channel_id: int, target_id: int, allow_value: int, deny_value: int, perm_type: str, *, reason: str | None = None) None
async get_from_cdn(url: str) bytes
async get_user(user_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
async pin_message(channel_id: int, message_id: int, reason: str | None = None) None
async unpin_message(channel_id: int, message_id: int, reason: str | None = None) None
async get_guilds(limit: int, before: int | None = None, after: int | None = None)
async get_guild(guild_id: int) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
get_state() FakeState

Get the current backend state, or raise an error if it hasn’t been configured

Returns:

Current backend state

make_guild(name: str, members: List[Member] | None = None, channels: List[TextChannel | CategoryChannel | GuildChannel | PrivateChannel] | None = None, roles: List[Role] | None = None, owner: bool = False, id_num: int = -1) Guild

Add a new guild to the backend, triggering any relevant callbacks on the configured client

Parameters:
  • name – Name of the guild

  • members – Existing members of the guild or None

  • channels – Existing channels in the guild or None

  • roles – Existing roles in the guild or None

  • owner – Whether the configured client owns the guild, default is false

  • id_num – ID of the guild, or nothing to auto-generate

Returns:

Newly created guild

update_guild(guild: Guild, roles: List[Role] | None = None) Guild

Update an existing guild with new information, triggers a guild update but not any individual item create/edit calls

Parameters:
  • guild – Guild to be updated

  • roles – New role list for the guild

Returns:

Updated guild object

make_role(name: str, guild: Guild, id_num: int = -1, colour: int = 0, color: int | None = None, permissions: int = 104324161, hoist: bool = False, mentionable: bool = False) Role

Add a new role to the backend, triggering any relevant callbacks on the configured client

Parameters:
  • name – Name of the new role

  • guild – Guild role is being added to

  • id_num – ID of the new role, or nothing to auto-generate

  • colour – Color of the new role

  • color – Alias for above

  • permissions – Permissions for the new role

  • hoist – Whether the new role is hoisted

  • mentionable – Whether the new role is mentionable

Returns:

Newly created role

update_role(role: Role, colour: int | None = None, color: int | None = None, permissions: int | None = None, hoist: bool | None = None, mentionable: bool | None = None, name: str | None = None) Role

Update an existing role with new data, triggering a role update event. Any value not passed/passed None will not update the existing value.

Parameters:
  • role – Role to update

  • colour – New color for the role

  • color – Alias for above

  • permissions – New permissions

  • hoist – New hoist value

  • mentionable – New mention value

  • name – New name for the role

Returns:

Role that was updated

delete_role(role: Role) None

Remove a role from the backend, deleting it from the guild

Parameters:

role – Role to delete

make_text_channel(name: str, guild: Guild, position: int = -1, id_num: int = -1, permission_overwrites: Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]] | None = None, parent_id: int | None = None) TextChannel
make_category_channel(name: str, guild: Guild, position: int = -1, id_num: int = -1, permission_overwrites: Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]] | None = None) CategoryChannel
make_voice_channel(name: str, guild: Guild, position: int = -1, id_num: int = -1, permission_overwrites: Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]] | None = None, parent_id: int | None = None, bitrate: int = 192, user_limit: int = 0) VoiceChannel
delete_channel(channel: TextChannel | CategoryChannel | GuildChannel | PrivateChannel) None
update_text_channel(channel: ~discord.channel.TextChannel, target: ~discord.user.User | ~discord.role.Role, override: ~discord.permissions.PermissionOverwrite | None = <object object>) None
make_user(username: str, discrim: str | int, avatar: str | None = None, id_num: int = -1) User
make_member(user: BaseUser | User, guild: Guild, nick: str | None = None, roles: List[Role] | None = None) Member
update_member(member: Member, nick: str | None = None, roles: List[Role] | None = None) Member
delete_member(member: Member) None
make_message(content: str, author: BaseUser | User, channel: TextChannel | CategoryChannel | GuildChannel | PrivateChannel, tts: bool = False, embeds: List[Embed] | None = None, attachments: List[Attachment] | None = None, nonce: int | None = None, id_num: int = -1) Message
edit_message(message: Message, **fields: MultipartParameters) Dict[str, str | int | bool | Dict[str, JsonVal] | List[JsonVal]]
find_user_mentions(content: str | None, guild: Guild | None) List[Member]
find_role_mentions(content: str | None, guild: Guild | None) List[int]
find_channel_mentions(content: str | None, guild: Guild | None) List[TextChannel | CategoryChannel | GuildChannel | PrivateChannel]
delete_message(message: Message) None
make_attachment(filename: Path, name: str | None = None, id_num: int = -1) Attachment
add_reaction(message: Message, user: BaseUser | User, emoji: str) None
remove_reaction(message: Message, user: BaseUser, emoji: str) None
clear_reactions(message: Message)
pin_message(channel_id: int, message_id: int)
unpin_message(channel_id: int, message_id: int)
configure(client: Client) None
configure(client: Client | None, *, use_dummy: bool = False) None

Configure the backend, optionally with the provided client

Parameters:
  • client – Client to use, or None

  • use_dummy – Whether to use a dummy if client param is None, or error