iamai.bot module

Bot Module

The basic module of iamai, each iamai robot is a Bot() instance.

class iamai.bot.Bot(*, config_file: str | None = 'config.toml', config_dict: Dict[str, Any] | None = None, hot_reload: bool = False)[source]

Bases: object

iamai Bot class object, defines the basic functionality for interacting.

Read and save configuration Config, load adapters Adapter and plugins Plugin, then distribute the events.

config

Bot configuration.

Type:

iamai.config.MainConfig

should_exit

Whether the bot should enter the ready-to-exit state.

Type:

asyncio.locks.Event

adapters

List of currently loaded adapters.

Type:

List[iamai.adapter.Adapter[Any, Any]]

plugins_priority_dict

Plugin priority dictionary.

Type:

Dict[int, List[Type[iamai.plugin.Plugin[Any, Any, Any]]]]

plugin_state

Plugin status.

Type:

Dict[str, Any]

global_state

Global status.

Type:

Dict[Any, Any]

adapter_run_hook(func: Callable[[Adapter[Any, Any]], Awaitable[None]]) Callable[[Adapter[Any, Any]], Awaitable[None]][source]

Register an adapter runtime function.

Parameters:

func – the registered function.

Returns:

The registered function.

adapter_shutdown_hook(func: Callable[[Adapter[Any, Any]], Awaitable[None]]) Callable[[Adapter[Any, Any]], Awaitable[None]][source]

Register a function when the adapter is closed.

Parameters:

func – the registered function.

Returns:

The registered function.

adapter_startup_hook(func: Callable[[Adapter[Any, Any]], Awaitable[None]]) Callable[[Adapter[Any, Any]], Awaitable[None]][source]

Register a function during adapter initialization.

Parameters:

func – the registered function.

Returns:

The registered function.

adapters: List[Adapter[Any, Any]]
bot_exit_hook(func: Callable[[Bot], Awaitable[None]]) Callable[[Bot], Awaitable[None]][source]

Register a function when the Bot exits.

Parameters:

func – the registered function.

Returns:

The registered function.

bot_run_hook(func: Callable[[Bot], Awaitable[None]]) Callable[[Bot], Awaitable[None]][source]

Register a function when Bot starts.

Parameters:

func – the registered function.

Returns:

The registered function.

config: MainConfig
error_or_exception(message: str, exception: Exception) None[source]

Output error or exception logs based on the current Bot configuration.

Parameters:
  • message – message.

  • exception – Exception.

event_postprocessor_hook(func: Callable[[Event[Any]], Awaitable[None]]) Callable[[Event[Any]], Awaitable[None]][source]

Register a post-event processing function.

Parameters:

func – the registered function.

Returns:

The registered function.

event_preprocessor_hook(func: Callable[[Event[Any]], Awaitable[None]]) Callable[[Event[Any]], Awaitable[None]][source]

Register an event preprocessing function.

Parameters:

func – the registered function.

Returns:

The registered function.

async get(func: Callable[[Event[Any]], bool | Awaitable[bool]] | None = None, *, event_type: None = None, adapter_type: None = None, max_try_times: int | None = None, timeout: int | float | None = None) Event[Any][source]
async get(func: Callable[[EventT], bool | Awaitable[bool]] | None = None, *, event_type: None = None, adapter_type: Type[Adapter[EventT, Any]], max_try_times: int | None = None, timeout: int | float | None = None) EventT
async get(func: Callable[[EventT], bool | Awaitable[bool]] | None = None, *, event_type: Type[EventT], adapter_type: Type[AdapterT] | None = None, max_try_times: int | None = None, timeout: int | float | None = None) EventT

Get events that meet the specified conditions. The coroutine will wait until the adapter receives events that meet the conditions, exceeds the maximum number of events, or times out.

Parameters:
  • func – Coroutine or function, the function will be automatically packaged as a coroutine for execution. Requires an event to be accepted as a parameter and returns a Boolean value. Returns the current event when the coroutine returns True. When None is equivalent to the input coroutine returning true for any event, that is, returning the next event received by the adapter.

  • event_type – When specified, only events of the specified type are accepted, taking effect before the func condition. Defaults to None.

  • adapter_type – When specified, only events generated by the specified adapter will be accepted, taking effect before the func condition. Defaults to None.

  • max_try_times – Maximum number of events.

  • timeout – timeout period.

Returns:

Returns events that satisfy the condition of func.

Raises:

GetEventTimeout – Maximum number of events exceeded or timeout.

get_adapter(adapter: str) Adapter[Any, Any][source]
get_adapter(adapter: Type[AdapterT]) AdapterT

Get the loaded adapter by name or adapter class.

Parameters:

adapter – adapter name or adapter class.

Returns:

The obtained adapter object.

Raises:

LookupError – No adapter object with this name found.

get_plugin(name: str) Type[Plugin[Any, Any, Any]][source]

Get the loaded plugin class by name.

Parameters:

name – plugin name

Returns:

The obtained plug-in class.

Raises:

LookupError – The plugin class with this name cannot be found.

global_state: Dict[Any, Any]
async handle_event(current_event: Event[Any], *, handle_get: bool = True, show_log: bool = True) None[source]

Called by the adapter object, distributes events to all plug-ins according to priority, and handles plug-in signals such as stop and skip.

This method should not be called manually by the user.

Parameters:
  • current_event – The currently pending Event.

  • handle_get – Whether the current event can be captured by the get method, the default is True.

  • show_log – Whether to display in the log, the default is True.

load_adapters(*adapters: Type[Adapter[Any, Any]] | str) None[source]

Load adapter.

Parameters:

*adapters

Adapter class or adapter name, type can be Type[Adapter] or str. If it is of type Type[Adapter], it will be loaded as an adapter class. If it is of type str, it will be loaded as the adapter module name, and the format is the same as the Python import statement.

For example: path.of.adapter.

load_plugins(*plugins: Type[Plugin[Any, Any, Any]] | str | Path) None[source]

Load the plugin.

Parameters:

*plugins

Plugin class, plugin module name or plug-in module file path. Type can be Type[Plugin], str or pathlib.Path. If it is Type[Plugin], it will be loaded as a plug-in class. If it is of type str, it will be loaded as the plug-in module name, and the format is the same as the Python import statement.

For example: path.of.plugin.

If it is of type pathlib.Path, it will be loaded as the plug-in module file path.

For example: pathlib.Path("path/of/plugin").

load_plugins_from_dirs(*dirs: Path) None[source]

Load plug-ins from the directory. Plug-ins in modules starting with _ will not be imported. The path can be a relative path or an absolute path.

Parameters:

*dirs – Module paths that store modules containing plugins. For example: pathlib.Path("path/of/plugins/") .

property locale: List[str]

Get the current language of the bot.

property locale_dirs: List[Path]

Get the locale directories of the bot.

plugin_state: Dict[str, Any]
property plugins: List[Type[Plugin[Any, Any, Any]]]

List of currently loaded plugins.

plugins_priority_dict: Dict[int, List[Type[Plugin[Any, Any, Any]]]]
reload_plugins() None[source]

Manually reload all plugins.

restart() None[source]

Exit and rerun iamai.

run() None[source]

Run iamai, monitor and intercept system exit signals, and update robot configuration.

should_exit: Event