dinglz 发表于 2022-2-13 20:47:27

QQ机器人开发 Mirai Java栈

QQ机器人开发,基于 Mirai

本文会介绍两种机器人开发方式,一种是基于 Mirai Console启动器系列的插件
另一种是将 Mirai-Core 嵌入到程序中,这样就可以驱动一个机器人

static/image/hrline/2.gif

方式一, Mirai插件开发

为了加载我们的插件,使用官方的 Mirai Console启动器 MCL
首先将它下载下来。

https://github.com/iTXTech/mirai-console-loader/releases

启动mcl.cmd。可以看到自动下载了一些核心lib。

创建了一些目录,我们开发的插件存放的目录就在plugin目录

打开IDEA,我们本文使用它来开发Mirai插件。

首先,安装idea的Mirai 开发插件



然后,新建项目,选择Mirai





等待IDE完成对项目的自动配置



我们来编写一个测试的插件



代码如下:
public void onEnable() {
      getLogger().info("Plugin 测试插件 loaded!"); //调用日志组件
      GlobalEventChannel.INSTANCE.subscribeAlways(FriendMessageEvent.class,event->{
            //注册了一个FriendMessage事件的监听器 好友消息事件
            MessageChain message = event.getMessage(); //取出传入的收到的消息
            if(message.contentToString().equals("hello"))
            {
                //如果消息是hello,那么就发送消息:插件运行正常
                event.getSubject().sendMessage("插件运行正常!");
            }
      });
      GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class,event->{
            //注册了一个GroupMessage事件的监听器 群消息事件
            MessageChain message = event.getMessage(); //同上
            if(message.contentToString().equals("hello"))
            {
                //标记消息为回复消息,回复收到的这条消息,即:hello
                event.getSubject().sendMessage(new QuoteReply(message).plus("插件运行正常"));
            }
      });
      GlobalEventChannel.INSTANCE.subscribeAlways(BotOnlineEvent.class,event->{
            //event.getBot().getNick()获取机器人昵称
         getLogger().info("机器人"+event.getBot().getNick()+"登录成功!");
      });
    }

编译出插件


插件在目录\build\mirai下

复制到plugin目录

启动mcl



使用指令login 账号 密码    来登录一个机器人实例



下面是正常工作状态的截图





群消息测试,私聊消息测试均成功。

更多开发文档:https://docs.mirai.mamoe.net/CoreAPI.html


static/image/hrline/2.gif

方法二,内嵌

首先新建一个maven项目,并在src目录创建main函数入口



引入Mirai-core依赖

<dependencies>
      <dependency>
            <groupId>net.mamoe</groupId>
            <artifactId>mirai-core-jvm</artifactId>
            <version>2.10.0</version>
      </dependency>
    </dependencies>

开始写代码



具体代码如下

import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.event.events.FriendMessageEvent;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.utils.BotConfiguration;

public class Main {
    public static void main(String[] args) {
      Bot bot = BotFactory.INSTANCE.newBot(3582683469L,"dbb76820618",new BotConfiguration(){{
            fileBasedDeviceInfo(); //创建device.json保存登录设备信息
            setProtocol(MiraiProtocol.ANDROID_PHONE); //使用安卓协议
      }});
      //创建一个新的机器人
      bot.login(); //登录
      bot.getLogger().info("登录成功!"); //调用内置日志组件
      bot.getEventChannel().subscribeAlways(FriendMessageEvent.class,event->{
         //同方式一,注册好友消息事件
            MessageChain message = event.getMessage();
            if(message.contentToString().equals("hello"))
            {
                event.getSubject().sendMessage("机器人运行正常");
            }
      });
      //这里只写了好友消息事件,具体同方式一一样
    }
}


运行



登录成功。

发送好友消息测试一下



ok,一切正常。

通过方式二,就可以不再依赖Mirai Console。内嵌进自己的java程序中。


static/image/hrline/2.gif


Mirai官方地址:https://github.com/mamoe/mirai

公开群机器人实现方式:Go语言链接go-cqhttp(修改版 by me)
感兴趣的可以了解一下
go-cqhttp: https://github.com/Mrs4s/go-cqhttp

dinglz 发表于 2022-2-13 20:52:52

顺便一提,我是与spring boot一起用的,这样就可以在web上配置,但是spring boot内置的Kotlin 版本是与mirai冲突的,解决方法如下

mirai 使用的 Kotlin 1.5 可能与你的项目使用的其他库依赖的 Kotlin 版本冲突,Maven 有时候无法正确处理这种冲突。此时请手动添加 Kotlin 标准库依赖。

<properties>
    <kotlin.version>1.5.10</kotlin.version>
</properties>


<dependencies>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib-jdk8</artifactId>
      <version>${kotlin.version}</version>
    </dependency>
</dependencies>

dinglz 发表于 2022-2-14 16:49:13

MiraiManager,欢迎围观
机器人统一管理登录。SpringBoot+Mirai-Core
https://gitee.com/dinglz/miria-manager
https://dinglz.gitee.io/miria-manager

竹羽盗一 发表于 2022-2-18 09:34:44

本帖和源码已围观

Angels〃欧豪 发表于 2022-2-25 09:51:49

源码已围观,谢谢楼主分享
页: [1]
查看完整版本: QQ机器人开发 Mirai Java栈