generated from KarinJS/karin-plugin-template-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 调整代码缩进和空格 - 移除多余的分号 - 统一使用模板字符串 - 简化了一些代码结构
- Loading branch information
1 parent
c53b273
commit 3319ec5
Showing
1 changed file
with
23 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,50 @@ | ||
import { karin, common,segment,logger } from 'node-karin' | ||
import { karin, common, segment, logger } from 'node-karin' | ||
export const magnetSearch = karin.command(/^#?磁力搜索\s*(.+)/, async (e) => { | ||
const searchQuery = e.msg.match(/^#?磁力搜索\s*(.+)$/)?.[1]; | ||
const searchQuery = e.msg.match(/^#?磁力搜索\s*(.+)$/)?.[1] | ||
if (!searchQuery) { | ||
await e.reply('请输入有效的搜索关键词!', { at: true, recallMsg: 0, reply: true }); | ||
return true; | ||
await e.reply('请输入有效的搜索关键词!', { at: true, recallMsg: 0, reply: true }) | ||
return true | ||
} | ||
|
||
const url = `https://cili.site/search?q=${encodeURIComponent(searchQuery)}`; | ||
const url = `https://cili.site/search?q=${encodeURIComponent(searchQuery)}` | ||
try { | ||
const response = await fetch(url); | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
throw new Error(`请求失败,状态码:${response.status}`); | ||
throw new Error(`请求失败,状态码:${response.status}`) | ||
} | ||
|
||
const data = await response.text(); | ||
const results: { title: string; size: string; link: string }[] = []; | ||
const data = await response.text() | ||
const results: { title: string; size: string; link: string }[] = [] | ||
const regex = | ||
/<tr>[\s\S]*?<td>[\s\S]*?<a href="([^"]+)">[\s\S]*?<p class="sample">([^<]+)<\/p>[\s\S]*?<\/a>[\s\S]*?<\/td>[\s\S]*?<td class="td-size">([^<]+)<\/td>/g; | ||
/<tr>[\s\S]*?<td>[\s\S]*?<a href="([^"]+)">[\s\S]*?<p class="sample">([^<]+)<\/p>[\s\S]*?<\/a>[\s\S]*?<\/td>[\s\S]*?<td class="td-size">([^<]+)<\/td>/g | ||
|
||
let match: RegExpExecArray | null; | ||
let match: RegExpExecArray | null | ||
while ((match = regex.exec(data)) !== null) { | ||
const link = `https://cili.site${match[1]}`; | ||
const title = match[2].trim(); | ||
const size = match[3].trim(); | ||
results.push({ title, size, link }); | ||
const link = `https://cili.site${match[1]}` | ||
const title = match[2].trim() | ||
const size = match[3].trim() | ||
results.push({ title, size, link }) | ||
} | ||
|
||
if (results.length > 0) { | ||
const forward = results.map((row) => | ||
segment.text(`名称: ${row.title}\n文件大小: ${row.size}\n下载链接: ${row.link}`) | ||
); | ||
) | ||
|
||
const msg = common.makeForward(forward, e.self_id, e.bot.account.name); | ||
await e.bot.sendForwardMessage(e.contact, msg); | ||
const msg = common.makeForward(forward, e.self_id, e.bot.account.name) | ||
await e.bot.sendForwardMessage(e.contact, msg) | ||
} else { | ||
await e.reply('未找到匹配的资源。', { at: true, recallMsg: 0, reply: true }); | ||
await e.reply('未找到匹配的资源。', { at: true, recallMsg: 0, reply: true }) | ||
} | ||
} catch (error) { | ||
await e.reply('搜索过程中发生错误,请稍后再试。', { at: true, recallMsg: 0, reply: true }); | ||
logger.error(error); | ||
await e.reply('搜索过程中发生错误,请稍后再试。', { at: true, recallMsg: 0, reply: true }) | ||
logger.error(error) | ||
} | ||
|
||
return true; | ||
return true | ||
}, { | ||
priority: 9999, | ||
log: true, | ||
name: '磁力搜索', | ||
permission: 'all', | ||
}); | ||
}) |