-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
161 lines (131 loc) · 4.75 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {TeamSpeak, TeamSpeakChannel} from "ts3-nodejs-library";
import QueryProtocol = TeamSpeak.QueryProtocol;
import {chatChannelNames} from "./channelNames";
import dotenv from "dotenv"
import * as process from "process";
import {v4} from "uuid";
dotenv.config();
console.log("Starting Application...");
console.log(v4())
const nameMap = new Map<string, string[]>();
nameMap.set(process.env.CHAT_PARENT_ID!, chatChannelNames);
const teamspeak = new TeamSpeak({
host: process.env.TS_HOST,
protocol: QueryProtocol.RAW,
serverport: Number(process.env.TS_SERVERPORT),
queryport: Number(process.env.TS_QUERYPORT),
username: process.env.TS_USERNAME,
password: process.env.TS_PASSWORD,
keepAlive: true
});
async function getSubChannels(parentID: string): Promise<TeamSpeakChannel[]> {
let allChannels = await teamspeak.channelList();
return allChannels.filter(c => {
return c.pid == parentID
});
}
async function getRandomChannelName(parentID: string): Promise<string> {
const currentChannels = await getSubChannels(parentID);
if (!nameMap.has(parentID)) {
return v4();
}
for (const s of nameMap.get(parentID)!) {
// Check if channel with this name exists
if (currentChannels.find(c => c.name.indexOf(s) != -1) == null) {
return s;
}
}
// Generate UUID as fallback name, if all 36 names are used
return v4();
}
function getEmptyChannels(subChannels: TeamSpeakChannel[]): TeamSpeakChannel[] {
let arr: TeamSpeakChannel[] = [];
let chanEmptyCount = 0;
for (const chan of subChannels) {
if (chan.totalClients == 0) {
chanEmptyCount++;
arr.push(chan);
}
}
console.log("Empty channels: ", arr.map(c => c.name))
return arr;
}
teamspeak.on("ready", async () => {
console.log("Conn ready");
await teamspeak.useByPort(Number(process.env.TS_SERVERPORT));
setInterval(async () => {
await checkChannels(process.env.CHAT_PARENT_ID!);
}, 2 * 1000);
setInterval(async () => {
await checkChannels(process.env.TRAINING_PARENT_ID!);
}, 2 * 1000);
});
async function cleanup(subChannels: TeamSpeakChannel[], parentID: string) {
let emptyChannels = getEmptyChannels(subChannels);
for (let i = 0; i < emptyChannels.length - 1; i++) {
const currChan = emptyChannels[emptyChannels.length - 1 - i];
await currChan.del();
}
}
async function checkChannels(parentID: string) {
const subChannels = await getSubChannels(parentID);
const emptyChannels = getEmptyChannels(subChannels);
if (emptyChannels.length > 1) {
await cleanup(subChannels, parentID);
return;
}
// The training channels are supposed to be called "Chat 1 - n"
if (parentID == process.env.TRAINING_PARENT_ID) {
let index = 1;
// Check that the numbering is consistent (in worst case, have to rename all channels)
for (const channel of subChannels) {
if (channel.name != `Training ${index}`) {
try {
await channel.edit({
channelName: `Training ${index}`
});
} catch (e) {
console.error(`Failed to rename channel... ${e}`);
}
}
index++;
}
try {
const newName = `Training ${index}`;
if (emptyChannels.length == 0) {
const chan = await teamspeak.channelCreate(`${newName}`, {
cpid: parentID,
channelFlagSemiPermanent: true,
});
await chan.setPerm({
permname: "b_client_force_push_to_talk",
permvalue: 0
});
return;
}
} catch (e) {
console.error(`Failed to create new channel... ${e}`);
}
}
if (parentID == process.env.CHAT_PARENT_ID) {
const newName = await getRandomChannelName(parentID);
if (emptyChannels.length == 0) {
try {
const chan = await teamspeak.channelCreate(`${newName}`, {
cpid: parentID,
channelFlagSemiPermanent: true,
});
await chan.setPerm({
permname: "b_client_force_push_to_talk",
permvalue: 0
});
return;
} catch (e) {
console.error(`Failed to create new chat channel... ${e}`)
}
}
}
}
teamspeak.on("error", (err) => {
console.log("err", err);
});