初始化

This commit is contained in:
2022-04-15 12:00:47 +08:00
parent 7a53119bf8
commit 68e060d4d4
9 changed files with 62 additions and 170 deletions

View File

@ -1,13 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RunMiraiKt" type="JetRunConfigurationType" nameIsGenerated="true">
<module name="mirai-console-plugin-template.test"/>
<option name="VM_PARAMETERS" value=""/>
<option name="PROGRAM_PARAMETERS" value=""/>
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false"/>
<option name="ALTERNATIVE_JRE_PATH"/>
<option name="PASS_PARENT_ENVS" value="true"/>
<option name="MAIN_CLASS_NAME" value="org.example.mirai.plugin.RunMiraiKt"/>
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$/run"/>
<option name="MAIN_CLASS_NAME" value="top.jie65535.jrandom.RunMiraiKt" />
<module name="mirai-console-jrandom-plugin" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.run" />
<method v="2">
<option name="Make" enabled="true" />
</method>

View File

@ -1,5 +1,3 @@
# mirai-console-plugin-template
# mirai-console-jrandom-plugin
[Mirai Console](https://github.com/mamoe/mirai-console) 插件模板, 使用 Kotlin + Gradle.
[如何使用](https://github.com/project-mirai/how-to-use-plugin-template)
随意作品

View File

@ -6,7 +6,7 @@ plugins {
id("net.mamoe.mirai-console") version "2.10.0"
}
group = "org.example"
group = "top.jie65535"
version = "0.1.0"
repositories {

View File

@ -1 +1 @@
rootProject.name = "mirai-console-plugin-template"
rootProject.name = "mirai-console-jrandom-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,46 @@
package top.jie65535.jrandom
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.utils.info
import java.awt.image.*
import javax.imageio.ImageIO
object JRandomPlugin : KotlinPlugin(
JvmPluginDescription(
id = "top.jie65535.mirai-console-jrandom-plugin",
name = "随意插件",
version = "0.1.0"
) {
author("jie65535")
info("随意插件")
}
) {
override fun onEnable() {
logger.info { "Plugin loaded" }
GlobalEventChannel
.parentScope(this)
.subscribeMessages {
"test" reply {
"OK"
}
}
object : SimpleCommand(this, "test", description = "test") {
@Handler
suspend fun CommandSender.onCommand() {
val bi = BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB)
val ig2 = bi.createGraphics()
ig2.drawString("Hello world", 10, 100)
ImageIO.write(bi, "PNG", dataFolderPath.resolve("test.PNG").toFile())
}
}.register()
}
}

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.jrandom.JRandomPlugin

View File

@ -1,4 +1,4 @@
package org.example.mirai.plugin
package top.jie65535.jrandom
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.console.MiraiConsole
@ -9,16 +9,13 @@ 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()
}
// .alsoLogin()
MiraiConsole.job.join()
}