Skip to content

Commit

Permalink
[5.3] 抽象化配置项模型, 调整依赖关系, 支持gocq-http框架
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Dec 31, 2022
1 parent f131cef commit 2b7c347
Show file tree
Hide file tree
Showing 35 changed files with 1,307 additions and 481 deletions.
47 changes: 38 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@

**[在线编辑器](https://dituon.github.io/petpet/editor)**

# [Mirai](https://github.com/mamoe/mirai)插件

## 使用方法

### [Mirai](https://github.com/mamoe/mirai)插件

0. 部署 [Mirai](https://github.com/mamoe/mirai) 机器人框架

1. 下载 [最新版本](https://github.com/Dituon/petpet/releases/)

2. 将插件放入 `Mirai/plugins/`

3. 下载 [图片素材](https://github.com/Dituon/petpet/tree/main/data/xmmt.dituon.petpet)

4. 将图片素材放入 `Mirai/data/xmmt.dituon.petpet`
4. 将图片素材放入 `Mirai/data/xmmt.dituon.petpet/`

5. 使用 **戳一戳**`30%` 的概率触发; 或发送 `pet @xxx`
5. 启动 `Mirai`, 可自行更改配置文件 `Petpet.yml`, 重启后生效 (参考 [配置项说明](#配置项说明))

- 使用 **戳一戳**`30%` 的概率触发; 或发送 `pet @xxx`

> `pet key @xxx``key @xxx` 可返回指定图片 例如 `pet kiss @xxx` `kiss @xxx`
Expand All @@ -37,12 +41,37 @@
> 可使用 `pet`指令 获取 `keyList`
**以上配置 均可在配置文件中修改**
### 单独运行

## 配置文件
1. 下载 [最新版本](https://github.com/Dituon/petpet/releases/) `petpet.jar``petpet-no-ws.jar`

首次运行 Petpet 插件时,会生成 `Mirai/config/xmmt.dituon.petpet/Petpet.yml` 文件
2. 下载 [图片素材](https://github.com/Dituon/petpet/tree/main/data/xmmt.dituon.petpet)

3. 将图片素材放入 `./data/xmmt.dituon.petpet/` 目录

4. `cd ./` `java -jar petpet.jar`, 可自行更改配置文件 `config.json`, 重启后生效

5. 参考[`WebServer`](#WebServer)一节 发起网络请求

### [gocq-http](https://github.com/Mrs4s/go-cqhttp)插件

> **Warning**
>
> 此功能处于测试阶段, 目前仅能通过`key`生成图片, 请期待后续开发!
0. 部署 [gocq-http](https://github.com/Mrs4s/go-cqhttp) 机器人框架, 设置**正向 WebSocket** 监听 (默认端口为`8080`)

1. 更改 `gocq-http` 配置项 `message.post-format``array`

2. 下载 [最新版本](https://github.com/Dituon/petpet/releases/) `petpet.jar`

3. 下载 [图片素材](https://github.com/Dituon/petpet/tree/main/data/xmmt.dituon.petpet)

4. 将图片素材放入 `./data/xmmt.dituon.petpet/` 目录

5. `cd ./` `java -jar petpet.jar`, 可自行更改配置文件 `gocqConfig.json`, 重启后生效

## 配置文件
#### 配置项说明

<details>
Expand Down Expand Up @@ -598,9 +627,9 @@
# WebServer

除了作为`Mirai`插件, `Petpet` 也可以作为**http服务器 / API**单独运行, 可被其它项目/语言使用
`Petpet` 可以作为**http服务器 / API**单独运行, 被其它项目/语言使用

`java -jar petpet.jar`
> `java -jar petpet.jar`
启动时会生成 `config.json`:
```
Expand Down
27 changes: 23 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
id 'org.jetbrains.kotlin.jvm' version '1.6.20'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.20'

id 'net.mamoe.mirai-console' version '2.11.0'
}
Expand All @@ -10,7 +10,7 @@ apply plugin: "java"


group = 'xmmt.dituon'
version = '5.2'
version = '5.3'

repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
Expand All @@ -20,9 +20,13 @@ repositories {
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.4'
annotationProcessor 'org.projectlombok:lombok:1.18.4'

implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3"
implementation "com.madgag:animated-gif-lib:1.4"
implementation "com.squareup:gifencoder:0.10.1"

implementation "org.java-websocket:Java-WebSocket:1.5.3"

testImplementation "junit:junit:4.11"
}

Expand Down Expand Up @@ -56,7 +60,7 @@ publishing {
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
manifest {
attributes 'Main-Class': 'moe.dituon.petpet.server.WebServer'
attributes 'Main-Class': 'moe.dituon.petpet.Main'
}
from {
(configurations.runtimeClasspath).collect {
Expand All @@ -65,6 +69,21 @@ jar {
}
}

task wsJar(type: Jar) {
group = 'build'
description = 'build jar, excluding the websocket lib'
archiveClassifier.set("no-ws")

exclude '**/petpet/websocket/**'
exclude '**/org/java_websocket/**'

with jar
}

mirai {
excludeDependency("org.java-websocket", "Java-WebSocket")
}

configurations {
compileOnly {
extendsFrom annotationProcessor
Expand Down
55 changes: 0 additions & 55 deletions config/xmmt.dituon.petpet/PetPet.yml

This file was deleted.

Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
25 changes: 25 additions & 0 deletions src/main/java/moe/dituon/petpet/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package moe.dituon.petpet;

import moe.dituon.petpet.server.WebServer;
import moe.dituon.petpet.websocket.gocq.GoCQPetpet;

import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
if (args.length == 0) {
WebServer server = new WebServer();
return;
}
List<String> param = Arrays.asList(args);
if (param.contains("-gocq")) {
try {
GoCQPetpet goCQPetpet = GoCQPetpet.getInstance();
} catch (NoClassDefFoundError e) {
e.printStackTrace();
System.err.println("无法加载WebSocket依赖, 请使用分发版本");
}
}
}
}
160 changes: 160 additions & 0 deletions src/main/java/moe/dituon/petpet/mirai/MiraiPetService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package moe.dituon.petpet.mirai;

import moe.dituon.petpet.plugin.*;
import moe.dituon.petpet.share.BaseConfigFactory;
import moe.dituon.petpet.share.GifAvatarExtraDataProvider;
import moe.dituon.petpet.share.TextExtraData;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.Member;
import net.mamoe.mirai.contact.NormalMember;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.utils.ExternalResource;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public class MiraiPetService extends PluginPetService{

public byte probability = 30;
public boolean respondSelfNudge = false;
public int coolDown = Cooler.DEFAULT_USER_COOLDOWN;
public int groupCoolDown = Cooler.DEFAULT_GROUP_COOLDOWN;
public String inCoolDownMessage = Cooler.DEFAULT_MESSAGE;
public boolean inCoolDownNudge = false;
public boolean devMode = false;
public boolean messageHook = false;
public boolean nudgeCanBeDisabled = true;
public boolean messageCanBeDisabled = false;
public boolean autoUpdate = true;
public String repositoryUrl = DataUpdater.DEFAULT_REPO_URL;

public void readConfigByPluginAutoSave() {
MiraiPluginConfig config = MiraiPluginConfig.INSTANCE;

probability = (byte) config.getProbability();
respondSelfNudge = config.getRespondSelfNudge();

autoUpdate = config.getAutoUpdate();
repositoryUrl = config.getRepositoryUrl();
disabledGroups = config.getDisabledGroups();
coolDown = config.getCoolDown();
groupCoolDown = config.getGroupCoolDown();
inCoolDownMessage = config.getInCoolDownMessage().isBlank() ?
null : config.getInCoolDownMessage();
if ("[nudge]".equals(inCoolDownMessage)) inCoolDownNudge = true;

devMode = config.getDevMode();
messageHook = config.getMessageHook();

switch (config.getDisablePolicy()) {
case NONE:
nudgeCanBeDisabled = false;
messageCanBeDisabled = false;
break;
case NUDGE:
nudgeCanBeDisabled = true;
messageCanBeDisabled = false;
break;
case MESSAGE:
nudgeCanBeDisabled = false;
messageCanBeDisabled = true;
break;
case FULL:
nudgeCanBeDisabled = true;
messageCanBeDisabled = true;
break;
}

super.readPluginServiceConfig(config.toPluginServiceConfig());
}

@Override
public void readData(File dir){
if (dir.listFiles() == null) {
System.out.println(autoUpdate ?
"o((>ω< ))o 你这头懒猪, 没有下载petData!\n\\^o^/ 还好我冰雪聪明, 帮你自动更新了⭐" :
"(゚Д゚*)ノ 没有petData! 你自己手动更新吧x\n(☆-v-) 笨蛋! 让你不开自动更新⭐");
return;
}
super.readData(dir);
}

/**
* 发送随机图片
*/
public void sendImage(Group group, Member from, Member to) { //发送随机图片
sendImage(group, from, to, randomableList.get(MiraiPetpet.random.nextInt(randomableList.size())));
}

/**
* 有概率发送随机图片
*/
public void sendImage(Group group, Member from, Member to, boolean random) {
if (!random) {
sendImage(group, from, to);
return;
}
int r = MiraiPetpet.random.nextInt(99) + 1; //不能为0
if (r >= probability) return;
sendImage(group, from, to);
}

/**
* 用key发送图片(无otherText)
*/
@Deprecated
public void sendImage(Group group, Member from, Member to, String key) {
sendImage(group, from, to, key, null);
}

/**
* 用key发送图片,指定otherText
*/
@Deprecated
public void sendImage(Group group, Member from, Member to, String key, String otherText) {
TextExtraData textExtraData = new TextExtraData(
from.getNameCard().isEmpty() ? from.getNick() : from.getNameCard(),
to.getNameCard().isEmpty() ? to.getNick() : to.getNameCard(),
group.getName(),
otherText == null || otherText.equals("") ? new ArrayList<>() :
new ArrayList<>(Arrays.asList(otherText.split("\\s+")))
);
GifAvatarExtraDataProvider gifAvatarExtraDataProvider = BaseConfigFactory.getGifAvatarExtraDataFromUrls(
from.getAvatarUrl(), to.getAvatarUrl(), group.getAvatarUrl(), group.getBotAsMember().getAvatarUrl(),
group.getMembers().stream().map(NormalMember::getAvatarUrl).collect(Collectors.toList())
);
sendImage(group, key, gifAvatarExtraDataProvider, textExtraData);
}

public void sendImage(Group group, String key,
GifAvatarExtraDataProvider gifAvatarExtraDataProvider, TextExtraData textExtraData) {
try {
group.sendMessage(
inputStreamToImage(generateImage(
key,
gifAvatarExtraDataProvider,
textExtraData,
null
).getFirst(), group)
);
} catch (Exception ex) {
System.out.println("发送图片时出错:" + ex.getMessage());
ex.printStackTrace();
}
}

public Image inputStreamToImage(@NotNull InputStream input, Contact contact) throws IOException {
ExternalResource resource = ExternalResource.create(input);
if (contact == null) contact = Bot.getInstances().get(0).getAsFriend();
Image image = contact.uploadImage(resource);
resource.close();
return image;
}
}
Loading

0 comments on commit 2b7c347

Please sign in to comment.