Skip to content

Commit 10f5f39

Browse files
committed
feat: bind group close #1
1 parent 69700d2 commit 10f5f39

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/main/kotlin/io/github/gnuf0rce/mirai/netdisk/NetDisk.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
101101
if (permission.testPermission(contact.permitteeId).not() &&
102102
permission.testPermission(sender.permitteeId).not()
103103
) return
104+
val netdisk = contact.netdisk
104105

105106
launch {
106107
logger.info { "发现文件消息 $content 开始上传" }
@@ -109,7 +110,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
109110
}
110111

111112
val rapid = try {
112-
uploadAbsoluteFile(file)
113+
netdisk.uploadAbsoluteFile(file)
113114
} catch (cause: Exception) {
114115
logger.warning({ "上传失败 $file" }, cause)
115116
if (NetdiskUploadConfig.reply) {
@@ -132,23 +133,25 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
132133
public fun MessageEvent.save() {
133134
val plain = message.findIsInstance<PlainText>() ?: return
134135
if (permission.testPermission(sender.permitteeId).not()) return
136+
val netdisk = sender.netdisk
135137

136138
launch {
137139
val paths = mutableListOf<String>()
138140
SHORT_URL_REGEX.findAll(plain.content).forEach { match ->
139141
logger.info { "发现分享链接 ${match.value} 开始转存" }
140142
val (surl, password) = match.destructured
141-
val path = sender.netdisk.saveShareLink(surl = surl, password = password)
143+
val path = netdisk.saveShareLink(surl = surl, password = password)
142144
paths.add(path)
143145

144146
launch {
145147
NetDiskFileSyncRecorder.record(source = source, surl = surl, password = password)
146148
}
147149
}
148-
if (NetdiskUploadConfig.reply) {
150+
if (paths.isNotEmpty() && NetdiskUploadConfig.reply) {
151+
val info = netdisk.user()
149152
subject.sendMessage(buildMessageChain {
150153
append(message.quote())
151-
appendLine("分享转存成功")
154+
appendLine("分享转存到 ${info.netdiskName} 成功")
152155
paths.forEach { path ->
153156
appendLine(path)
154157
}
@@ -160,17 +163,18 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
160163
STAND_CODE_REGEX.findAll(plain.content).forEach { match ->
161164
logger.info { "发现秒传码 ${match.value} 开始保存" }
162165
val upload = RapidUploadInfo.parse(code = match.value)
163-
val path = sender.netdisk.saveRapidUpload(upload = upload)
166+
val path = netdisk.saveRapidUpload(upload = upload)
164167
paths.add(path)
165168

166169
launch {
167170
NetDiskFileSyncRecorder.record(source = source, rapid = upload)
168171
}
169172
}
170-
if (NetdiskUploadConfig.reply) {
173+
if (paths.isNotEmpty() && NetdiskUploadConfig.reply) {
174+
val info = netdisk.user()
171175
subject.sendMessage(buildMessageChain {
172176
append(message.quote())
173-
appendLine("秒传码保存成功")
177+
appendLine("秒传码保存到 ${info.netdiskName} 成功")
174178
paths.forEach { path ->
175179
appendLine(path)
176180
}
@@ -184,18 +188,19 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
184188
val (base64) = match.destructured
185189
STAND_CODE_REGEX.findAll(base64.decodeBase64String()).forEach { m ->
186190
val upload = RapidUploadInfo.parse(code = m.value)
187-
val path = sender.netdisk.saveRapidUpload(upload = upload)
191+
val path = netdisk.saveRapidUpload(upload = upload)
188192
paths.add(path)
189193

190194
launch {
191195
NetDiskFileSyncRecorder.record(source = source, rapid = upload)
192196
}
193197
}
194198
}
195-
if (NetdiskUploadConfig.reply) {
199+
if (paths.isNotEmpty() && NetdiskUploadConfig.reply) {
200+
val info = netdisk.user()
196201
subject.sendMessage(buildMessageChain {
197202
append(message.quote())
198-
appendLine("秒传链接保存成功")
203+
appendLine("秒传链接到 ${info.netdiskName} 保存成功")
199204
paths.forEach { path ->
200205
appendLine(path)
201206
}
@@ -205,7 +210,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
205210
}
206211

207212
@PublishedApi
208-
internal suspend fun uploadAbsoluteFile(file: AbsoluteFile): RapidUploadInfo {
213+
internal suspend fun BaiduNetDiskClient.uploadAbsoluteFile(file: AbsoluteFile): RapidUploadInfo {
209214
val url = requireNotNull(file.getUrl()) { "远程文件 URL 获取失败" }
210215
@Suppress("INVISIBLE_MEMBER")
211216
val rapid = with(file) {

src/main/kotlin/io/github/gnuf0rce/mirai/netdisk/command/BaiduOAuthCommand.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ internal object BaiduOAuthCommand : CompositeCommand(
6262

6363
@SubCommand
6464
suspend fun UserCommandSender.bind() {
65-
val client = BaiduNetDiskPool.create(id = user.id)
65+
val target = when(val sender = user) {
66+
is Friend -> sender.id
67+
is Member -> if (sender.isOperator()) sender.group.id else sender.id
68+
else -> 0
69+
}
70+
val client = BaiduNetDiskPool.create(id = target)
6671
client.runCatching {
6772
authorize { url ->
6873
requestInput("请打开连接,然后在十分钟内输入获得的认证码, $url")
6974
} to user()
7075
}.onSuccess { (token, info) ->
71-
BaiduNetDiskPool.cache[user.id] = client
76+
BaiduNetDiskPool.cache[target] = client
7277
logger.info { "百度云用户认证成功, ${info.baiduName} by $token" }
7378
sendMessage("百度云用户认证成功\n ${info.baiduName}")
7479
}.onFailure { cause ->

0 commit comments

Comments
 (0)