Implement v0.1.0

This commit is contained in:
2022-06-14 11:46:35 +08:00
parent 1f6f12d84b
commit 8838330f0b
8 changed files with 103 additions and 137 deletions

View File

@ -1,5 +1,3 @@
# mirai-console-plugin-template
# mirai-console-j24-plugin
[Mirai Console](https://github.com/mamoe/mirai-console) 插件模板, 使用 Kotlin + Gradle.
[如何使用](https://github.com/project-mirai/how-to-use-plugin-template)
24点计算器

View File

@ -6,10 +6,14 @@ plugins {
id("net.mamoe.mirai-console") version "2.11.1"
}
group = "org.example"
group = "top.jie65535"
version = "0.1.0"
repositories {
maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库
mavenCentral()
}
dependencies {
implementation("net.objecthunter:exp4j:0.4.8")
}

View File

@ -1 +1 @@
rootProject.name = "mirai-console-plugin-template"
rootProject.name = "mirai-console-j24-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

@ -1,93 +1,51 @@
package org.example.mirai.plugin
package top.jie65535.j24
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.event.subscribeMessages
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 = "插件示例",
id = "top.jie65535.j24",
name = "J 24点游戏",
version = "0.1.0"
) {
author("作者名称或联系方式")
info(
"""
这是一个测试插件,
在这里描述插件的功能和用法等.
""".trimIndent()
)
// author 和 info 可以删除.
author("jie65535")
info("24点游戏")
}
) {
private val games: MutableMap<Long, Point24> = mutableMapOf()
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("复读", ""))
eventChannel.subscribeMessages {
contains("24点") quoteReply {
val game = Point24()
games[this.sender.id] = game
"你抽到了 [${game.points[0]}] [${game.points[1]}] [${game.points[2]}] [${game.points[3]}]\n" +
"请用以上四组数字组合成结果为24的算式以“答”开头验证"
}
if (message.contentToString() == "hi") {
//群内发送
group.sendMessage("hi")
//向发送者私聊发送消息
sender.sendMessage("hi")
//不继续处理
return@subscribeAlways
startsWith("") quoteReply {
val game = games[this.sender.id]
if (game == null) {
"你还没有抽数字哦说“24点”来开始游戏吧"
} else {
try {
val result = game.evaluate(message.contentToString().removePrefix("").trim())
if (result == 24.0) {
"恭喜你,答对了!"
} else {
"答错了,计算结果为 $result"
}
//分类示例
message.forEach {
//循环每个元素在消息里
if (it is Image) {
//如果消息这一部分是图片
val url = it.queryUrl()
group.sendMessage("图片,下载地址$url")
}
if (it is PlainText) {
//如果消息这一部分是纯文本
group.sendMessage("纯文本,内容:${it.content}")
} catch (e: Throwable) {
"错误:${e.message}"
}
}
}
eventChannel.subscribeAlways<FriendMessageEvent>{
//好友信息
sender.sendMessage("hi")
}
eventChannel.subscribeAlways<NewFriendRequestEvent>{
//自动同意好友申请
accept()
}
eventChannel.subscribeAlways<BotInvitedJoinGroupRequestEvent>{
//自动同意加群申请
accept()
}
}
}

View File

@ -0,0 +1,55 @@
package top.jie65535.j24
import net.objecthunter.exp4j.ExpressionBuilder
import net.objecthunter.exp4j.shuntingyard.ShuntingYard
import net.objecthunter.exp4j.tokenizer.NumberToken
import net.objecthunter.exp4j.tokenizer.Token
import kotlin.random.Random
class Point24 {
var points = genPoints()
fun regenPoints() {
points = genPoints()
}
private fun genPoints() = arrayOf(
Random.nextInt(1, 14),
Random.nextInt(1, 14),
Random.nextInt(1, 14),
Random.nextInt(1, 14)
)
fun evaluate(expression: String): Double {
val tokens = ShuntingYard.convertToRPN(
expression,
null,
mapOf(),
null,
false
)
val nums = points.toMutableList()
for (token in tokens) {
if (token.type == Token.TOKEN_NUMBER.toInt()) {
val value = (token as NumberToken).value
var i = 0
while (i < nums.size)
if (nums[i].toDouble() == value)
break
else ++i
if (i < nums.size)
nums.removeAt(i)
else
throw IllegalArgumentException("不能使用未允许数值")
}
}
if (nums.isNotEmpty())
throw IllegalArgumentException("必须使用所有数值")
return ExpressionBuilder(expression)
.implicitMultiplication(false)
.build()
.evaluate()
}
}

View File

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

View File

@ -1,4 +1,4 @@
package org.example.mirai.plugin
package top.jie65535.j24
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.console.MiraiConsole
@ -9,16 +9,18 @@ import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader
suspend fun main() {
MiraiConsoleTerminalLoader.startAsDaemon()
//如果是Kotlin
PluginMain.load()
PluginMain.enable()
//如果是Java
// JavaPluginMain.INSTANCE.load()
// JavaPluginMain.INSTANCE.enable()
val bot = MiraiConsole.addBot(123456, "") {
fileBasedDeviceInfo()
}.alsoLogin()
// val bot = MiraiConsole.addBot(123456, "") {
// fileBasedDeviceInfo()
// }.alsoLogin()
MiraiConsole.job.join()
}
fun test() {
val game = Point24()
println(game.points)
println(game.evaluate("${game.points[0]}+${game.points[1]}+${game.points[2]}+${game.points[3]}"))
}