创建插件基本框架

This commit is contained in:
2022-01-10 18:25:31 +08:00
parent 9056df5f23
commit f0eccaedaf
10 changed files with 128 additions and 156 deletions

View File

@ -1,12 +1,12 @@
plugins {
val kotlinVersion = "1.4.30"
val kotlinVersion = "1.5.30"
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.2"
}
group = "org.example"
group = "top.jie65535"
version = "0.1.0"
repositories {

View File

@ -6,4 +6,4 @@ pluginManagement {
maven("https://dl.bintray.com/kotlin/kotlin-eap")
}
}
rootProject.name = "mirai-console-plugin-template"
rootProject.name = "mirai-console-jhr-plugin"

View File

@ -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());
});
}
}

View File

@ -0,0 +1,24 @@
package top.jie65535.jhr
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CompositeCommand
object JHRCommand : CompositeCommand(
JHorseRacing, "jhr",
description = "HorseRacing Commands"
) {
@SubCommand
@Deprecated("开启赛马")
suspend fun CommandSender.enable(group: Long) {
if (JHRPluginConfig.enabledGroups.indexOf(group) == -1)
JHRPluginConfig.enabledGroups.add(group)
sendMessage("OK")
}
@SubCommand
@Deprecated("开启赛马")
suspend fun CommandSender.disable(group: Long) {
JHRPluginConfig.enabledGroups.remove(group)
sendMessage("OK")
}
}

View File

@ -0,0 +1,14 @@
package top.jie65535.jhr
import net.mamoe.mirai.console.data.AutoSavePluginConfig
import net.mamoe.mirai.console.data.ValueDescription
import net.mamoe.mirai.console.data.value
object JHRPluginConfig : AutoSavePluginConfig("HorseRacingPluginConfig") {
@ValueDescription("签到奖励")
val signInReward by value(5000)
@ValueDescription("启用赛马的群")
var enabledGroups: MutableList<Long> by value()
}

View File

@ -0,0 +1,12 @@
package top.jie65535.jhr
import net.mamoe.mirai.console.data.AutoSavePluginData
import net.mamoe.mirai.console.data.ValueDescription
import net.mamoe.mirai.console.data.value
object JHRPluginData : AutoSavePluginData("HorseRacingPluginData") {
@ValueDescription("用户存款")
val Scores: HashMap<Long, Int> by value()
}

View File

@ -0,0 +1,70 @@
package top.jie65535.jhr
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
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.At
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
import java.util.*
object JHorseRacing : KotlinPlugin(
JvmPluginDescription(
id = "top.jie65535.mirai-console-jhr-plugin",
name = "J Horse Racing",
version = "0.1.0"
) {
author("jie65535")
info("赛马娘")
}
) {
var date: Date = Calendar.getInstance().time
override fun onEnable() {
logger.info { "Plugin loaded" }
JHRPluginConfig.reload()
JHRPluginData.reload()
JHRCommand.register()
val eventChannel = GlobalEventChannel.parentScope(this)
eventChannel.subscribeAlways<GroupMessageEvent> {
// 确认该群是否启用赛马
if (JHRPluginConfig.enabledGroups.indexOf(group.id) == -1) {
return@subscribeAlways
}
val msg = message.contentToString()
// at机器人
if (message.any { it is At && it.target == bot.id }) {
when {
msg.startsWith("#赛马") -> TODO()
msg.startsWith("#开始赛马") -> TODO()
}
} else {
when {
msg.startsWith("#签到") -> TODO()
msg.startsWith("#押马") -> TODO()
msg.startsWith("#余额") -> TODO()
}
}
}
}
override fun onDisable() {
JHRCommand.unregister()
}
}

View File

@ -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()
}
}
}

View File

@ -1 +1 @@
org.example.mirai.plugin.PluginMain
top.jie65535.jhr.JHorseRacing

View File

@ -1,4 +1,4 @@
package org.example.mirai.plugin
package top.jie65535.jhr
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.console.MiraiConsole
@ -9,12 +9,8 @@ import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader
suspend fun main() {
MiraiConsoleTerminalLoader.startAsDaemon()
//如果是Kotlin
PluginMain.load()
PluginMain.enable()
//如果是Java
// JavaPluginMain.INSTANCE.load()
// JavaPluginMain.INSTANCE.enable()
JHorseRacing.load()
JHorseRacing.enable()
val bot = MiraiConsole.addBot(123456, "") {
fileBasedDeviceInfo()