mirai_core package

Submodules

mirai_core.bot module

mirai_core.bot.retry_once(func)[source]
class mirai_core.bot.Bot(qq: int, host: str = '127.0.0.1', port: int = 8080, auth_key: str = 'abcdefgh', loop=None)[source]

Bases: object

See https://github.com/mamoe/mirai-api-http for details

async handshake()[source]

Authenticate and verify the session_key Automatically called if session_key needs to be updated

async auth() → None[source]

Post auth_key, and get session_key

async verify() → None[source]

Post session_key to verify the session

async release() → None[source]

Post session_key to release the session Needs to be called manually if Updater is not used

async send_message(target: Union[mirai_core.models.Entity.Friend, mirai_core.models.Entity.Member, mirai_core.models.Entity.Group, int], message_type: mirai_core.models.Types.MessageType, message: Union[mirai_core.models.Message.MessageChain, mirai_core.models.Message.BaseMessageComponent, List[mirai_core.models.Message.BaseMessageComponent], str] = '', temp_group: Optional[int] = None, quote_source: Union[int, mirai_core.models.Message.Source] = None) → mirai_core.models.Message.BotMessage[source]

Send Group/Friend message, only keyword arguments are allowed Image ID is available in returned message if uploaded via file path

Parameters
  • target – Group, Member, Friend, int

  • message_type – ChatType, specify the type of target

  • temp_group – If message_type is Member and target is int, then temp group must be specified

  • message – MessageChain, BaseMessageComponent, List of BaseMessageComponent or str, the content to send

  • quote_source – int (the 64-bit int) or Source, the message to quote The purpose of this argument is to save image ids for future use.

Returns

BotMessage (contains message id)

async recall(source: Union[mirai_core.models.Message.Source, int]) → None[source]

Recall a message Success if no exception is raised

Parameters

source – int (the 64-bit int) or Source

property groups

Get list of joined groups

Returns

List of Group

property friends

Get list of friends

Returns

List of Friend

async get_members(target: Union[mirai_core.models.Entity.Group, int]) → List[mirai_core.models.Entity.Member][source]

Get list of members of a group

Parameters

target – int or Group, the target group

Returns

List of Member

async upload_image(message_type: mirai_core.models.Types.MessageType, image_path: Union[pathlib.Path, str]) → Optional[mirai_core.models.Message.Image][source]

Deprecated Upload a image to QQ server. The image between group and friend is not exchangeable This function can be called separately to acquire image uuids, or automatically if using LocalImage while sending

Parameters
  • message_type – MessageType, Friend, Group or Temp

  • image_path – absolute path of the image

Returns

Image object

async fetch_message(count: int) → List[mirai_core.models.Event.BaseEvent][source]

Deprecated Fetch a list of messages This function is called automatically if using polling instead of websocket

Parameters

count – maximum count of one fetch

Returns

List of BaseEvent

async mute_all(group: Union[mirai_core.models.Entity.Group, int]) → None[source]

Mute all non admin members in group Success if no exception is raised

Parameters

group – int or Group, the target group

async unmute_all(group: Union[mirai_core.models.Entity.Group, int]) → None[source]

Unmute all non admin members in group Success if no exception is raised

Parameters

group – int or Group, the target group

async get_member_info(group: Union[mirai_core.models.Entity.Group, int], member: Union[mirai_core.models.Entity.Member, int]) → mirai_core.models.Entity.MemberChangeableSetting[source]

Get the info of a member

Parameters
  • group – int or Group, target group

  • member – int or Member, target member

Returns

MemberChangeableSetting

async get_bot_member_info(group: Union[mirai_core.models.Entity.Group, int]) → mirai_core.models.Entity.MemberChangeableSetting[source]

Get the info of this bot

Parameters

group – int or Group, target group

Returns

MemberChangeableSetting

async set_member_info(group: Union[mirai_core.models.Entity.Group, int], member: Union[mirai_core.models.Entity.Member, int], setting: mirai_core.models.Entity.MemberChangeableSetting) → None[source]

Set the info of a member Success if no exception is raised

Parameters
  • group – int or Group, target group

  • member – int or Member, target member

  • setting – MemberChangeableSetting, the new settings

async get_group_config(group: Union[mirai_core.models.Entity.Group, int]) → mirai_core.models.Entity.GroupSetting[source]

Get the group config of a group

Parameters

group – int or Group, target group

Returns

GroupSetting

async set_group_config(group: Union[mirai_core.models.Entity.Group, int], config: mirai_core.models.Entity.GroupSetting) → None[source]

Set the group config of a group Success if no exception is raised

Parameters
  • group – int or Group, target group

  • config – GroupSetting

async mute(group: Union[mirai_core.models.Entity.Group, int], member: Union[mirai_core.models.Entity.Member, int], time: Union[datetime.timedelta, int]) → None[source]

Mute a member of a group Success if no exception is raised

Parameters
  • group – int or Group, target group

  • member – int or Member, target member

  • time – int or datetime.timedelta, must between 1 minutes and 30 days

async unmute(group: Union[mirai_core.models.Entity.Group, int], member: Union[mirai_core.models.Entity.Member, int]) → None[source]

Unmute a member of a group Success if no exception is raised

Parameters
  • group – int or Group, target group

  • member – int or Member, target member

async kick(group: Union[mirai_core.models.Entity.Group, int], member: Union[mirai_core.models.Entity.Member, int], message: str = '') → None[source]

Kick a member of a group

Parameters
  • group – int or Group, target group

  • member – int or Member, target member

  • message – string, message to the member

async quit(group: Union[mirai_core.models.Entity.Group, int])[source]

Quit a group

Parameters

group – int or Group, target group

async respond_request(request: Union[mirai_core.models.Event.NewFriendRequestEvent, mirai_core.models.Event.MemberJoinRequestEvent], response: Union[mirai_core.models.Types.NewFriendRequestResponse, mirai_core.models.Types.MemberJoinRequestResponse], message: str = '')[source]

Respond NewFriendRequestEvent and MemberJoinRequestEvent

Parameters
  • request – NewFriendRequestEvent or MemberJoinRequestEvent

  • response – NewFriendRequestResponse or MemberJoinRequestResponse

  • message – text message for the response

async get_config() → dict[source]

Get the config of http api

Returns

config

async set_config(cache_size: int = 4096, enable_websocket: bool = True) → None[source]

Set the config of http api Success if no exception is raised

Parameters
  • cache_size – int, the size of message cache

  • enable_websocket – bool, whether to enable websocket (will disable fetch_message accordingly)

async create_websocket(handler, ws_close_handler=None, listen: str = 'all') → None[source]

Register callback for websocket. Once an BaseEvent or Message is received, the handler will be invoked

Parameters
  • handler – callable

  • ws_close_handler – callable, websocket shutdown hook

  • listen – ‘all’, ‘event’ or ‘message’

mirai_core.exceptions module

exception mirai_core.exceptions.MiraiException[source]

Bases: Exception

Generic exception

exception mirai_core.exceptions.NetworkException[source]

Bases: mirai_core.exceptions.MiraiException

Exceptions such as not able to reach mirai, possibly due to service not started or incorrect ip/port

exception mirai_core.exceptions.AuthenticationException[source]

Bases: mirai_core.exceptions.MiraiException

Exceptions such as incorrect auth_key

exception mirai_core.exceptions.SessionException[source]

Bases: mirai_core.exceptions.MiraiException

Exceptions such as incorrect session_key

exception mirai_core.exceptions.PrivilegeException[source]

Bases: mirai_core.exceptions.MiraiException

You do not have permission to perform such action (such as mute, kick, etc.)

exception mirai_core.exceptions.UnknownTargetException[source]

Bases: mirai_core.exceptions.MiraiException

Target not found (sending to non-existing group, friend, etc.)

exception mirai_core.exceptions.BadRequestException[source]

Bases: mirai_core.exceptions.MiraiException

Request contains invalid parameter

exception mirai_core.exceptions.ServerException[source]

Bases: mirai_core.exceptions.MiraiException

Server is returning non 200 return code, see console log for more details This is more likely to be mirai side issue, but sometimes incorrect parameters can cause this problem too

mirai_core.log module

mirai_core.log.create_logger(module: str) → logging.Logger[source]

create logger for module

mirai_core.log.install_logger()[source]

mirai_core.network module

class mirai_core.network.HttpClient(base_url: str, timeout=5, loop=None)[source]

Bases: object

Internal use only HttpClient implemented by aiohttp

DEFAULT_TIMEOUT = 5
async get(url, headers=None, params=None)[source]

send http get request

Parameters
  • url – the sub url

  • headers – request headers

  • params – get params

Returns

json decoded response

async post(url, headers=None, data=None)[source]

send http post request

Parameters
  • url – the sub url

  • headers – request headers

  • data – post params

Returns

json decoded response

async upload(url, headers=None, data=None, file: str = None)[source]

upload using multipart upload

Parameters
  • url – the sub url

  • headers – request headers

  • data – post params

  • file – file to attach

Returns

json decoded response

async websocket(url: str, handler: callable, ws_close_handler: callable)[source]

Create websocket subscriber to url

Parameters
  • url – the sub url

  • handler – request headers

  • ws_close_handler – callback for connection close

async close()[source]

Close session

mirai_core.updater module

class mirai_core.updater.Updater(bot: mirai_core.bot.Bot, use_websocket: bool = True)[source]

Bases: object

Initialize Updater

Parameters
  • bot – the Bot object to use

  • use_websocket – bool. whether websocket (recommended) should be used

async run_task(shutdown_hook: callable = None)[source]

return awaitable coroutine to run in event loop (must be the same loop as bot object)

Parameters

shutdown_hook – callable, if running in main thread, this must be set. Trigger is called on shutdown

add_handler(event: Union[mirai_core.models.Event.Message, mirai_core.models.Event.BotOnlineEvent, mirai_core.models.Event.BotOfflineEventActive, mirai_core.models.Event.BotOfflineEventForce, mirai_core.models.Event.BotOfflineEventDropped, mirai_core.models.Event.BotReloginEvent, mirai_core.models.Event.BotGroupPermissionChangeEvent, mirai_core.models.Event.BotMuteEvent, mirai_core.models.Event.BotUnmuteEvent, mirai_core.models.Event.BotJoinGroupEvent, mirai_core.models.Event.BotLeaveEventActive, mirai_core.models.Event.BotLeaveEventKick, mirai_core.models.Event.GroupNameChangeEvent, mirai_core.models.Event.GroupEntranceAnnouncementChangeEvent, mirai_core.models.Event.GroupMuteAllEvent, mirai_core.models.Event.GroupRecallEvent, mirai_core.models.Event.FriendRecallEvent, mirai_core.models.Event.GroupAllowAnonymousChatEvent, mirai_core.models.Event.GroupAllowConfessTalkEvent, mirai_core.models.Event.GroupAllowMemberInviteEvent, mirai_core.models.Event.MemberJoinEvent, mirai_core.models.Event.MemberLeaveEventKick, mirai_core.models.Event.MemberLeaveEventQuit, mirai_core.models.Event.MemberCardChangeEvent, mirai_core.models.Event.MemberSpecialTitleChangeEvent, mirai_core.models.Event.MemberPermissionChangeEvent, mirai_core.models.Event.MemberMuteEvent, mirai_core.models.Event.MemberUnmuteEvent, mirai_core.models.Event.NewFriendRequestEvent, mirai_core.models.Event.MemberJoinRequestEvent, mirai_core.models.Event.BaseEvent, List[Union[mirai_core.models.Event.Message, mirai_core.models.Event.BotOnlineEvent, mirai_core.models.Event.BotOfflineEventActive, mirai_core.models.Event.BotOfflineEventForce, mirai_core.models.Event.BotOfflineEventDropped, mirai_core.models.Event.BotReloginEvent, mirai_core.models.Event.BotGroupPermissionChangeEvent, mirai_core.models.Event.BotMuteEvent, mirai_core.models.Event.BotUnmuteEvent, mirai_core.models.Event.BotJoinGroupEvent, mirai_core.models.Event.BotLeaveEventActive, mirai_core.models.Event.BotLeaveEventKick, mirai_core.models.Event.GroupNameChangeEvent, mirai_core.models.Event.GroupEntranceAnnouncementChangeEvent, mirai_core.models.Event.GroupMuteAllEvent, mirai_core.models.Event.GroupRecallEvent, mirai_core.models.Event.FriendRecallEvent, mirai_core.models.Event.GroupAllowAnonymousChatEvent, mirai_core.models.Event.GroupAllowConfessTalkEvent, mirai_core.models.Event.GroupAllowMemberInviteEvent, mirai_core.models.Event.MemberJoinEvent, mirai_core.models.Event.MemberLeaveEventKick, mirai_core.models.Event.MemberLeaveEventQuit, mirai_core.models.Event.MemberCardChangeEvent, mirai_core.models.Event.MemberSpecialTitleChangeEvent, mirai_core.models.Event.MemberPermissionChangeEvent, mirai_core.models.Event.MemberMuteEvent, mirai_core.models.Event.MemberUnmuteEvent, mirai_core.models.Event.NewFriendRequestEvent, mirai_core.models.Event.MemberJoinRequestEvent, mirai_core.models.Event.BaseEvent]]])[source]

Decorator for event listeners Catch all is not supported at this time

Parameters

event – events.Events

run(log_to_stderr=True) → None[source]

Start the Updater and block the thread

Parameters

log_to_stderr – if you are setting other loggers that capture the log from this Library, set to False

async handshake()[source]

Internal use only, automatic handshake Called when launch or websocket disconnects

Returns

async message_polling(count=5, interval=0.5) → None[source]

Internal use only, polling message and fire events

Parameters
  • count – maximum message count for each polling

  • interval – minimum interval between two polling

async event_caller(event: mirai_core.models.Event.BaseEvent) → None[source]

Internal use only, call the event handlers sequentially

Parameters

event – the event

async raise_shutdown(shutdown_event: Callable[[...], Awaitable[None]]) → None[source]

Internal use only, shutdown

Parameters

shutdown_event – callable

handle_exception(loop, context)[source]
class mirai_core.updater.EventHandler(func: Callable)[source]

Bases: object

Contains the callback function

func: Callable = None
exception mirai_core.updater.Shutdown[source]

Bases: Exception

Internal use only Shutdown BaseEvent

Module contents