mirror of
https://github.com/jie65535/mirai-console-jpa-plugin.git
synced 2024-07-27 19:15:06 +08:00
V0.1.0
This commit is contained in:
parent
351c282c92
commit
bcfc156bd0
@ -3,10 +3,10 @@ plugins {
|
||||
kotlin("jvm") version kotlinVersion
|
||||
kotlin("plugin.serialization") version kotlinVersion
|
||||
|
||||
id("net.mamoe.mirai-console") version "2.6.6"
|
||||
id("net.mamoe.mirai-console") version "2.9.0-RC"
|
||||
}
|
||||
|
||||
group = "org.example"
|
||||
group = "top.jie65535"
|
||||
version = "0.1.0"
|
||||
|
||||
repositories {
|
||||
|
@ -6,4 +6,4 @@ pluginManagement {
|
||||
maven("https://dl.bintray.com/kotlin/kotlin-eap")
|
||||
}
|
||||
}
|
||||
rootProject.name = "mirai-console-plugin-template"
|
||||
rootProject.name = "mirai-console-jpa-plugin"
|
@ -1,51 +0,0 @@
|
||||
package org.example.mirai.plugin;
|
||||
|
||||
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription;
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
|
||||
import net.mamoe.mirai.event.Event;
|
||||
import net.mamoe.mirai.event.EventChannel;
|
||||
import net.mamoe.mirai.event.GlobalEventChannel;
|
||||
import net.mamoe.mirai.event.events.FriendMessageEvent;
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
||||
|
||||
|
||||
/**
|
||||
* 使用 Java 请把
|
||||
* {@code /src/main/resources/META-INF.services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin}
|
||||
* 文件内容改成 {@code org.example.mirai.plugin.JavaPluginMain} <br/>
|
||||
* 也就是当前主类全类名
|
||||
*
|
||||
* 使用 Java 可以把 kotlin 源集删除且不会对项目有影响
|
||||
*
|
||||
* 在 {@code settings.gradle.kts} 里改构建的插件名称、依赖库和插件版本
|
||||
*
|
||||
* 在该示例下的 {@link JvmPluginDescription} 修改插件名称,id 和版本等
|
||||
*
|
||||
* 可以使用 {@code src/test/kotlin/RunMirai.kt} 在 IDE 里直接调试,
|
||||
* 不用复制到 mirai-console-loader 或其他启动器中调试
|
||||
*/
|
||||
|
||||
public final class JavaPluginMain extends JavaPlugin {
|
||||
public static final JavaPluginMain INSTANCE = new JavaPluginMain();
|
||||
private JavaPluginMain() {
|
||||
super(new JvmPluginDescriptionBuilder("org.example.mirai-example", "0.1.0")
|
||||
.info("EG")
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getLogger().info("日志");
|
||||
EventChannel<Event> eventChannel = GlobalEventChannel.INSTANCE.parentScope(this);
|
||||
eventChannel.subscribeAlways(GroupMessageEvent.class, g -> {
|
||||
//监听群消息
|
||||
getLogger().info(g.getMessage().contentToString());
|
||||
|
||||
});
|
||||
eventChannel.subscribeAlways(FriendMessageEvent.class, f -> {
|
||||
//监听好友消息
|
||||
getLogger().info(f.getMessage().contentToString());
|
||||
});
|
||||
}
|
||||
}
|
24
src/main/kotlin/JPACommand.kt
Normal file
24
src/main/kotlin/JPACommand.kt
Normal file
@ -0,0 +1,24 @@
|
||||
package top.jie65535.jpa
|
||||
|
||||
import net.mamoe.mirai.console.command.CommandSender
|
||||
import net.mamoe.mirai.console.command.CompositeCommand
|
||||
|
||||
object JPACommand : CompositeCommand(
|
||||
JPictureArchiving,"jpa",
|
||||
description = "图片存档插件命令"
|
||||
) {
|
||||
|
||||
@SubCommand("setDir")
|
||||
@Description("设置图片存档目录")
|
||||
suspend fun CommandSender.setDir(dir: String) {
|
||||
JPAPluginConfig.archiveDirectory = dir
|
||||
sendMessage("OK")
|
||||
}
|
||||
|
||||
@SubCommand("reset")
|
||||
@Description("重置存档目录到插件数据目录")
|
||||
suspend fun CommandSender.resetDir() {
|
||||
JPAPluginConfig.archiveDirectory = ""
|
||||
sendMessage("OK")
|
||||
}
|
||||
}
|
10
src/main/kotlin/JPAPluginConfig.kt
Normal file
10
src/main/kotlin/JPAPluginConfig.kt
Normal file
@ -0,0 +1,10 @@
|
||||
package top.jie65535.jpa
|
||||
|
||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||
import net.mamoe.mirai.console.data.ValueDescription
|
||||
import net.mamoe.mirai.console.data.value
|
||||
|
||||
object JPAPluginConfig : AutoSavePluginConfig("jpa") {
|
||||
@ValueDescription("指定存档根目录 为空则保存到插件数据目录")
|
||||
var archiveDirectory: String by value()
|
||||
}
|
57
src/main/kotlin/JPictureArchiving.kt
Normal file
57
src/main/kotlin/JPictureArchiving.kt
Normal file
@ -0,0 +1,57 @@
|
||||
package top.jie65535.jpa
|
||||
|
||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.event.events.MessageEvent
|
||||
import net.mamoe.mirai.event.globalEventChannel
|
||||
import net.mamoe.mirai.message.data.Image
|
||||
import net.mamoe.mirai.message.data.Image.Key.queryUrl
|
||||
import net.mamoe.mirai.utils.info
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
object JPictureArchiving : KotlinPlugin(
|
||||
JvmPluginDescription(
|
||||
id = "top.jie65535.mirai-console-jpa-plugin",
|
||||
name = "J Picture Archiving",
|
||||
version = "0.1.0"
|
||||
) {
|
||||
author("jie65535")
|
||||
info("这个插件只做一件事,将机器人收到的所有图片存档")
|
||||
}
|
||||
) {
|
||||
private val okHttpClient: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.build()
|
||||
}
|
||||
override fun onEnable() {
|
||||
logger.info { "Plugin loaded" }
|
||||
JPAPluginConfig.reload()
|
||||
JPACommand.register()
|
||||
|
||||
globalEventChannel().subscribeAlways<MessageEvent> {
|
||||
message.filterIsInstance<Image>().forEach {
|
||||
val url = it.queryUrl()
|
||||
val filePath = "${this.subject.id}/${it.imageId}"
|
||||
val file = if (JPAPluginConfig.archiveDirectory.isBlank()) {
|
||||
resolveDataFile(filePath)
|
||||
} else {
|
||||
File(JPAPluginConfig.archiveDirectory, filePath)
|
||||
}
|
||||
if (!file.exists()) {
|
||||
val request = Request.Builder().url(url).build()
|
||||
val imageByte = okHttpClient.newCall(request).execute().body!!.bytes()
|
||||
val fileParent = file.parentFile
|
||||
if (!fileParent.exists()) fileParent.mkdirs()
|
||||
file.writeBytes(imageByte)
|
||||
logger.info("Saved ${file.path}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package org.example.mirai.plugin
|
||||
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.event.EventChannel
|
||||
import net.mamoe.mirai.event.GlobalEventChannel
|
||||
import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent
|
||||
import net.mamoe.mirai.event.events.FriendMessageEvent
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent
|
||||
import net.mamoe.mirai.event.events.NewFriendRequestEvent
|
||||
import net.mamoe.mirai.event.globalEventChannel
|
||||
import net.mamoe.mirai.message.data.Image
|
||||
import net.mamoe.mirai.message.data.Image.Key.queryUrl
|
||||
import net.mamoe.mirai.message.data.PlainText
|
||||
import net.mamoe.mirai.utils.info
|
||||
|
||||
/**
|
||||
* 使用 kotlin 版请把
|
||||
* `src/main/resources/META-INF.services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin`
|
||||
* 文件内容改成 `org.example.mirai.plugin.PluginMain` 也就是当前主类全类名
|
||||
*
|
||||
* 使用 kotlin 可以把 java 源集删除不会对项目有影响
|
||||
*
|
||||
* 在 `settings.gradle.kts` 里改构建的插件名称、依赖库和插件版本
|
||||
*
|
||||
* 在该示例下的 [JvmPluginDescription] 修改插件名称,id和版本,etc
|
||||
*
|
||||
* 可以使用 `src/test/kotlin/RunMirai.kt` 在 ide 里直接调试,
|
||||
* 不用复制到 mirai-console-loader 或其他启动器中调试
|
||||
*/
|
||||
|
||||
object PluginMain : KotlinPlugin(
|
||||
JvmPluginDescription(
|
||||
id = "org.example.mirai-example",
|
||||
name = "插件示例",
|
||||
version = "0.1.0"
|
||||
) {
|
||||
author("作者名称或联系方式")
|
||||
info(
|
||||
"""
|
||||
这是一个测试插件,
|
||||
在这里描述插件的功能和用法等.
|
||||
""".trimIndent()
|
||||
)
|
||||
// author 和 info 可以删除.
|
||||
}
|
||||
) {
|
||||
override fun onEnable() {
|
||||
logger.info { "Plugin loaded" }
|
||||
//配置文件目录 "${dataFolder.absolutePath}/"
|
||||
val eventChannel = GlobalEventChannel.parentScope(this)
|
||||
eventChannel.subscribeAlways<GroupMessageEvent>{
|
||||
//群消息
|
||||
//复读示例
|
||||
if (message.contentToString().startsWith("复读")) {
|
||||
group.sendMessage(message.contentToString().replace("复读", ""))
|
||||
}
|
||||
if (message.contentToString() == "hi") {
|
||||
//群内发送
|
||||
group.sendMessage("hi")
|
||||
//向发送者私聊发送消息
|
||||
sender.sendMessage("hi")
|
||||
//不继续处理
|
||||
return@subscribeAlways
|
||||
}
|
||||
//分类示例
|
||||
message.forEach {
|
||||
//循环每个元素在消息里
|
||||
if (it is Image) {
|
||||
//如果消息这一部分是图片
|
||||
val url = it.queryUrl()
|
||||
group.sendMessage("图片,下载地址$url")
|
||||
}
|
||||
if (it is PlainText) {
|
||||
//如果消息这一部分是纯文本
|
||||
group.sendMessage("纯文本,内容:${it.content}")
|
||||
}
|
||||
}
|
||||
}
|
||||
eventChannel.subscribeAlways<FriendMessageEvent>{
|
||||
//好友信息
|
||||
sender.sendMessage("hi")
|
||||
}
|
||||
eventChannel.subscribeAlways<NewFriendRequestEvent>{
|
||||
//自动同意好友申请
|
||||
accept()
|
||||
}
|
||||
eventChannel.subscribeAlways<BotInvitedJoinGroupRequestEvent>{
|
||||
//自动同意加群申请
|
||||
accept()
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
org.example.mirai.plugin.PluginMain
|
||||
top.jie65535.jpa.JPictureArchiving
|
@ -5,13 +5,14 @@ import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable
|
||||
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load
|
||||
import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader
|
||||
import top.jie65535.jpa.JPictureArchiving
|
||||
|
||||
suspend fun main() {
|
||||
MiraiConsoleTerminalLoader.startAsDaemon()
|
||||
|
||||
//如果是Kotlin
|
||||
PluginMain.load()
|
||||
PluginMain.enable()
|
||||
JPictureArchiving.load()
|
||||
JPictureArchiving.enable()
|
||||
//如果是Java
|
||||
// JavaPluginMain.INSTANCE.load()
|
||||
// JavaPluginMain.INSTANCE.enable()
|
||||
|
Loading…
Reference in New Issue
Block a user