-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
372 lines (334 loc) · 9.91 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// jshint esversion: 9
const mysql = require("mysql2");
const { Client, GatewayIntentBits } = require("discord.js");
const uniqid = require("uniqid");
const { token } = require("./token.json");
const db_config = require("./db_config.json");
var sql = mysql.createConnection({
host: db_config.host,
user: db_config.user,
password: db_config.password,
database: db_config.database,
});
sql.connect((err) => {
if (err) {
console.error("error connecting: " + err.stack);
return;
}
console.log("connected as id " + sql.threadId);
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const botPrefix = "~zltd~";
function checkPerm(guild, permission) {
const botID = client.user.id;
const hasPerm = getMember(guild, botID).hasPermission(permission);
return hasPerm;
}
function getMember(guild, DiscordID) {
const member = guild.members.cache.get(DiscordID);
return member;
}
function getDiscordID(GuildID, SteamID64, callback) {
const query = `
SELECT DiscordID FROM guild_${GuildID} WHERE SteamID64 = ?
`;
sql.query(query, [SteamID64], (err, result) => {
if (err) throw err;
if (result && result.length > 0) {
callback(result[0].DiscordID || false);
} else {
callback(false);
}
});
}
function getSteamID64(GuildID, DiscordID, callback) {
const query = `
SELECT SteamID64 FROM guild_${GuildID} WHERE DiscordID = ?
`;
sql.query(query, [DiscordID], (err, result) => {
if (err) throw err;
if (result && result.length > 0) {
callback(result[0].SteamID64 || false);
} else {
callback(false);
}
});
}
function checkStatus(guild) {
if (checkPerm(guild, "MUTE_MEMBERS")) {
const GuildID = guild.id;
const query = `
SELECT * FROM guild_${GuildID}
`;
sql.query(query, (err, result) => {
if (err) throw err;
if (result && result.length > 0) {
result.forEach((resulti) => {
const member = getMember(guild, resulti.DiscordID);
if (member) {
if (resulti.Muted == "1" && resulti.Connected == "1" && !member.serverMute) {
member.setMute(true).then(() => {
console.log(`Muted ${member.user.id} from ${GuildID}. Reason: Set to muted.`);
});
} else if (resulti.Muted == "0" && member.serverMute) {
member.setMute(false).then(() => {
console.log(`Unmuted ${member.user.id} from ${GuildID}. Reason: Set to unmuted.`);
});
}
}
});
}
});
}
}
function linkIDs(GuildID, DiscordID, SteamID64) {
const query = `
INSERT INTO guild_${GuildID} (DiscordID, SteamID64, Muted, Connected)
VALUES (?, ?, '0', '0')
ON DUPLICATE KEY UPDATE
DiscordID = VALUES(DiscordID),
SteamID64 = VALUES(SteamID64),
Muted = VALUES(Muted),
Connected = VALUES(Connected)
`;
sql.query(query, [DiscordID, SteamID64], (err, result) => {
if (err) throw err;
console.log("Row inserted or updated");
});
}
function unmute(GuildID, SteamID64) {
const query = `
UPDATE guild_${GuildID} SET Muted = '0' WHERE SteamID64 = ?
`;
sql.query(query, [SteamID64], (err, result) => {
if (err) throw err;
});
}
function init(guild) {
const query = `
CREATE TABLE IF NOT EXISTS tokens (
GuildID VARCHAR(64) NOT NULL,
Token VARCHAR(18) NOT NULL,
TokenSent TINYINT(1) NOT NULL,
UNIQUE (GuildID)
)
`;
sql.query(query, (err, result) => {
if (err) throw err;
});
guildInit(guild);
checkToken(guild);
}
function tokenProcess(tokenCheck, guild) {
switch (tokenCheck) {
case "create":
createToken(guild);
break;
case "send":
sendToken(guild);
break;
case false:
break;
}
}
function guildInit(guild) {
const query = `
CREATE TABLE IF NOT EXISTS guild_${guild.id} (
DiscordID VARCHAR(64) NOT NULL,
SteamID64 VARCHAR(64) NOT NULL,
Muted TINYINT(1) NOT NULL,
Connected TINYINT(1),
UNIQUE (DiscordID)
)
`;
sql.query(query, (err, result) => {
if (err) throw err;
});
}
function checkToken(guild) {
const query = `
SELECT TokenSent FROM tokens WHERE GuildID = ?
`;
sql.query(query, [guild.id], (err, result) => {
if (err) throw err;
if (result && result.length !== 0) {
const resulti = result[0];
if (resulti.TokenSent == 1) {
tokenProcess(false, guild);
} else if (resulti.TokenSent == 0) {
tokenProcess("send", guild);
}
} else {
tokenProcess("create", guild);
}
});
}
function createToken(guild) {
const token = uniqid();
const query = `
INSERT INTO tokens (GuildID, Token, TokenSent)
VALUES (?, ?, 0)
ON DUPLICATE KEY UPDATE Token = VALUES(Token)
`;
sql.query(query, [guild.id, token], (err, result) => {
if (err) throw err;
});
sendToken(guild);
}
function sendToken(guild) {
const query = `
SELECT Token FROM tokens WHERE GuildID = ?
`;
sql.query(query, [guild.id], (err, result) => {
if (err) throw err;
if (result.length !== 0) {
const resulti = result[0];
guild.owner
.send(
`This is your token: \`${resulti.Token}\`.\nYou need to insert this token inside the _TTTDiscordLink.lua_ file.\nFor more information visit https://github.com/zlyfer/TTTDiscordLink#install-garrys-mod-server-script.\nHave fun!`
)
.then(() => {
const updateQuery = `
UPDATE tokens SET TokenSent = 1 WHERE GuildID = ?
`;
sql.query(updateQuery, [guild.id], (err, result) => {
if (err) throw err;
});
});
}
});
}
client.on("messageCreate", (message) => {
var Guild = message.guild;
var Author = message.author;
var Channel = message.channel;
var MessageContent = message.content;
if (MessageContent.indexOf(botPrefix) != -1) {
if (!Author.bot) {
MessageContent = MessageContent.replace(botPrefix, "");
MessageContent = String(MessageContent).split(" ");
var cmd = MessageContent[0];
var msg = MessageContent[1];
if (Channel.type == 0) {
var GuildID = Guild.id;
var AuthorID = Author.id;
var Member = getMember(Guild, AuthorID);
switch (cmd) {
case "help":
var helpObj = {
help: {
parameter: "none",
desc: "Shows this help message.",
},
link: {
parameter: "SteamID64",
desc: "Links your SteamID64 with your DiscordID.",
},
unmute: {
parameter: "none",
desc: "Unmutes you - even if you are already dead.",
},
token: {
parameter: "none",
desc: "Generate a new token. Only usable by the Discord server owner!",
},
};
var reply = `help is on the way:\n`;
reply += "Make sure to use **" + botPrefix + "** as prefix!\n";
reply += "The format is: **COMMAND** __PARAMETER__ - *DESCRIPTION*.\n\n";
for (var key in helpObj) {
reply +=
"**" +
key +
"** __" +
helpObj[key].parameter +
"__ - *" +
helpObj[key].desc +
"*\n";
}
reply +=
"\nINFO: If you encounter any issues or have questions, feel free to contact me.\n";
message.reply(reply);
break;
case "link":
if (msg) {
var SteamID64 = msg;
linkIDs(GuildID, AuthorID, SteamID64);
// message.reply(`I linked your DiscordID **${AuthorID}** with SteamID64 **${SteamID64}**.`);
message.react("👍");
setTimeout(
function () {
message.delete();
},
3000,
message
);
} else {
message.reply(`please provide your SteamID64.`);
message.delete();
}
break;
case "unmute":
getSteamID64(GuildID, AuthorID, function (SteamID64) {
if (SteamID64) {
unmute(GuildID, SteamID64);
}
});
message.channel.send(`${Member} unmuted him-/herself.`);
break;
case "token":
if (message.author.id == message.guild.owner.id) {
createToken(message.guild);
} else {
message.author.send(`Sorry, only the Discord server owner can request a new token.`);
}
break;
default:
message.reply(
`sorry I didn't understand that. Use **${botPrefix}help** to see all available commands.`
);
break;
}
} else {
message.reply(
`Sorry I am supposed to be controlled via a text channel on a discord server.`
);
}
}
}
});
client.on("guildCreate", (guild) => {
init(guild);
});
client.on("ready", () => {
client.user.setActivity("Use " + botPrefix + "help for help!", { type: "PLAYING" });
for (let i = 0; i < client.guilds.cache.length; i++) {
init(client.guilds.cache[i]);
}
setInterval(() => {
for (let i = 0; i < client.guilds.cache.length; i++) {
checkStatus(client.guilds.cache[i]);
}
}, 1000);
console.log(`Bot is ready.`);
console.log(`Logged in as ${client.user.tag}!`);
});
/* ---------- Bot Shutdown ---------- */
const handleShutdown = async () => {
console.info("Logging out and shutting down...");
await client.destroy();
process.exit(0);
};
process.on("SIGINT", handleShutdown);
process.on("SIGTERM", handleShutdown);
process.on("unhandledRejection", (err) => {
console.error("UNHANDLED: " + err);
});
/* ----------- Bot Startup ---------- */
client.login(token);