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, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]]], state: discord.ext.test.state.FakeState)¶
The testcord 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, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]]]¶
Alias for field number 0
- state: discord.ext.test.state.FakeState¶
Alias for field number 1
- __slots__ = ()¶
- class FakeRequest(status: int, reason: str)¶
A fake web response, for use with discord
HTTPException- __slots__ = ()¶
- class FakeHttp(loop: Optional[asyncio.events.AbstractEventLoop] = None)¶
A mock implementation of an
HTTPClient. Instead of actually sending requests to discord, it triggers a runner callback and calls thetestcordbackend to update any necessary state and trigger any necessary fake messages to the client.- 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
testcord. 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: Optional[str] = None, **options: Any) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async get_channel(channel_id: int) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async start_private_message(user_id: int) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async send_message(channel_id: Union[str, int], content: Optional[str], *, tts: bool = False, embed: Optional[discord.types.embed.Embed] = None, embeds: Optional[List[discord.types.embed.Embed]] = None, nonce: Optional[str] = None, allowed_mentions: Optional[discord.types.message.AllowedMentions] = None, message_reference: Optional[discord.types.message.MessageReference] = None, stickers: Optional[List[discord.types.sticker.StickerItem]] = None, components: Optional[List[Union[discord.types.components.ActionRow, discord.types.components.ButtonComponent, discord.types.components.SelectMenu]]] = None) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
-
async send_files(channel_id: Union[str, int], *, files: Sequence[
discord.File], content: Optional[str] = None, tts: bool = False, embed: Optional[discord.types.embed.Embed] = None, embeds: Optional[List[discord.types.embed.Embed]] = None, nonce: Optional[str] = None, allowed_mentions: Optional[discord.types.message.AllowedMentions] = None, message_reference: Optional[discord.types.message.MessageReference] = None, stickers: Optional[List[discord.types.sticker.StickerItem]] = None, components: Optional[List[Union[discord.types.components.ActionRow, discord.types.components.ButtonComponent, discord.types.components.SelectMenu]]] = None) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async edit_message(channel_id: int, message_id: int, **fields: Any) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async get_message(channel_id: int, message_id: int) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async logs_from(channel_id: int, limit: int, before: Optional[int] = None, after: Optional[int] = None, around: Optional[int] = None) List[Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]]¶
- async ban(user_id: int, guild_id: int, delete_message_days: int = 1, reason: Optional[str] = None) None¶
- async change_my_nickname(guild_id: int, nickname: str, *, reason: Optional[str] = None) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async edit_member(guild_id: int, user_id: int, *, reason: Optional[str] = None, **fields: Any) None¶
- async get_member(guild_id: int, member_id: int) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async edit_role(guild_id: int, role_id: int, *, reason: Optional[str] = None, **fields: Any) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async create_role(guild_id: int, *, reason: Optional[str] = None, **fields: Any) Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]¶
- async move_role_position(guild_id: int, positions: List[Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]], *, reason: Optional[str] = None) None¶
- async delete_channel_permissions(channel_id: int, target_id: int, *, reason: Optional[str] = None) None¶
- get_state() discord.ext.test.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: Optional[List[
discord.Member]] = None, channels: Optional[List[Union[discord.TextChannel,discord.CategoryChannel, discord.abc.GuildChannel, discord.abc.PrivateChannel]]] = None, roles: Optional[List[discord.Role]] = None, owner: bool = False, id_num: int = - 1)discord.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:
discord.Guild, roles: Optional[List[discord.Role]] = None)discord.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:
discord.Guild, id_num: int = - 1, colour: int = 0, color: Optional[int] = None, permissions: int = 104324161, hoist: bool = False, mentionable: bool = False)discord.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:
discord.Role, colour: Optional[int] = None, color: Optional[int] = None, permissions: Optional[int] = None, hoist: Optional[bool] = None, mentionable: Optional[bool] = None, name: Optional[str] = None)discord.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:
discord.Role) None¶ Remove a role from the backend, deleting it from the guild
- Parameters
role – Role to delete
-
make_text_channel(name: str, guild:
discord.Guild, position: int = - 1, id_num: int = - 1, permission_overwrites: Optional[Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]] = None, parent_id: Optional[int] = None)discord.TextChannel¶
-
make_category_channel(name: str, guild:
discord.Guild, position: int = - 1, id_num: int = - 1, permission_overwrites: Optional[Dict[str, Union[str, int, bool, Dict[str, JsonVal], List[JsonVal]]]] = None)discord.CategoryChannel¶
-
delete_channel(channel: Union[
discord.TextChannel,discord.CategoryChannel, discord.abc.GuildChannel, discord.abc.PrivateChannel]) None¶
- update_text_channel(channel: discord.channel.TextChannel, target: typing.Union[discord.user.User, discord.role.Role], override: typing.Optional[discord.permissions.PermissionOverwrite] = <object object>) None¶
-
make_user(username: str, discrim: Union[str, int], avatar: Optional[str] = None, id_num: int = - 1)
discord.User¶
-
make_member(user: Union[discord.user.BaseUser, discord.abc.User], guild:
discord.Guild, nick: Optional[str] = None, roles: Optional[List[discord.Role]] = None)discord.Member¶
-
update_member(member:
discord.Member, nick: Optional[str] = None, roles: Optional[List[discord.Role]] = None)discord.Member¶
-
delete_member(member:
discord.Member) None¶
-
make_message(content: str, author: Union[discord.user.BaseUser, discord.abc.User], channel: Union[
discord.TextChannel,discord.CategoryChannel, discord.abc.GuildChannel, discord.abc.PrivateChannel], tts: bool = False, embeds: Optional[List[discord.Embed]] = None, attachments: Optional[List[discord.Attachment]] = None, nonce: Optional[int] = None, id_num: int = - 1)discord.Message¶
-
find_user_mentions(content: Optional[str], guild: Optional[
discord.Guild]) List[discord.Member]¶
-
find_channel_mentions(content: Optional[str], guild: Optional[
discord.Guild]) List[Union[discord.TextChannel,discord.CategoryChannel, discord.abc.GuildChannel, discord.abc.PrivateChannel]]¶
-
delete_message(message:
discord.Message) None¶
-
make_attachment(filename: pathlib.Path, name: Optional[str] = None, id_num: int = - 1)
discord.Attachment¶
-
add_reaction(message:
discord.Message, user: Union[discord.user.BaseUser, discord.abc.User], emoji: str) None¶
-
remove_reaction(message:
discord.Message, user: discord.user.BaseUser, emoji: str) None¶
-
clear_reactions(message:
discord.Message)¶
-
configure(client:
discord.Client) None¶ -
configure(client: Optional[
discord.Client], *, 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