forked from manix84/discord_gmod_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
223 lines (201 loc) · 5.81 KB
/
web.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const Discord = require('discord.js');
const http = require('http');
const https = require('https');
const DEBUG = Boolean(process.env.DEBUG == 1);
const PORT = Number(process.env.PORT) || 37405; //unused port and since now the OFFICIAL ttt_discord_bot port ;)
const DISCORD_GUILD = String(process.env.DISCORD_GUILD);
const DISCORD_CHANNEL = String(process.env.DISCORD_CHANNEL);
const DISCORD_TOKEN = String(process.env.DISCORD_TOKEN);
const KEEPALIVE_HOST = String(process.env.KEEPALIVE_HOST);
const KEEPALIVE_PORT = Number(process.env.KEEPALIVE_PORT) || PORT;
const KEEPALIVE_ENABLED = Boolean(process.env.KEEPALIVE_ENABLED == 1);
const API_KEY = String(process.env.API_KEY) || false;
const log = (...msg) => (DEBUG ? console.log(...msg) : () => {});
log('Constants: ');
log(" DEBUG: ", DEBUG, `(${typeof DEBUG})`);
log(" PORT: ", PORT, `(${typeof PORT})`);
log(" DISCORD_GUILD: ", DISCORD_GUILD, `(${typeof DISCORD_GUILD})`);
log(" DISCORD_CHANNEL: ", DISCORD_CHANNEL, `(${typeof DISCORD_CHANNEL})`);
log(" DISCORD_TOKEN: ", DISCORD_TOKEN, `(${typeof DISCORD_TOKEN})`);
log(" KEEPALIVE_HOST: ", KEEPALIVE_HOST, `(${typeof KEEPALIVE_HOST})`);
log(" KEEPALIVE_PORT: ", KEEPALIVE_PORT, `(${typeof KEEPALIVE_PORT})`);
log(" KEEPALIVE_ENABLED: ", KEEPALIVE_ENABLED, `(${typeof KEEPALIVE_ENABLED})`);
log(" API_KEY: ", API_KEY, `(${typeof API_KEY})`);
let discordGuild;
let discordChannel;
const mutedPlayers = {};
const requests = [];
//create discord client
const client = new Discord.Client();
client.login(DISCORD_TOKEN);
client.on('ready', () => {
log('Bot is ready to mute them all! :)');
discordGuild = client.guilds.get(DISCORD_GUILD);
// guild = client.guilds.find('id', DISCORD_GUILD);
discordChannel = discordGuild.channels.get(DISCORD_CHANNEL);
// channel = guild.channels.find('id', DISCORD_CHANNEL);
});
client.on('voiceStateUpdate', (oldMember, newMember) => { //player leaves the ttt-channel
if (oldMember.voiceChannel != newMember.voiceChannel && isMemberInVoiceChannel(oldMember)) {
if (isMemberMutedByBot(newMember) && newMember.serverMute) newMember.setMute(false).then(() => {
setMemberMutedByBot(newMember, false);
});
}
});
isMemberInVoiceChannel = (member) => member.voiceChannelID == DISCORD_CHANNEL;
isMemberMutedByBot = (member) => mutedPlayers[member] == true;
setMemberMutedByBot = (member, set = true) => mutedPlayers[member] = set;
requests['connect'] = (params, ret) => {
let tag_utf8 = params.tag.split(" ");
let tag = "";
tag_utf8.forEach((e) => {
tag = tag + String.fromCharCode(e);
});
let found = discordGuild.members.filterArray(val => val.user.tag.match(new RegExp('.*' + tag + '.*')));
if (found.length > 1) {
ret({
answer: 1 //pls specify
});
} else if (found.length < 1) {
ret({
answer: 0 //no found
});
} else {
ret({
tag: found[0].user.tag,
id: found[0].id
});
log(
"[Connect]",
`Connecting ${found[0].user.tag} (${found[0].id})`
);
}
};
requests['mute'] = (params, ret) => {
let id = params.id;
let mute = params.mute;
if (typeof id !== 'string' || typeof mute !== 'boolean') {
ret();
return;
}
log(
"[Mute]",
`Muted ${id}`
);
//let member = guild.members.find('id', id);
let member = discordGuild.members.find(user => user.id === id);
if (member) {
if (isMemberInVoiceChannel(member)) {
if (!member.serverMute && mute) {
member.setMute(true, "dead players can't talk!").then(() => {
setMemberMutedByBot(member);
ret({
success: true
});
}).catch((err) => {
ret({
success: false,
error: err
});
});
}
if (member.serverMute && !mute) {
member.setMute(false).then(() => {
setMemberMutedByBot(member, false);
ret({
success: true
});
}).catch((err) => {
ret({
success: false,
error: err
});
});
}
} else {
ret();
}
} else {
ret({
success: false,
err: 'member not found!' //TODO lua: remove from ids table + file
});
}
};
requests['keep_alive'] = (params, ret) => {
ret({
success: true,
});
log(
"[KeepAlive]",
`Requested`
);
};
const keepAliveReq = () => {
const options = {
host: KEEPALIVE_HOST,
port: KEEPALIVE_PORT,
path: '/keep_alive',
headers: {
req: 'keep_alive',
authorization: `Basic ${API_KEY}`
},
timeout: 5 * 1000 // 5 second request timeout.
};
log(
'[KeepAlive]',
'Requesting',
options
);
https.get(options, (res) => {
const { statusCode } = res;
if (statusCode === 200) {
log(
'[KeepAlive]',
'Success',
`Request successful`
);
} else {
log(
'[KeepAlive]',
'Error',
`Request Failed Status Code: ${statusCode}`
);
}
});
};
http.createServer((req, res) => {
if (
typeof req.headers.params === 'string' &&
typeof req.headers.req === 'string' &&
typeof requests[req.headers.req] === 'function' &&
typeof API_KEY === 'string' && req.headers.authorization === `Basic ${API_KEY}`
) {
try {
let params = JSON.parse(req.headers.params);
requests[req.headers.req](
params,
(ret) => res.end(
JSON.stringify(ret)
)
);
} catch (e) {
res.end('no valid JSON in params');
}
} else {
res.end();
}
}).listen({
port: PORT
}, () => {
log(`Bot endpoint is running: https://${KEEPALIVE_HOST}:${KEEPALIVE_PORT}`);
if (KEEPALIVE_ENABLED) {
log(
'[KeepAlive]',
'[Startup]',
'Initialisation'
);
setInterval(keepAliveReq, 20 * 60 * 1000); // load every 20 minutes
setTimeout(keepAliveReq, 3 * 1000); // load first attempt after 3 seconds.
}
});