@@ -96,54 +96,80 @@ object Lolicon: CompositeCommand(
96
96
val (apikey, r18, recall, cooldown) = ExecutionConfig .create(subject)
97
97
val parameters = RequestParams (apikey, keyword, r18, 1 , PluginConfig .proxy, PluginConfig .size1200)
98
98
Main .logger.info(parameters.toReadable())
99
+ Main .logger.info(" proxy: ${FuelManager .instance.proxy} " )
100
+ val response: Response ?
101
+ try {
102
+ response = RequestHandler .get(parameters)
103
+ } catch (fe: FuelError ) {
104
+ Main .logger.warning(fe.toString())
105
+ sendMessage(ReplyConfig .fuelError)
106
+ return
107
+ } catch (ae: APIException ) {
108
+ Main .logger.warning(ae.toString())
109
+ sendMessage(ae.toReadable())
110
+ return
111
+ } catch (e: Exception ) {
112
+ Main .logger.error(e)
113
+ return
114
+ }
115
+ Main .logger.info(response.toReadable())
99
116
try {
100
- Main .logger.info(" proxy: ${FuelManager .instance.proxy} " )
101
- val response: Response = RequestHandler .get(parameters)
102
- Main .logger.info(response.toReadable())
103
117
for (imageData in response.data) {
104
118
Main .logger.info(" url: ${imageData.url} " )
105
- sendMessage(imageData.toReadable())
106
- val stream: InputStream = if (PluginConfig .save && PluginConfig .cache) {
107
- try {
108
- val paths = imageData.url.split(" /" )
109
- val path = " /data/mirai-console-lolicon/download/${paths[paths.lastIndex]} "
110
- val cache = File (System .getProperty(" user.dir" ) + path)
111
- if (cache.exists()) cache.inputStream() else RequestHandler .download(imageData.url)
112
- } catch (e: Exception ) {
113
- RequestHandler .download(imageData.url)
114
- }
115
- } else RequestHandler .download(imageData.url)
116
- val img = subject?.uploadImage(stream)
117
- if (img != null ) {
118
- val receipt = (if (PluginConfig .flash) sendMessage(FlashImage (img)) else sendMessage(img)) ? : return
119
- if (recall > 0 ) {
120
- GlobalScope .launch {
121
- val result = receipt.recallIn((recall * 1000 ).toLong()).awaitIsSuccess()
122
- withContext(Dispatchers .Default ) {
123
- if (! result) Main .logger.warning(receipt.target.toString() + " 撤回失败" )
124
- else Main .logger.info(receipt.target.toString() + " 图片已撤回" )
119
+ val imgInfoReceipt = sendMessage(imageData.toReadable()) ? : continue
120
+ var stream: InputStream ? = null
121
+ try {
122
+ stream = if (PluginConfig .save && PluginConfig .cache) {
123
+ try {
124
+ val paths = imageData.url.split(" /" )
125
+ val path = " /data/mirai-console-lolicon/download/${paths[paths.lastIndex]} "
126
+ val cache = File (System .getProperty(" user.dir" ) + path)
127
+ if (cache.exists()) cache.inputStream() else RequestHandler .download(imageData.url)
128
+ } catch (e: Exception ) {
129
+ RequestHandler .download(imageData.url)
130
+ }
131
+ } else RequestHandler .download(imageData.url)
132
+ val img = subject?.uploadImage(stream)
133
+ if (img != null ) {
134
+ val imgReceipt = (if (PluginConfig .flash) sendMessage(FlashImage (img)) else sendMessage(img)) ? : return
135
+ if (recall > 0 && PluginConfig .recallImg) {
136
+ GlobalScope .launch {
137
+ val result = imgReceipt.recallIn((recall * 1000 ).toLong()).awaitIsSuccess()
138
+ withContext(Dispatchers .Default ) {
139
+ if (! result) Main .logger.warning(imgReceipt.target.toString() + " 图片撤回失败" )
140
+ else Main .logger.info(imgReceipt.target.toString() + " 图片已撤回" )
141
+ }
142
+ }
143
+ }
144
+ if (cooldown > 0 ) {
145
+ Timer .setCooldown(subject)
146
+ GlobalScope .launch {
147
+ Timer .cooldown(subject, cooldown)
148
+ withContext(Dispatchers .Default ) {
149
+ Main .logger.info(imgReceipt.target.toString()+ " 命令已冷却" )
150
+ }
125
151
}
126
152
}
127
153
}
128
- if (cooldown > 0 ) {
129
- Timer .setCooldown(subject)
154
+ } catch (fe: FuelError ) {
155
+ Main .logger.warning(fe.toString())
156
+ sendMessage(ReplyConfig .fuelError)
157
+ } catch (e: Exception ) {
158
+ Main .logger.error(e)
159
+ } finally {
160
+ @Suppress(" BlockingMethodInNonBlockingContext" )
161
+ stream?.close()
162
+ if (recall > 0 && PluginConfig .recallImgInfo) {
130
163
GlobalScope .launch {
131
- Timer .cooldown(subject, cooldown )
164
+ val result = imgInfoReceipt.recallIn((recall * 1000 ).toLong()).awaitIsSuccess( )
132
165
withContext(Dispatchers .Default ) {
133
- Main .logger.info(receipt.target.toString()+ " 命令已冷却" )
166
+ if (! result) Main .logger.warning(imgInfoReceipt.target.toString() + " 图片信息撤回失败" )
167
+ else Main .logger.info(imgInfoReceipt.target.toString() + " 图片信息已撤回" )
134
168
}
135
169
}
136
170
}
137
171
}
138
- @Suppress(" BlockingMethodInNonBlockingContext" )
139
- stream.close()
140
172
}
141
- } catch (fe: FuelError ) {
142
- Main .logger.warning(fe.toString())
143
- sendMessage(ReplyConfig .fuelError)
144
- } catch (ae: APIException ) {
145
- Main .logger.warning(ae.toString())
146
- sendMessage(ae.toReadable())
147
173
} catch (e: Exception ) {
148
174
Main .logger.error(e)
149
175
}
0 commit comments