karin-plugin-teamspeak 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/apps/ts3.d.ts CHANGED
@@ -1,12 +1,11 @@
1
- import { TeamSpeak } from "ts3-nodejs-library";
1
+ import { Query } from "teamspeak.js";
2
2
  declare class ts3 {
3
- teamspeak: undefined | TeamSpeak;
3
+ teamspeak: undefined | Query;
4
4
  connectTimer: number;
5
5
  private isReConnecting;
6
6
  init: () => Promise<void>;
7
7
  getAllChannelList: () => Promise<string | undefined>;
8
8
  quitTs: () => Promise<void>;
9
- reconnectTs: () => Promise<void>;
10
9
  private handelReconnect;
11
10
  }
12
11
  declare const teamspeak3: ts3;
package/lib/apps/ts3.js CHANGED
@@ -1,4 +1,4 @@
1
- import { QueryProtocol, TeamSpeak } from "ts3-nodejs-library";
1
+ import { Query } from "teamspeak.js";
2
2
  import { config } from "../utils/index.js";
3
3
  import karin, { logger, segment, app } from "node-karin";
4
4
  import moment from "node-karin/moment";
@@ -19,48 +19,47 @@ class ts3 {
19
19
  if (this.teamspeak) {
20
20
  await this.quitTs();
21
21
  }
22
- const _teamspeak = new TeamSpeak({
23
- host: TS.HOST,
24
- protocol: QueryProtocol[TS.PROTOCOL],
25
- queryport: TS.QUERY_PORT,
26
- serverport: TS.SERVER_PORT,
27
- username: TS.USERNAME,
28
- password: TS.PASSWORD,
29
- nickname: TS.NICKNAME,
22
+ const _teamspeak = new Query({
23
+ host: "jacksixth.top",
24
+ port: 10011,
25
+ protocol: TS.PROTOCOL == "SSH" ? "ssh" : "tcp",
30
26
  });
31
- _teamspeak.on("ready", async () => {
27
+ _teamspeak.on("Ready", async () => {
32
28
  if (this.isReConnecting) {
33
- logger.info(loggerPluginName + "重连成功");
29
+ logger.info(loggerPluginName + "ts3重连成功");
34
30
  }
35
31
  else {
36
32
  logger.info(loggerPluginName + "ts3连接成功");
37
33
  }
38
34
  this.isReConnecting = false;
35
+ await _teamspeak.login(TS.USERNAME, TS.PASSWORD);
36
+ await _teamspeak.virtualServers.use({ port: TS.QUERY_PORT });
37
+ await _teamspeak.notifications.subscribeAll();
38
+ await _teamspeak.client.setNickname(TS.NICKNAME);
39
39
  this.teamspeak = _teamspeak;
40
40
  });
41
- _teamspeak.on("close", (e) => {
42
- logger.error(loggerPluginName + "ts3连接断开", e);
41
+ _teamspeak.on("Close", () => {
42
+ logger.error(loggerPluginName + "ts3连接断开");
43
43
  this.handelReconnect();
44
44
  });
45
- _teamspeak.on("error", (err) => {
45
+ _teamspeak.on("Error", (err) => {
46
46
  logger.error(loggerPluginName + "ts3连接出错", err);
47
- this.handelReconnect();
48
47
  });
49
- _teamspeak.on("clientconnect", (e) => {
50
- if (!disNotifyNameList.includes(e.client.nickname)) {
51
- logger.info(loggerPluginName + e.client.nickname + "进入ts");
52
- const msg = segment.text(e.client.nickname + "进入ts");
48
+ _teamspeak.on("ClientEnterView", (e) => {
49
+ if (e.nickname && !disNotifyNameList.includes(e.nickname)) {
50
+ logger.info(loggerPluginName + e.nickname + "进入ts");
51
+ const msg = segment.text(e.nickname + "进入ts");
53
52
  TS.NOTICE_GROUP_NO.forEach((groupNo) => {
54
53
  const contact = karin.contact("group", groupNo + "");
55
54
  karin.sendMsg(karin.getBotAll()[1].account.selfId, contact, msg);
56
55
  });
57
56
  }
58
57
  });
59
- _teamspeak.on("clientdisconnect", (e) => {
60
- if (e.client) {
61
- if (!disNotifyNameList.includes(e.client.nickname)) {
62
- logger.info(loggerPluginName + e.client.nickname + "离开ts");
63
- const msg = segment.text(e.client.nickname + "离开ts");
58
+ _teamspeak.on("ClientLeaveView", (e) => {
59
+ if (e.nickname) {
60
+ if (!disNotifyNameList.includes(e.nickname)) {
61
+ logger.info(loggerPluginName + e.nickname + "离开ts");
62
+ const msg = segment.text(e.nickname + "离开ts");
64
63
  TS.NOTICE_GROUP_NO.forEach((groupNo) => {
65
64
  const contact = karin.contact("group", groupNo + "");
66
65
  karin.sendMsg(karin.getBotAll()[1].account.selfId, contact, msg);
@@ -69,15 +68,13 @@ class ts3 {
69
68
  }
70
69
  });
71
70
  // 监听用户移动频道事件
72
- _teamspeak.on("clientmoved", (e) => {
71
+ _teamspeak.on("ClientMove", (e) => {
73
72
  if (TS.ENABLE_CHANNEL_MOVE_NOTIFY === "true" &&
74
- e.client &&
75
- !disNotifyNameList.includes(e.client.nickname)) {
76
- logger.info(loggerPluginName +
77
- e.client.nickname +
78
- "移动到频道: " +
79
- e.channel.name);
80
- const msg = segment.text(e.client.nickname + "移动到频道: " + e.channel.name);
73
+ e.nickname &&
74
+ e.channel &&
75
+ !disNotifyNameList.includes(e.nickname)) {
76
+ logger.info(loggerPluginName + e.nickname + "移动到频道: " + e.channel.name);
77
+ const msg = segment.text(e.nickname + "移动到频道: " + e.channel.name);
81
78
  TS.NOTICE_GROUP_NO.forEach((groupNo) => {
82
79
  const contact = karin.contact("group", groupNo + "");
83
80
  karin.sendMsg(karin.getBotAll()[1].account.selfId, contact, msg);
@@ -90,83 +87,63 @@ class ts3 {
90
87
  if (!this.teamspeak) {
91
88
  return;
92
89
  }
93
- //默认尝试使用渲染器,调用失败则表示未连接渲染器
94
- let usePuppeteer = false;
95
- // try {
96
- // render.App()
97
- // } catch (error) {
98
- // usePuppeteer = false
99
- // }
100
90
  const TS = config();
101
91
  const renderList = [];
102
- renderList.push(usePuppeteer
103
- ? `<h1>${TS.SERVER_NAME || TS.HOST}</h1>`
104
- : `====${TS.SERVER_NAME || TS.HOST}====`);
105
- renderList.push(usePuppeteer
106
- ? `<div class="tips">仅展示有人的频道</div>`
107
- : `仅展示有人的频道 `);
108
- if (!usePuppeteer) {
109
- //不渲染成图片就使用=====分割频道
110
- renderList.push(`======`);
111
- }
112
- const channelList = await this.teamspeak.channelList(); //所有频道
92
+ renderList.push(`====${TS.SERVER_NAME || TS.HOST}====`);
93
+ renderList.push(`仅展示有人的频道 `);
94
+ renderList.push(`======`);
95
+ const channels = await this.teamspeak.channels.fetch(); //所有频道
96
+ const channelList = channels.map((i) => i);
113
97
  let count = 0;
98
+ const allClients = await this.teamspeak.clients.fetch(); //所有在线用户
99
+ const allClient = allClients
100
+ .filter((c) => !disNotifyNameList.includes(c.nickname))
101
+ .map((i) => i); //过滤掉不提醒的用户
114
102
  for (let index = 0; index < channelList.length; index++) {
115
103
  const channel = channelList[index]; //频道
116
- const allClient = await channel.getClients(); //在当前频道的人
117
- //排除不显示的人
118
- const clients = allClient.filter((c) => !disNotifyNameList.includes(c.nickname));
104
+ const clients = allClient.filter((c) => c.channelId == channel.id); //频道内所有用户
119
105
  count += clients.length;
120
106
  if (clients.length == 0)
121
107
  continue;
122
- renderList.push(usePuppeteer ? `<ul>${channel.name}` : ` ${channel.name} `);
108
+ renderList.push(` ${channel.name} `);
123
109
  for (let index = 0; index < clients.length; index++) {
124
110
  const client = clients[index];
125
- const connectTimeSec = moment().diff(moment.unix(client.lastconnected), "second");
126
- let connectTime = `(${Math.floor(connectTimeSec / 60)}:${Math.floor(connectTimeSec % 60)}) `;
127
- renderList.push(usePuppeteer
128
- ? `<li>${client.nickname} ${connectTime}</li>`
129
- : `- ${client.nickname} ${connectTime}`);
130
- }
131
- if (!usePuppeteer) {
132
- //不渲染成图片就使用=====分割频道
133
- renderList.push(`======`);
134
- }
135
- else {
136
- renderList.push("</ul>");
111
+ if (client.databaseId) {
112
+ const cdb = await this.teamspeak.getRawClientDatabaseProperties(client.databaseId);
113
+ const connectTimeSec = moment().diff(moment.unix(Number(cdb.client_lastconnected)), "second");
114
+ let connectTime = `(${Math.floor(connectTimeSec / 60)}:${Math.floor(connectTimeSec % 60)}) `;
115
+ renderList.push(`- ${client.nickname} ${connectTime}`);
116
+ }
137
117
  }
118
+ renderList.push(`======`);
138
119
  }
139
- renderList.splice(2, 0, usePuppeteer
140
- ? `<div class="count">当前频道内共有${count}人</div>`
141
- : `当前频道内共有${count}人`);
142
- return usePuppeteer ? renderList.join("") : renderList.join("\n");
120
+ renderList.splice(2, 0, `当前频道内共有${count}人`);
121
+ return renderList.join("\n");
143
122
  };
144
123
  //关闭连接
145
124
  quitTs = async () => {
146
125
  if (this.teamspeak) {
147
- this.teamspeak.removeAllListeners();
148
- this.teamspeak.quit();
149
- this.teamspeak = undefined;
150
- }
151
- };
152
- //重新连接
153
- reconnectTs = async () => {
154
- if (this.teamspeak) {
155
- this.teamspeak.reconnect(config().RECONNECT_TIMER, 1000);
156
- }
157
- else {
158
- this.init();
126
+ try {
127
+ this.teamspeak.removeAllListeners();
128
+ this.teamspeak.destroy();
129
+ this.teamspeak = undefined;
130
+ }
131
+ catch (error) {
132
+ logger.info(loggerPluginName + "关闭连接失败", error);
133
+ this.teamspeak = undefined;
134
+ }
159
135
  }
160
136
  };
161
137
  //重连逻辑
162
138
  async handelReconnect() {
163
- const TS = config();
139
+ //没走过重连
164
140
  if (!this.teamspeak || this.isReConnecting)
165
141
  return;
166
142
  this.isReConnecting = true;
167
143
  logger.info(loggerPluginName + "重连中...");
168
144
  try {
169
- await this.teamspeak.reconnect(TS.RECONNECT_TIMER, 1000);
145
+ this.quitTs();
146
+ await teamspeak3.init();
170
147
  }
171
148
  catch (e) {
172
149
  logger.error(loggerPluginName + "连接TS3失败", e);
@@ -183,26 +160,32 @@ export const getAllUserList = async () => {
183
160
  if (teamspeak3.teamspeak) {
184
161
  let count = 0;
185
162
  const res = {};
186
- const channelList = await teamspeak3.teamspeak.channelList().catch(() => {
187
- return [];
188
- }); //所有频道
163
+ const channels = await teamspeak3.teamspeak.channels.fetch(); //所有频道
164
+ const channelList = channels.map((i) => i);
165
+ const allClients = await teamspeak3.teamspeak.clients.fetch();
166
+ const allClient = allClients
167
+ .filter((c) => !disNotifyNameList.includes(c.nickname))
168
+ .map((i) => i);
189
169
  for (let index = 0; index < channelList.length; index++) {
190
170
  const channel = channelList[index]; //频道
191
- const allClient = await channel.getClients(); //在当前频道的人
192
- //排除不显示的人
193
- const clients = allClient.filter((c) => !disNotifyNameList.includes(c.nickname));
171
+ const clients = allClient.filter((c) => c.channelId == channel.id);
194
172
  count += clients.length;
195
- res[channel.name] = clients.map((c) => {
196
- const connectTimeSec = moment().diff(moment.unix(c.lastconnected), "second");
197
- let connectTime = `${Math.floor(connectTimeSec / 60)}:${Math.floor(connectTimeSec % 60)}`;
198
- return {
199
- nickName: c.nickname, //昵称
200
- lastconnected: moment
201
- .unix(c.lastconnected)
202
- .format("YYYY-MM-DD HH:mm:ss"), //上次连接进来的时间
203
- connectTime, //已经连接的时间 - 单位分:秒
204
- };
205
- });
173
+ if (channel.name) {
174
+ res[channel.name] = [];
175
+ for (let index = 0; index < clients.length; index++) {
176
+ const client = clients[index];
177
+ const cdb = await teamspeak3.teamspeak.getRawClientDatabaseProperties(client.databaseId);
178
+ const connectTimeSec = moment().diff(moment.unix(Number(cdb.client_lastconnected)), "second");
179
+ let connectTime = `(${Math.floor(connectTimeSec / 60)}:${Math.floor(connectTimeSec % 60)}) `;
180
+ res[channel.name].push({
181
+ nickName: client.nickname,
182
+ connectTime,
183
+ lastconnected: moment
184
+ .unix(Number(cdb.client_lastconnected))
185
+ .format("YYYY-MM-DD HH:mm:ss"), //上次连接进来的时间
186
+ });
187
+ }
188
+ }
206
189
  }
207
190
  return {
208
191
  name: config().SERVER_NAME || config().HOST,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "karin-plugin-teamspeak",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "karin 的 teamspeak 插件",
5
5
  "homepage": "https://github.com/jacksixth/karin-plugin-teamspeak3",
6
6
  "bugs": {
@@ -26,7 +26,7 @@
26
26
  "pr": "node lib/cli/pr.js"
27
27
  },
28
28
  "dependencies": {
29
- "ts3-nodejs-library": "^3.5.2"
29
+ "teamspeak.js": "^1.11.2"
30
30
  },
31
31
  "karin": {
32
32
  "apps": [