Skip to content

Building a QQ Automation Bot with Lua and Mirai

July 25, 2021

GitHub

https://github.com/only52607/lua-mirai

Lua Mirai Documentation

https://ooooonly.gitee.io/lua-mirai-doc/

Mirai Official Documentation

https://docs.mirai.mamoe.net/ Make sure to read the documentation before starting.

Running as a MiraiConsoleLoader Plugin

Here we run Lua Mirai as an MCL plugin.

  1. Compile or download the latest version of mirai console loader
  2. Configure the mirai console loader environment and place the jar file into the plugin directory.

Directory Structure

├─bots │  └─QQ_number ├─cache ├─config │  ├─Console │  └─lua-mirai ├─data ├─libs ├─plugins------plugin directory └─scripts  config.json  mcl------Linux execution  mcl.cmd------Windows execution  mcl.jar

Basic Commands

All commands must be entered in the console. /lua list — List all loaded scripts (including executed and unexecuted). /lua add <script path> — Load a script without executing. /lua load <script path> — Load and execute a script. /lua execute <script number> — Execute an already loaded script (skipped if already executed). /lua reload <script number> — Re-execute a script (stops the script, re-reads the file, and executes again). /lua stop <script number> — Stop a script (stops all event listeners within the script). /lua delete <script number> — Delete a script (removes from the list only, does not delete the file).

Editing Lua Scripts

Create ai.lua in the plugins directory with the following content:

Event.subscribe("BotOnlineEvent",function(event)
    local bot = event.bot
    bot:getFriend(QQ_NUMBER_TO_SEND):sendMessage("MESSAGE_TO_SEND")
end)

Make sure to use UTF-8 encoding, otherwise Chinese messages will appear garbled.

Running

Double-click mcl.cmd to run. After starting, use the auto-login command to add the QQ account and specify the ANDROID_PAD protocol.

/AutoLogin add QQ_NUMBER QQ_PASSWORD
autoLogin setConfig QQ_NUMBER protocol ANDROID_PHONE

Or edit /config/Console/AutoLogin.yml:

accounts:
  - # Account, currently only supports QQ numeric accounts
    account: QQ_NUMBER
    password: QQ_PASSWORD
      # Password type: PLAIN or MD5
      kind: PLAIN
      # Password content: plain text for PLAIN, 16-byte hex for MD5
      value:

    # Account configuration, available options (case-sensitive), recommend "ANDROID_PAD" to avoid kicking out mobile QQ
    # "protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH"
    # "device": "device.json"
    configuration:
      protocol: ANDROID_PHONE
      device: device.json

Add the plugin:

/lua load plugins\ai.lua

Or edit /config/lua-mirai/scripts.json:

[
    {
        "type": "file",
        "file": "plugins\ai.lua",
        "enable": true
    }
]

After completion, simply run mcl.cmd to auto-login and auto-send messages.

Scheduled Sending

To send messages on a schedule, use Windows Task Scheduler to run mcl.cmd at scheduled times. After each run, you’ll also need to schedule a task to terminate mcl.cmd, otherwise it cannot launch again.

Previous
Research on Classroom Management Software
Next
Scraping Bilibili Live Chat with Python