已解决 # 如何设置工作目录&&如何new一个目录&&接入TBP过程到底有什么问题
-
~ 如何设置工作目录
override fun onEnable() { super.onEnable() logger.info("sayBack onEnable") val workingDir = File("data/com.greenhandzdl/") val cacheDir = File("data/com.greenhandzdl/cache/") init()//初始化配置文件
这样好像不太对
~如何创建目录val file = File("data/插件名") file.mkdirs()
提示special,飘红
~TBP函数相关问题fun TBP(m: String) : String{ val json = JSONObject(File("data/TBP").readText()) //获取数据 val SecretId = json.getString("SecretId") val SecretKey = json.getString("SecretKey") val BotId = json.getString("BotId") val BotEnv = json.getString("BotEnv") val TerminalId = json.getString("TerminalId") val requestBody = FormBody.Builder() .add("SecretId", SecretId) .add("SecretKey", SecretKey) .add("BotId", BotId) .add("BotEnv", BotEnv) .add("TerminalId", TerminalId) .add("InputText", m) .build() val request = Request.Builder() .url("https://tbp.tencentcloudapi.com") .post(requestBody) .build() val response = OkHttpClient().newCall(request).execute() val responseData = response.body?.string() //解析json val jsonObject = JSONObject(responseData) //提取json中的ResponseMessage并转为string var responseMessage = jsonObject.getString("ResponseMessage") if (responseMessage == "") { responseMessage = "由于网络问题,暂时没找到答案!" } return responseMessage }
相关问题项目:https://github.com/greenhandzdl/mirai-plugins-sayBack/blob/master/src/main/kotlin/SayBack.kt
-
-
都不清楚你在干什么
Plugin 有
Plugin.dataFolder
作为插件专门存储数据的目录 -
@cssxsh
但是我要在启动的时候去初始一个example文件,然后在里面去初始几个配置值 -
val pluginfiles = File(Plugin.dataFolder).listFiles()
ide提示datafile飘红
Unresolved reference: dataFolder -
···
public interface PluginFileExtensions {
/**
* 数据目录路径
* @see PluginData
*/
public val dataFolderPath: Path/** * 数据目录. `dataFolderPath.toFile()` * @see PluginData */ public val dataFolder: File
···
我是这样用的val pluginfiles = File(Plugin.dataFolderPath) ··· 但是dataFolderPath报错
-
SayBack.dataFolder
你的SayBack
是 plugin 实例,调用它不就好了 -
谢谢大佬
另外开发中的还有这几个问题并没有解决:
- 接入TBP接口中的error:
022-05-02 22:27:45 E/sayBack: Exception in coroutine Plugin com.greenhandzdl.mirai-plugins-sayBack of sayBack org.json.JSONException: JSONObject["ResponseMessage"] not found. at org.json.JSONObject.get(JSONObject.java:587) at org.json.JSONObject.getString(JSONObject.java:874) at com.greenhandzdl.SayBackKt.tbp(SayBack.kt:261) at com.greenhandzdl.SayBack$onEnable$1.invokeSuspend(SayBack.kt:180) at com.greenhandzdl.SayBack$onEnable$1.invoke(SayBack.kt) at com.greenhandzdl.SayBack$onEnable$1.invoke(SayBack.kt) at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invokeSuspend(EventChannel.kt:455) at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invoke(EventChannel.kt) at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invoke(EventChannel.kt) at net.mamoe.mirai.internal.event.Handler$onEvent$2.invokeSuspend(InternalEventListeners.kt:50) at net.mamoe.mirai.internal.event.Handler$onEvent$2.invoke(InternalEventListeners.kt) at net.mamoe.mirai.internal.event.Handler$onEvent$2.invoke(InternalEventListeners.kt) at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89) at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:166) at kotlinx.coroutines.BuildersKt.withContext(Unknown Source) at net.mamoe.mirai.internal.event.Handler.onEvent(InternalEventListeners.kt:50) at net.mamoe.mirai.internal.event.InternalEventListenersKt.process(InternalEventListeners.kt:166) at net.mamoe.mirai.internal.event.InternalEventListenersKt.callAndRemoveIfRequired(InternalEventListeners.kt:115) at net.mamoe.mirai.event._EventBroadcast.broadcastImpl(Event.kt:174) at net.mamoe.mirai.IMirai.broadcastEvent(IMirai.kt:318) at net.mamoe.mirai.internal.MiraiImpl.broadcastEvent$suspendImpl(MiraiImpl.kt:308) at net.mamoe.mirai.internal.MiraiImpl.broadcastEvent(MiraiImpl.kt) at net.mamoe.mirai.event._EventBroadcast.broadcastPublic$suspendImpl(Event.kt:161) at net.mamoe.mirai.event._EventBroadcast.broadcastPublic(Event.kt) at net.mamoe.mirai.event.EventKt.broadcast(Event.kt:146) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invokeSuspend(EventDispatcher.kt:90) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invoke(EventDispatcher.kt) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invoke(EventDispatcher.kt) at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89) at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:166) at kotlinx.coroutines.BuildersKt.withContext(Unknown Source) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcast$suspendImpl(EventDispatcher.kt:89) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcast(EventDispatcher.kt) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invokeSuspend(EventDispatcher.kt:105) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invoke(EventDispatcher.kt) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invoke(EventDispatcher.kt) at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55) at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112) at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56) at kotlinx.coroutines.BuildersKt.launch(Unknown Source) at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcastAsync-kKe4JaQ(EventDispatcher.kt:102) at net.mamoe.mirai.internal.network.components.EventDispatcher.broadcastAsync-kKe4JaQ$default(EventDispatcher.kt:35) at net.mamoe.mirai.internal.network.components.EventBroadcasterPacketHandler.impl(PacketHandler.kt:87) at net.mamoe.mirai.internal.network.components.EventBroadcasterPacketHandler.handlePacket(PacketHandler.kt:73) at net.mamoe.mirai.internal.network.components.PacketHandlerChain.handlePacket(PacketHandler.kt:42) at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invokeSuspend(NetworkHandlerSupport.kt:84) at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invoke(NetworkHandlerSupport.kt) at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invoke(NetworkHandlerSupport.kt) at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55) at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112) at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56) at kotlinx.coroutines.BuildersKt.launch(Unknown Source) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47) at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source) at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport.collectReceived$mirai_core(NetworkHandlerSupport.kt:82) at net.mamoe.mirai.internal.network.impl.netty.NettyNetworkHandler$PacketDecodePipeline$send$1.invokeSuspend(NettyNetworkHandler.kt:178) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:829) 2022-05-02 22:27:45 V/Bot.742431945: [无产Bot,联合起来!(970131925)] greenhandzdl(修炼中)(2029252158) -> 问答 草 >
部分代码
fun tbp(m :String) : String{ val json = JSONObject(File("$configFolder/TBP.json").readText()) //获取数据 val SecretId = json.getString("SecretId") val SecretKey = json.getString("SecretKey") val BotId = json.getString("BotId") val BotEnv = json.getString("BotEnv") val TerminalId = json.getString("TerminalId") val requestBody = FormBody.Builder() .add("SecretId", SecretId) .add("SecretKey", SecretKey) .add("BotId", BotId) .add("BotEnv", BotEnv) .add("TerminalId", TerminalId) .add("InputText", m) .build() val request = Request.Builder() .url("https://tbp.tencentcloudapi.com") .post(requestBody) .build() val response = OkHttpClient().newCall(request).execute() val responseData = response.body?.string() //解析json val jsonObject = JSONObject(responseData) //提取json中的ResponseMessage并转为string var responseMessage = jsonObject.getString("ResponseMessage") if (responseMessage == "") { responseMessage = "由于网络问题,暂时没找到答案!" } return responseMessage } }
message.contentToString().startsWith("问答") -> { val m = message.contentToString().replace("问答", "") val responseMessage = tbp(m) group.sendMessage(messageChainOf(PlainText(responseMessage))) }
-
@greenhandzdl
同样的,我试图插入维基api也遇到这种情况message.contentToString().startsWith("维基") -> { val m = message.contentToString().replace("维基", "") val result = wiki(m) group.sendMessage(messageChainOf(PlainText(result))) }
fun wiki(m :String) :String{ val requestBody =FormBody.Builder() .add("title", m) .build() val request = Request.Builder() .url("https://wiki.greenhandzdl.tk/api/rest_v1/page/related/") .post(requestBody) .build() val response = OkHttpClient().newCall(request).execute() val responseBody = response.body?.string() val json = JSONObject(responseBody) //解析每一个page和extract,并将page和extract拼接起来返回 val pages = json.getJSONArray("pages") var result = "" for (i in 0 until pages.length()) { val page = pages.getJSONObject(i) val extract = page.getString("extract") val title = page.getString("title") result += title + ":" + extract + "\n" } return result }
-
此回复已被删除! -
我觉得你应该 先跑一下单元测试,看一下json的实际内容是否符合要求
另外为什么不用json解析框架,手动处理json不麻烦吗
-
json大概跑了,基本上卡在签名那
我目前没太多时间去整,而且我相信用户也没有耐心去调该死的配置(真的,特别麻烦)
所以到最后我可能会尝试接入其他的机器人api -