-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.js
More file actions
63 lines (58 loc) · 1.71 KB
/
Copy pathdemo.js
File metadata and controls
63 lines (58 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import fs from 'fs'
import path from 'path'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const pluginsDir = './plugins'
const yourUrl = 'example.com'
// 使用find方法快速定位插件
const findPlugin = () =>
fs.readdirSync(pluginsDir).find((p) => {
const pkgPath = path.join(pluginsDir, p, 'package.json')
try {
const { dependencies } = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
return dependencies?.['qq-official-bot']
} catch {
return false
}
})
const plugin = findPlugin()
if (!plugin) throw new Error('请先安装XiaoyeQQBot插件')
try {
const { SessionManager } = require(path.join(
process.cwd(),
pluginsDir,
plugin,
'node_modules/qq-official-bot/lib/sessionManager.js',
))
Object.assign(SessionManager.prototype, {
getWsUrl: async function () {
const { sandbox, appid } = this.bot.config
const base = sandbox
? `https://${yourUrl}/proxy?url=https://sandbox.api.sgroup.qq.com`
: `https://${yourUrl}/proxy?url=https://api.sgroup.qq.com`
this.bot.request.defaults.baseURL = base
return new Promise((resolve) => {
this.bot.request
.get('/gateway/bot', {
headers: {
Accept: '*/*',
'Accept-Encoding': 'utf-8',
'Accept-Language': 'zh-CN,zh;q=0.8',
Connection: 'keep-alive',
'User-Agent': 'v1',
Authorization: '',
},
})
.then((res) => {
if (!res.data) throw new Error('获取ws连接信息异常')
this.wsUrl = `wss://${yourUrl}/ws?url=${res.data.url}&appid=${appid}`
logger.info(`WebSocket URL 已更新: ${this.wsUrl}`)
resolve(this.wsUrl)
})
})
},
})
} catch (error) {
console.error('插件加载失败:', error)
process.exit(1)
}