dico_interaction.client module
- class dico_interaction.client.InteractionClient(*, loop: ~typing.Optional[~asyncio.events.AbstractEventLoop] = None, respond_via_endpoint: bool = True, client: ~typing.Optional[~dico.client.Client] = None, auto_register_commands: bool = False, guild_id_lock: ~typing.Optional[~typing.Union[int, str, ~dico.model.snowflake.Snowflake]] = None, guild_ids_lock: ~typing.Optional[~typing.List[~typing.Union[int, str, ~dico.model.snowflake.Snowflake]]] = None, context_cls: ~typing.Type[~dico_interaction.context.InteractionContext] = <class 'dico_interaction.context.InteractionContext'>)
Bases:
objectThis handles all interaction.
Note
auto_register_commandsmust be enabled to properly respond via webserver.Attribute
interactionwill be automatically added to your websocket client if you pass paramclient.
- Parameters:
loop – Asyncio loop instance to use in this client. Default
asyncio.get_event_loop().respond_via_endpoint – Whether to respond via endpoint, which is for gateway response. Otherwise, set to
False. DefaultTrue.client – Optional dico client. Passing this enables automatic command register, wait_interaction, and auto event registration.
auto_register_commands – Whether to automatically register commands. Default
False.guild_id_lock – Guild ID to force-apply to all commands. This is useful for testing commands.
- Variables:
loop – asyncio Loop of the client.
commands – Dict of commands registered to the client.
subcommands – Dict of subcommands registered to the client.
subcommand_groups – Dict of subcommand groups registered to the client.
components – Dict of component callbacks registered to the client.
logger – Logger of the client.
respond_via_endpoint – Whether to automatically register commands.
guild_id_lock – Guild ID that will be force-applied to all commands.
- async register_commands()
Automatically registers command to discord.
- async receive(interaction: InteractionContext) Optional[dict]
Receive and handle interaction.
Note
If
respond_via_endpointis set toFalse, you can get initial response as dict by awaiting.- Parameters:
interaction (
context.InteractionContext) – Interaction received.- Returns:
Optional[dict]
- get_command(interaction: InteractionContext) Optional[InteractionCommand]
Gets command based on interaction received.
- Parameters:
interaction (InteractionContext) – Interaction received.
- Returns:
Optional[InteractionCommand]
- get_autocomplete(interaction: InteractionContext) Optional[AutoComplete]
Gets autocomplete based on interaction received.
- Parameters:
interaction (InteractionContext) – Interaction received.
- Returns:
Optional[AutoComplete]
- async handle_interaction(target: Union[InteractionCommand, ComponentCallback, AutoComplete], interaction: InteractionContext)
Handles received interaction.
- Parameters:
target (Union[InteractionCommand, ComponentCallback, AutoComplete]) – What to execute.
interaction (InteractionContext) – Context to use.
- async execute_error_handler(target: Union[InteractionCommand, ComponentCallback], interaction: InteractionContext, ex: Exception)
Executes error handler. This is intended to be used internally.
- Parameters:
target – Target interaction object.
interaction – Interaction context object.
ex – Exception raised.
- wait_interaction(*, timeout: Optional[float] = None, check: Optional[Callable[[InteractionContext], bool]] = None)
Waits for interaction. Basically same as
dico.Client.waitbut withinteractionevent as default.- Parameters:
timeout – When to timeout. Default
None, which will wait forever.check – Check to apply.
- Returns:
- Raises:
asyncio.TimeoutError – Timed out.
- export_commands() dict
Exports commands of the client as the form below.
{ "global": [...], "guild": { GUILD_ID_1: [...], GUILD_ID_2: [...], ... } }
- Returns:
dict
- add_command(interaction: InteractionCommand)
Adds new interaction command to the client.
- Parameters:
interaction – Command to add.
- remove_command(interaction: InteractionCommand)
Removes command from client.
- Parameters:
interaction – Command to remove.
- add_callback(callback: Union[ComponentCallback, ModalCallback])
Adds component or modal callback to the client.
- Parameters:
callback – Callback to add.
- remove_callback(callback: Union[ComponentCallback, ModalCallback])
Removes callback from client.
- Parameters:
callback – Callback to remove.
- add_autocomplete(autocomplete: AutoComplete)
Adds autocomplete to the client.
- Parameters:
autocomplete – Autocomplete to add.
- remove_autocomplete(autocomplete: AutoComplete)
Removes autocomplete from the client.
- Parameters:
autocomplete – Autocomplete to remove.
- command(name: Optional[str] = None, *, subcommand: Optional[str] = None, subcommand_group: Optional[str] = None, description: Optional[str] = None, subcommand_description: Optional[str] = None, subcommand_group_description: Optional[str] = None, command_type: Union[int, ApplicationCommandTypes] = 1, options: Optional[List[ApplicationCommandOption]] = None, default_permission: bool = True, guild_id: Optional[Union[int, str, Snowflake]] = None, guild_ids: Optional[List[Union[int, str, Snowflake]]] = None, connector: Optional[Dict[str, str]] = None)
Creates and registers interaction command to the client.
Note
You should use
slash()orcontext_menu().Warning
It is not recommended to create subcommand using options, since it won’t be handled properly in the client.
- Parameters:
name – Name of the command.
subcommand – Subcommand of the command.
subcommand_group – Subcommand group of the command.
description – Description of the command.
subcommand_description – Description of subcommand.
subcommand_group_description – Description of subcommand group.
command_type – Type of command.
options – Options of the command.
default_permission – Whether default permission is enabled.
guild_id – ID of the guild. This is deprecated since
0.0.7.guild_ids – List of ID of the guilds.
connector – Option parameter connector.
- slash(name: Optional[str] = None, *, subcommand: Optional[str] = None, subcommand_group: Optional[str] = None, description: str, subcommand_description: Optional[str] = None, subcommand_group_description: Optional[str] = None, options: Optional[List[ApplicationCommandOption]] = None, default_permission: bool = True, guild_id: Optional[Union[int, str, Snowflake]] = None, guild_ids: Optional[List[Union[int, str, Snowflake]]] = None, connector: Optional[Dict[str, str]] = None)
Creates and registers slash command to the client.
Example: .. code-block:: python
@interaction.slash(“example”) async def example_slash(ctx):
…
Connector Example: .. code-block:: python
- {
“옵션”: “option”, “시간”: “hour”
}
Warning
It is not recommended to create subcommand using options, since it won’t be handled properly in the client.
- Parameters:
name – Name of the command.
subcommand – Subcommand of the command.
subcommand_group – Subcommand group of the command.
description – Description of the command.
subcommand_description – Description of subcommand.
subcommand_group_description – Description of subcommand group.
options – Options of the command.
default_permission – Whether default permission is enabled.
guild_id – ID of the guild. This is deprecated since
0.0.7.guild_ids – List of ID of the guilds.
connector – Option parameter connector.
Creates and registers context menu to the client.
- Parameters:
name – Name of the command.
menu_type – Type of the context menu.
guild_id – ID of the guild. This is deprecated since
0.0.7.guild_ids – List of ID of the guilds.
- component_callback(custom_id: Optional[str] = None)
Adds component callback to the client.
- Parameters:
custom_id – Custom ID of the component. Can be prefix of the custom ID.
- autocomplete(*names: str, name: Optional[str] = None, subcommand_group: Optional[str] = None, subcommand: Optional[str] = None, option: Optional[str] = None)
Adds autocomplete to the client. Supports two style:
- Parameters:
name – Name of the command that has autocomplete option.
subcommand_group – Subcommand group of the command.
subcommand – Subcommand of the command.
option – Name of the option with autocomplete enabled.