Skip to main content

Myrai

build

用 Python (>=3.9) 写你熟悉的 mirai-core!(WIP)

使用了 py4j。自带一个接口的 jar 文件,并且会自己启动。可以设置使用其它 jar 文件或不从 Python 端启动。

简介

为了用 Python 连接 mirai-core,现在需要经过 mirai-api-http。通用方法是使用 mirai-console-loader 进行启动,这样太麻烦(需要额外的软件与插件)。为了节省这些麻烦,直接通过 py4j 利用 socket 实现了 Python 与 mirai-core 的交互,节省了中间使用 HTTP 的麻烦。

至于为什么是 3.9 版本或以上……因为 3.9 好!3.8 没有什么很特别的更新,而 3.7 的支持结束时间是 2023/6/27。

使用方法

前提条件

  • Python 3.9+
  • Java 11+

安装

pip install myrai

初始化

import myrai

myrai.init()
# ========
bot = myrai.start_bot(114514, "password")  # qq 和 password
# -------- 或者
import hashlib
bot = myrai.start_bot(
    114514,
    hashlib.md5("password".encode("utf-8")).digest()  # 用 MD5
)
# ========
bot.login()

初始化完成后

使用方式与 mirai-core 一模一样。在 mirai_types 中提供了所有(WIP)在 mirai-core 中有的类。在 java 中提供了一部分会经常使用的 Java 类。

这些类大多用作代码提示,因为运行时它们的类型都不一样,都是 py4j 的 JavaClass,JavaObject 等。

使用 .new 可以创建它的 Java 对象,使用 .static 能获取到它的类对象。所以想要调用静态方法和其它的方法可以这样:

def listener(e: MessageEvent):
    e.getSender().sendMessage(
        MessageUtils.static.newChain(
            [
                QuoteReply.new(e.getMessage()),
                PlainText.new("hi"),
            ]
        )
    )

注意:有些 Java 静态方法 / 成员为了方便使用,可以在 Python 类上直接调用静态方法 / 变量 (实际使用了 classmethod 和 property)。这些方法会有代码提示。没有代码提示的静态方法请使用 .static。如果你没有代码提示或不放心一直报错,那就使用 .static 调用静态成员。例如:

MessageUtils.newChain(...)  # OK
MessageUtils.static.newChain(...)  # OK
MessageUtils.static.buildMessageChain(...)  # OK

MessageUtils.buildMessageChain(...)  # 不OK

由于这个新特性(将 @classmethod 和 @property 用在一起)的不稳定性,Python 可能在后续版本中废弃这个写法(见 issue)。所以 myrai 的后续版本中也会修改这个用法并降低可使用的 Python 版本。

收尾

myrai.close()

建议使用 atexit。

import atexit
import myrai

atexit.register(myrai.close)

Metadata

Release files for myrai 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for myrai 0.1.0
File Size Uploaded
myrai-0.1.0.tar.gz 33.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for myrai 0.1.0
File Interpreter ABI Platform
myrai-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 67.2 MB

Release files / myrai-0.1.0.tar.gz

Download URL myrai-0.1.0.tar.gz
Size 33.6 MB
Tags Source
SHA-256 checksum
How to use checksums
3a6ea73aabebc050e74a552e336783b9e58eb9a1bb0d951dd9fac5072b5d124e
BLAKE2b-256 checksum
How to use checksums
d13ca311a59f86b040f3c28987dee26a10e98347847eb549d2a6076294ada31d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.7

Release files / myrai-0.1.0-py3-none-any.whl

Download URL myrai-0.1.0-py3-none-any.whl
Size 33.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
65b55e45a0def55133c9289fb84dc87a4d5c16858484bcff57288e44d56c55cd
BLAKE2b-256 checksum
How to use checksums
17277d8c90deb40d483cdaed42fd919c5478093772fe7cca8ac7a1cdb5568470
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page