|
1 | 1 | // 以下仅为用法示意,详情请参照文档:https://bot.q.qq.com/wiki/develop/nodesdk/
|
| 2 | +const { createOpenAPI, createWebsocket } = require('qq-guild-bot'); |
2 | 3 |
|
3 |
| -const fs = require('fs'); |
4 |
| -const path = require('path'); |
5 |
| -const { createOpenAPI, createWebsocket } = require('../lib/index.js'); |
| 4 | +const testConfigWs = { |
| 5 | + appID: '', |
| 6 | + token: '', |
| 7 | +}; |
6 | 8 |
|
7 |
| -let config = {}; |
8 |
| -// config.json需要您用自己的机器人信息配置 |
9 |
| -const configPath = path.join(__dirname, 'config.json'); |
10 |
| -if (fs.existsSync(configPath)) { |
11 |
| - config = fs.readFileSync(configPath).toString(); |
12 |
| - config = JSON.parse(config); |
13 |
| -} |
| 9 | +const client = createOpenAPI(testConfigWs); |
14 | 10 |
|
15 |
| -console.log(`config信息:\n\n ${JSON.stringify(config)} \n`); |
| 11 | +const ws = createWebsocket(testConfigWs); |
| 12 | +ws.on('READY', (wsdata) => { |
| 13 | + console.log('[READY] 事件接收 :', wsdata); |
| 14 | +}); |
16 | 15 |
|
17 |
| -(async function () { |
18 |
| - // API Demo |
19 |
| - const client = createOpenAPI(config); |
20 |
| - let res = await client.meApi.me(); |
21 |
| - console.log(`\n meApi.me() res: \n\n ${JSON.stringify(res.data)} \n`); |
| 16 | +ws.on('ERROR', (data) => { |
| 17 | + console.log('[ERROR] 事件接收 :', data); |
| 18 | +}); |
| 19 | +ws.on('GUILDS', (data) => { |
| 20 | + console.log('[GUILDS] 事件接收 :', data); |
| 21 | +}); |
| 22 | +ws.on('GUILD_MEMBERS', (data) => { |
| 23 | + console.log('[GUILD_MEMBERS] 事件接收 :', data); |
| 24 | +}); |
| 25 | +ws.on('GUILD_MESSAGES', (data) => { |
| 26 | + console.log('[GUILD_MESSAGES] 事件接收 :', data); |
| 27 | +}); |
| 28 | +ws.on('GUILD_MESSAGE_REACTIONS', (data) => { |
| 29 | + console.log('[GUILD_MESSAGE_REACTIONS] 事件接收 :', data); |
| 30 | +}); |
| 31 | +ws.on('DIRECT_MESSAGE', (data) => { |
| 32 | + console.log('[DIRECT_MESSAGE] 事件接收 :', data); |
| 33 | +}); |
| 34 | +ws.on('INTERACTION', (data) => { |
| 35 | + console.log('[INTERACTION] 事件接收 :', data); |
| 36 | +}); |
| 37 | +ws.on('MESSAGE_AUDIT', (data) => { |
| 38 | + console.log('[MESSAGE_AUDIT] 事件接收 :', data); |
| 39 | +}); |
| 40 | +ws.on('FORUMS_EVENT', (data) => { |
| 41 | + console.log('[FORUMS_EVENT] 事件接收 :', data); |
| 42 | +}); |
| 43 | +ws.on('AUDIO_ACTION', (data) => { |
| 44 | + console.log('[AUDIO_ACTION] 事件接收 :', data); |
| 45 | +}); |
| 46 | +ws.on('PUBLIC_GUILD_MESSAGES', async (eventData) => { |
| 47 | + console.log('[PUBLIC_GUILD_MESSAGES] 事件接收 :', eventData); |
| 48 | + const {data} = await client.messageApi.postMessage('', { |
| 49 | + content: 'test' |
| 50 | + }) |
| 51 | + console.log(data); |
| 52 | +}); |
22 | 53 |
|
23 |
| - res = await client.meApi.meGuilds(); |
24 |
| - console.log(`\n meApi.meGuilds() res: \n\n ${JSON.stringify(res.data)} \n`); |
| 54 | +// client.guildApi.guild('').then((data) => { |
| 55 | +// console.log(data); |
| 56 | +// }); |
25 | 57 |
|
26 |
| - //WS Demo |
27 |
| - const ws = createWebsocket(config); |
28 |
| - ws.on('READY', (data) => { |
29 |
| - console.log('[READY] 事件接收 :', data); |
30 |
| - }); |
31 |
| - ws.on('ERROR', (data) => { |
32 |
| - console.log('[ERROR] 事件接收 :', data); |
33 |
| - }); |
34 |
| - ws.on('GUILDS', (data) => { |
35 |
| - console.log('[GUILDS] 事件接收 :', data); |
36 |
| - }); |
37 |
| - ws.on('GUILD_MEMBERS', (data) => { |
38 |
| - console.log('[GUILD_MEMBERS] 事件接收 :', data); |
39 |
| - }); |
40 |
| - ws.on('DIRECT_MESSAGE', (data) => { |
41 |
| - console.log('[DIRECT_MESSAGE] 事件接收 :', data); |
42 |
| - }); |
43 |
| - ws.on('AUDIO_ACTION', (data) => { |
44 |
| - console.log('[AUDIO_ACTION] 事件接收 :', data); |
45 |
| - }); |
46 |
| - ws.on('AT_MESSAGES', (data) => { |
47 |
| - console.log('[AT_MESSAGES] 事件接收 :', data); |
48 |
| - const msg = data.msg; |
49 |
| - client.messageApi.postMessage(msg.channel_id, { |
50 |
| - content: `<@!${msg.author.id}> hi 收到你的消息啦`, |
51 |
| - msg_id: msg.id, |
52 |
| - }); |
53 |
| - }); |
54 |
| -})(); |
| 58 | +// // ✅ |
| 59 | +// client.channelApi.channels(guildID).then((res) => { |
| 60 | +// console.log(res.data); |
| 61 | +// }); |
0 commit comments