-
Notifications
You must be signed in to change notification settings - Fork 20
编写WeChatBc插件
并不建议您直接将wechatbc嵌入你的项目,而是建议以docker容器运行,以编写插件的形式扩展您自己的逻辑
写过bukkit插件的朋友应该会很容易上手这套流程,我们的目的是开发一个插件,当接收到消息 "今夕是何年" 的时候回复 "当前的年份"
首先,新建一个java项目,选择maven作为构建工具
注意,为了避免不必要的错误,jdk请选择 java8
目前只能使用本地引入的方式,稍后会将依赖发布至中央仓库
在src下新建一个lib文件夹(当然看你自己习惯),将最新的wechatbc粘贴到里面
此时结构是这样
打开pom.xml,导入依赖
<dependencies>
<dependency>
<groupId>com.meteor</groupId>
<artifactId>wechatbc</artifactId>
<version>1.0.6</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/WeChatBc-1.0.6-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
此时你的pom.xml可能长这样
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.meteor</groupId>
<artifactId>TestPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.meteor</groupId>
<artifactId>wechatbc</artifactId>
<version>1.0.6</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/WeChatBc-1.0.6-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
</project>
点击右上角的小图标,刷新一下
新增一个类 PluginMain
(其实叫什么都好啦) ,这是插件的入口类
public class PluginMain extends BasePlugin {
@Override
public void onLoad() {
}
// 插件启动时
@Override
public void onEnable() {
getLogger().info("插件载入啦");
}
// 插件卸载时
@Override
public void onDisable() {
getLogger().info("插件卸载啦");
}
}
作为插件的主类,必须继承自BasePlugin,这样才会被WeChatBc所识别,getLogger()会取得插件的日志对象,打印一行输出
接下来是最重要的一步,我们必须要让WeChatBc知道它是一个插件,怎么做呢?
wechatbc通过插件资源文件下的 plugin.yml
来识别,在 resources
下新建一个 plugin.yml
# 插件名
name: 'TestPlugin'
# 主类
main: com.meteor.testplugin.PluginMain
# 版本号
version: 1.0
# 作者列表
author:
- 'meteor'
# 描述
description: '这是一个示例插件,展示了怎么编写wechatbc插件'
这里最关键的是 main
,它告诉wechatbc插件的入口类是哪里,这里必须是完整的路径
示例中的 main: com.meteor.testplugin.PluginMain
指向下图
还记得我们要干什么吗?想要让bot根据特定的消息回复内容,我们只需要监听 ReceiveMessageEvent
事件就好了
wechatbc将 接收消息
拍一拍
等封装成了一系列事件,ReceiveMessageEvent
为接收消息时传唤
新增一个类 ReceiveMessageListener
public class ReceiveMessageListener implements Listener {
// 插件主类
private PluginMain pluginMain;
public ReceiveMessageListener(PluginMain pluginMain){
this.pluginMain = pluginMain;
}
@EventHandler
public void onReceiveMessage(ReceiveMessageEvent receiveMessageEvent){
// 获取消息文本
String content = receiveMessageEvent.getContent();
// 消息的完整响应
Message message = receiveMessageEvent.getMessage();
// 取得消息的发送者
String fromUserName = message.getFromUserName();
if("今夕是何年".equalsIgnoreCase(content)){
// 获得客户端对象
WeChatClient weChatClient = pluginMain.getWeChatClient();
// 获得当前年份
Calendar instance = Calendar.getInstance();
int year = instance.get(Calendar.YEAR);
// 回复消息给发送者
weChatClient.getWeChatCore().getHttpAPI().sendMessage(fromUserName,String.valueOf(year));
}
}
}
等等,这一大坨是什么?且听小生慢慢道来
作为监听器类必须要实现 Listener
接口
@EventHandler
注解指定该方法为事件监听
ReceiveMessageEvent
是具体监听的事件
在方法体内编写你的逻辑,如上便实现了我们的需求,当事件触发时,判断消息是否为 "今夕是何年",如果是则回复当前的年份
接下来,把监听器注册到插件上,修改主类的 onEnable
方法
@Override
public void onEnable() {
getLogger().info("插件载入啦");
// 注册监听器
ReceiveMessageListener receiveMessageListener = new ReceiveMessageListener(this);
getWeChatClient().getEventManager().registerPluginListener(this,receiveMessageListener);
}
如此,就大功告成了,此时我们的插件结构长这样
打包为jar,放入wechatbc的plugins文件夹
重启服务