-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
570 lines (490 loc) · 20 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js');
const axios = require('axios');
const config = require('./config.json');
const sqlite3 = require('sqlite3').verbose();
const { open } = require('sqlite');
const fs = require('fs');
const dbPromise = initializeDatabase();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', async () => {
// await setupDatabase();
console.log(`Logged in as ${client.user.tag}!`);
const guild = client.guilds.cache.get(config.discordGuildId);
if (!guild) {
console.log('Bot is not in the specified guild. Generate an invite link to add the bot:');
console.log(`https://discord.com/oauth2/authorize?client_id=${config.discordClientId}&permissions=551903307776&scope=bot`);
} else {
console.log(`Bot is already in the guild: ${guild.name}`);
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
// Check if the user is banned
if (config.banned_users.includes(interaction.user.id)) {
await interaction.reply('You are banned from using this bot.');
} else {
switch (commandName) {
case 'shock':
await commandShock(interaction, options);
break;
case 'vibrate':
await commandVibrate(interaction, options);
break;
case 'beep':
await commandBeep(interaction, options);
break;
case 'stats':
await commandStats(interaction, options);
break;
case 'list':
await commandList(interaction, options);
break;
case 'add':
case 'adminadd':
await commandAdd(interaction, options);
break;
case 'remove':
case 'adminremove':
await commandRemove(interaction, options);
break;
case 'debug':
await commandDebug(interaction, options);
break;
case 'shockban':
await commandShockBan(interaction, options);
break;
case 'shockunban':
await commandShockUnban(interaction, options);
break;
case 'config':
await commandConfig(interaction, options);
break;
default:
break;
}
}
});
async function triggerPiShock(action, op, visualOp, intensity, duration, username, sharecode) {
try {
const data = {
Op: op,
intensity: intensity,
duration: duration ?? 1,
Username: config.pishockUsername,
Name: config.pishockAppName,
Code: sharecode,
Apikey: config.pishockApiKey,
}
const response = await axios.post('https://do.pishock.com/api/apioperate/', data);
if (response.status === 200) {
// TODO: Check response data
}
return `${visualOp} **${username}** with intensity ${intensity} and a duration of ${duration}!`; // Customize the response
} catch (error) {
console.error('Error triggering PiShock:', error);
return `Failed to perform ${action}.`;
}
}
async function debugPiShock(sharecode) {
try {
const data = {
Username: config.pishockUsername,
Code: sharecode,
Apikey: config.pishockApiKey,
}
const response = await axios.post('https://do.pishock.com/api/apioperate/', data);
if (response.status === 200) {
// TODO: Check response data
}
return response.data;
} catch (error) {
console.error('Error debugging PiShock:', error);
return `Failed to perform debug.`;
}
}
async function logAction(userId, actionType, intensity, duration, username) {
const db = await dbPromise;
const query = `INSERT INTO logs (discord_user_id, type, intensity, duration, pishock_user) VALUES (?, ?, ?, ?, ?)`;
try {
await db.run(query, [userId, actionType, intensity, duration, username]);
console.log('Action logged successfully.');
} catch (err) {
console.error('Error logging action:', err.message);
}
}
async function initializeDatabase() {
const db = await open({
filename: './pishock_logs.db',
driver: sqlite3.Database
});
await db.exec(`CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
discord_user_id TEXT NOT NULL,
pishock_user TEXT NOT NULL,
type TEXT NOT NULL,
intensity INTEGER NOT NULL,
duration INTEGER NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)`);
return db;
}
function humanizeDuration(duration) {
let seconds = duration % 60;
let minutes = Math.floor(duration / 60);
let hours = Math.floor(minutes / 60);
minutes = minutes % 60;
if (hours > 0) {
return `${hours} hours, ${minutes} minutes, ${seconds} seconds`;
} else if (minutes > 0) {
return `${minutes} minutes, ${seconds} seconds`;
} else {
return `${seconds} seconds`;
}
}
async function commandShock(interaction, options) {
const intensity = options.getInteger('intensity') ?? 1;
const duration = options.getInteger('duration');
const user = options.getString('user');
if (intensity < 1 || intensity > 100) {
await interaction.reply('Intensity must be between 1 and 100.');
return;
}
if (duration < 1 || duration > 15) {
await interaction.reply('Duration must be between 1 and 15.');
return;
}
if (user === 'all' || !user) {
// Shock everyone!
for (const pishock_user of config.pishock_users) {
let shockIntensity = Math.min(intensity, pishock_user.max_shock_intensity);
let shockDuration = Math.min(duration, pishock_user.max_shock_duration);
await triggerPiShock('shock', 0, 'Shocking', shockIntensity, shockDuration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
logAction(interaction.user.id, 'shock', shockIntensity, shockDuration, pishock_user.pishockUsername);
}
await interaction.reply(`Shocking **everyone** with intensity ${intensity} and a duration of ${duration}!`);
} else {
let found = false;
for (const pishock_user of config.pishock_users) {
if (pishock_user.pishockUsername === user) {
let shockIntensity = Math.min(intensity, pishock_user.max_shock_intensity);
let shockDuration = Math.min(duration, pishock_user.max_shock_duration);
const response = await triggerPiShock('shock', 0, 'Shocking', shockIntensity, shockDuration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
await interaction.reply(response);
logAction(interaction.user.id, 'shock', shockIntensity, shockDuration, pishock_user.pishockUsername);
found = true;
}
}
if (!found) {
await interaction.reply('User not found.');
}
}
}
async function commandVibrate(interaction, options) {
const intensity = options.getInteger('intensity') ?? 1;
const duration = options.getInteger('duration');
const user = options.getString('user');
if (intensity < 1 || intensity > 100) {
await interaction.reply('Intensity must be between 1 and 100.');
return;
}
if (duration < 1 || duration > 15) {
await interaction.reply('Duration must be between 1 and 15.');
return;
}
if (user === 'all' || !user) {
// Shock everyone!
for (const pishock_user of config.pishock_users) {
let vibrateIntensity = Math.min(intensity, pishock_user.max_vibrate_intensity);
let vibrateDuration = Math.min(duration, pishock_user.max_vibrate_duration);
await triggerPiShock('vibrate', 1, 'Vibrating', vibrateIntensity, vibrateDuration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
logAction(interaction.user.id, 'vibrate', vibrateIntensity, vibrateDuration, pishock_user.pishockUsername);
}
await interaction.reply(`Vibrating **everyone** with intensity ${intensity} and a duration of ${duration}!`);
} else {
let found = false;
for (const pishock_user of config.pishock_users) {
if (pishock_user.pishockUsername === user) {
let vibrateIntensity = Math.min(intensity, pishock_user.max_vibrate_intensity);
let vibrateDuration = Math.min(duration, pishock_user.max_vibrate_duration);
const response = await triggerPiShock('vibrate', 1, 'Vibrating', vibrateIntensity, vibrateDuration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
await interaction.reply(response);
logAction(interaction.user.id, 'vibrate', vibrateIntensity, vibrateDuration, pishock_user.pishockUsername);
found = true;
}
}
if (!found) {
await interaction.reply('User not found.');
}
}
}
async function commandBeep(interaction, options) {
const duration = options.getInteger('duration');
const user = options.getString('user');
if (duration < 1 || duration > 15) {
await interaction.reply('Duration must be between 1 and 15.');
return;
}
if (user === 'all' || !user) {
// Shock everyone!
for (const pishock_user of config.pishock_users) {
await triggerPiShock('beep', 2, 'Beeping', 1, duration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
logAction(interaction.user.id, 'beep', 1, duration, pishock_user.pishockUsername);
}
await interaction.reply(`Beeping **everyone** with intensity ${intensity} and a duration of ${duration}!`);
} else {
let found = false;
for (const pishock_user of config.pishock_users) {
if (pishock_user.pishockUsername === user) {
const response = await triggerPiShock('beep', 2, 'Beeping', 1, duration, pishock_user.pishockUsername, pishock_user.pishockShareCode);
await interaction.reply(response);
logAction(interaction.user.id, 'beep', 1, duration, pishock_user.pishockUsername);
found = true;
}
}
if (!found) {
await interaction.reply('User not found.');
}
}
}
async function commandStats(interaction, options) {
try {
const db = await dbPromise;
const rows = await db.all(
`SELECT discord_user_id, SUM(duration) as total_duration
FROM logs
WHERE type IN ('shock', 'vibrate')
GROUP BY discord_user_id
ORDER BY total_duration DESC
LIMIT 10`
);
// Creating an embed
const statsEmbed = new EmbedBuilder()
.setColor(0x0099ff) // Use a hexadecimal color
.setTitle('Top 10 Users: Shock & Vibrate Duration')
.setDescription('Users with the most total time spent using shock and vibrate commands')
.setTimestamp();
for (const row of rows) {
try {
const user = await client.users.fetch(row.discord_user_id);
const userName = user ? user.username : 'Unknown User';
statsEmbed.addFields({ name: `User: ${userName}`, value: `Total Duration: ${humanizeDuration(row.total_duration)}`, inline: false });
} catch (error) {
console.error(`Failed to fetch user ${row.discord_user_id}:`, error);
statsEmbed.addFields({ name: `User ID: ${row.discord_user_id}`, value: `Total Duration: ${humanizeDuration(row.total_duration)} seconds (user fetch failed)`, inline: false });
}
}
await interaction.reply({ embeds: [statsEmbed] });
} catch (error) {
console.error('Failed to retrieve stats:', error);
await interaction.reply('Failed to retrieve stats.');
}
}
async function commandList(interaction, options) {
try {
// Creating an embed
const statsEmbed = new EmbedBuilder()
.setColor(0x0099ff) // Use a hexadecimal color
.setTitle('Connected PiShock Users')
.setTimestamp();
let index = 1;
for (const pishock_user of config.pishock_users) {
statsEmbed.addFields({ name: '#' + index, value: pishock_user.pishockUsername, inline: false });
index++;
}
await interaction.reply({ embeds: [statsEmbed] });
} catch (error) {
console.error('Failed to retrieve stats:', error);
await interaction.reply('Failed to retrieve stats.');
}
}
async function commandAdd(interaction, options) {
const username = (interaction.commandName === 'add') ? interaction.user.username : options.getString('username');
const sharecode = options.getString('sharecode');
// Check if the admin is adding a user
if (interaction.commandName === 'adminadd') {
const admin = interaction.member;
if (!admin.roles.cache.some(role => role.id === config.discordAdminRoleId)) {
await interaction.reply('You must be an admin to use this command.');
return;
}
}
if (!username || !sharecode) {
await interaction.reply('Username and sharecode are required.');
return;
}
let found = false;
for (let i = 0; i < config.pishock_users.length; i++) {
const pishock_user = config.pishock_users[i];
if (pishock_user.pishockUsername === username) {
config.pishock_users[i].pishockShareCode = sharecode;
found = true;
break;
}
}
if (found) {
// Save the config file
await interaction.reply(`Updated user ${username}!`);
} else {
config.pishock_users.push({
pishockUsername: username,
pishockShareCode: sharecode,
max_shock_intensity: config.defaultShockIntensity,
max_shock_duration: config.defaultShockDuration,
max_vibrate_intensity: config.defaultVibrateIntensity,
max_vibrate_duration: config.defaultVibrateDuration
});
await interaction.reply(`Added user ${username}!`);
}
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4));
}
async function commandRemove(interaction, options) {
const username = (interaction.commandName === 'remove') ? interaction.user.username : options.getString('username');
// Check if the admin is adding a user
if (interaction.commandName === 'adminremove') {
const admin = interaction.member;
if (!admin.roles.cache.some(role => role.id === config.discordAdminRoleId)) {
await interaction.reply('You must be an admin to use this command.');
return;
}
}
if (!username) {
await interaction.reply('Username is required.');
return;
}
let found = false;
for (let i = 0; i < config.pishock_users.length; i++) {
const pishock_user = config.pishock_users[i];
if (pishock_user.pishockUsername === username) {
config.pishock_users.splice(i, 1);
found = true;
break;
}
}
if (found) {
// Save the config file
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4));
await interaction.reply(`Removed user ${username}!`);
} else {
await interaction.reply('User not found.');
}
}
async function commandDebug(interaction, options) {
const user = options.getString('user');
if (!user) {
await interaction.reply('User is required.');
return;
}
let found = false;
for (const pishock_user of config.pishock_users) {
if (pishock_user.pishockUsername === user) {
const response = await debugPiShock(pishock_user.pishockShareCode);
await interaction.reply(response);
console.log(response);
found = true;
}
}
if (!found) {
await interaction.reply('User not found.');
}
}
async function commandShockBan(interaction, options) {
const user = options.getUser('username');
// Check if the admin is adding a user
const admin = interaction.member;
if (!admin.roles.cache.some(role => role.id === config.discordAdminRoleId)) {
await interaction.reply('You must be an admin to use this command.');
return;
}
if (!user) {
await interaction.reply('Username is required.');
return;
}
let found = false;
for (let i = 0; i < config.banned_users.length; i++) {
const banned_user = config.banned_users[i];
if (banned_user === user.id) {
found = true;
break;
}
}
if (found) {
// Save the config file
await interaction.reply(`${user.username} is already banned!`);
} else {
config.banned_users.push(user.id);
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4));
await interaction.reply(`${user.username} has been banned!`);
}
}
async function commandShockUnban(interaction, options) {
const user = options.getUser('username');
// Check if the admin is adding a user
const admin = interaction.member;
if (!admin.roles.cache.some(role => role.id === config.discordAdminRoleId)) {
await interaction.reply('You must be an admin to use this command.');
return;
}
if (!user) {
await interaction.reply('Username is required.');
return;
}
let found = false;
for (let i = 0; i < config.banned_users.length; i++) {
const banned_user = config.banned_users[i];
if (banned_user === user.id) {
console.log('Removing banned user:', user.id)
config.banned_users.splice(i, 1);
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4));
found = true;
break;
}
}
if (!found) {
// Save the config file
await interaction.reply(`${user.username} is not banned!`);
} else {
config.banned_users.push(user.id);
await interaction.reply(`${user.username} has been unbanned!`);
}
}
async function commandConfig(interaction, options) {
const username = interaction.user.username;
const maxshockintensity = options.getInteger('maxshockintensity');
const maxshockduration = options.getInteger('maxshockduration');
const maxvibrateintensity = options.getInteger('maxvibrateintensity');
const maxvibrateduration = options.getInteger('maxvibrateduration');
if (!maxshockintensity && !maxshockduration && !maxvibrateintensity && !maxvibrateduration) {
await interaction.reply('At least one option is required.');
return;
}
let found = false;
for (let i = 0; i < config.pishock_users.length; i++) {
const pishock_user = config.pishock_users[i];
if (pishock_user.pishockUsername === username) {
let configuser = config.pishock_users[i];
if (maxshockintensity) {
configuser.max_shock_intensity = maxshockintensity;
}
if (maxshockduration) {
configuser.max_shock_duration = maxshockduration;
}
if (maxvibrateintensity) {
configuser.max_vibrate_intensity = maxvibrateintensity;
}
if (maxvibrateduration) {
configuser.max_vibrate_duration = maxvibrateduration;
}
found = true;
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4));
break;
}
}
if (found) {
// Save the config file
await interaction.reply(`Updated user ${username}!`);
} else {
await interaction.reply(`You're not currently in the database. Please use the /add command to add yourself.`);
}
}
client.login(config.discordToken);