-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathpointsactions.js
593 lines (498 loc) · 21.5 KB
/
pointsactions.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
class PointsActions {
constructor(options = {}) {
this.pointsSystem = options.pointsSystem;
this.dbName = options.dbName || 'pointsActionsDB';
this.storeName = options.storeName || 'commandSettings';
this.defaultCommands = {
'!points': { cost: 0, cooldown: 5000, description: 'Check your points balance' },
'!leaderboard': { cost: 0, cooldown: 10000, description: 'Show points leaderboard' },
'!spend': { cost: 0, cooldown: 1000, description: 'Spend points on a reward', isMetaCommand: true },
'!rewards': { cost: 0, cooldown: 5000, description: 'List available rewards' }
};
// Custom commands/actions stored here
this.customCommands = new Map();
// Track command cooldowns
this.cooldowns = new Map();
// Initialize database
this.db = null;
this.initPromise = this.initDatabase();
}
async initDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, 1);
request.onupgradeneeded = event => {
const db = event.target.result;
if (!db.objectStoreNames.contains(this.storeName)) {
const store = db.createObjectStore(this.storeName, { keyPath: 'commandName' });
store.createIndex('active', 'active');
// Add default commands
const tx = event.target.transaction;
const commandStore = tx.objectStore(this.storeName);
Object.entries(this.defaultCommands).forEach(([cmd, settings]) => {
commandStore.add({
commandName: cmd,
cost: settings.cost,
cooldown: settings.cooldown,
description: settings.description,
active: true,
isDefault: true,
isMetaCommand: settings.isMetaCommand || false
});
});
}
};
request.onsuccess = event => {
this.db = event.target.result;
this.loadCommands().then(resolve);
};
request.onerror = () => reject(request.error);
});
}
async ensureDB() {
if (!this.db) await this.initPromise;
return this.db;
}
async loadCommands() {
const db = await this.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(this.storeName, 'readonly');
const store = tx.objectStore(this.storeName);
const request = store.getAll();
request.onsuccess = () => {
this.customCommands.clear();
request.result.forEach(cmd => {
if (cmd.active) {
this.customCommands.set(cmd.commandName.toLowerCase(), cmd);
}
});
resolve();
};
request.onerror = () => {
console.error('Error loading commands:', request.error);
resolve();
};
});
}
async saveCommand(commandData) {
const db = await this.ensureDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(this.storeName, 'readwrite');
const store = tx.objectStore(this.storeName);
// Make sure the command is lowercase for consistent lookups
commandData.commandName = commandData.commandName.toLowerCase();
const request = store.put(commandData);
request.onsuccess = () => {
// Update in-memory map if active
if (commandData.active) {
this.customCommands.set(commandData.commandName, commandData);
} else {
this.customCommands.delete(commandData.commandName);
}
resolve(commandData);
};
request.onerror = () => reject(request.error);
});
}
async deleteCommand(commandName) {
const db = await this.ensureDB();
const normalizedName = commandName.toLowerCase();
// Don't allow deleting default commands
const command = this.customCommands.get(normalizedName);
if (command && command.isDefault) {
return { success: false, message: "Cannot delete default commands" };
}
return new Promise((resolve) => {
const tx = db.transaction(this.storeName, 'readwrite');
const store = tx.objectStore(this.storeName);
const request = store.delete(normalizedName);
request.onsuccess = () => {
this.customCommands.delete(normalizedName);
resolve({ success: true, message: "Command deleted" });
};
request.onerror = () => {
resolve({ success: false, message: "Error deleting command" });
};
});
}
async getCommands() {
return Array.from(this.customCommands.values());
}
async getCommandByName(commandName) {
const normalizedName = commandName.toLowerCase();
return this.customCommands.get(normalizedName) || null;
}
isOnCooldown(username, type, commandName) {
const key = `${username}:${type}:${commandName}`;
const cooldownUntil = this.cooldowns.get(key);
if (!cooldownUntil) return false;
const now = Date.now();
return now < cooldownUntil;
}
setCooldown(username, type, commandName, duration) {
const key = `${username}:${type}:${commandName}`;
const now = Date.now();
this.cooldowns.set(key, now + duration);
// Cleanup expired cooldowns periodically
setTimeout(() => {
if (now + duration <= Date.now()) {
this.cooldowns.delete(key);
}
}, duration + 1000);
}
async processCommand(message) {
if (!message || !message.chatmessage || !message.chatname || !message.type) {
return null;
}
const input = message.chatmessage.trim();
const parts = input.split(' ');
const commandName = parts[0].toLowerCase();
const args = parts.slice(1);
// Check if this is a valid command
const command = this.customCommands.get(commandName);
if (!command) {
return null; // Not a command
}
// Check for cooldown
if (this.isOnCooldown(message.chatname, message.type, commandName)) {
return {
success: false,
message: "This command is on cooldown",
commandName
};
}
// Handle special case for !spend meta-command
if (commandName === '!spend') {
return this.handleSpendCommand(message, args);
}
// Process standard commands
return this.executeCommand(message, command, args);
}
async executeCommand(message, command, args = []) {
const { chatname, type } = message;
// Check if user has enough points
if (command.cost > 0) {
const result = await this.pointsSystem.spendPoints(chatname, type, command.cost);
if (!result.success) {
return {
success: false,
message: `@${chatname}, you don't have enough points. ${result.available} available, ${command.cost} needed.`,
commandName: command.commandName
};
}
}
// Set cooldown
this.setCooldown(chatname, type, command.commandName, command.cooldown);
// Process built-in commands
switch (command.commandName) {
case '!points':
return this.handlePointsCommand(message);
case '!leaderboard':
return this.handleLeaderboardCommand(message, args);
case '!rewards':
return this.handleRewardsCommand(message);
default:
// Process custom commands with actions
return this.handleCustomCommand(message, command, args);
}
}
async handleSpendCommand(message, args) {
// Format: !spend [amount] [reward name]
if (args.length < 2) {
return {
success: false,
message: `@${message.chatname}, usage: !spend [amount] [reward name]`,
commandName: '!spend'
};
}
const amount = parseInt(args[0], 10);
if (isNaN(amount) || amount <= 0) {
return {
success: false,
message: `@${message.chatname}, please provide a valid amount to spend`,
commandName: '!spend'
};
}
const rewardName = args.slice(1).join(' ');
// TODO: Implement custom reward handling logic
// For now, just spend the points
const result = await this.pointsSystem.spendPoints(message.chatname, message.type, amount);
if (!result.success) {
return {
success: false,
message: `@${message.chatname}, you don't have enough points. ${result.available} available, ${amount} needed.`,
commandName: '!spend'
};
}
return {
success: true,
message: `@${message.chatname} spent ${amount} points on "${rewardName}". ${result.remaining} points remaining.`,
commandName: '!spend',
type: 'chat'
};
}
async handlePointsCommand(message) {
const { chatname, type } = message;
const userData = await this.pointsSystem.getUserPoints(chatname, type);
const availablePoints = userData.points - userData.pointsSpent;
return {
success: true,
message: `@${chatname}, you have ${availablePoints} points (${userData.points} earned, ${userData.pointsSpent} spent). Current streak: ${userData.currentStreak}x`,
commandName: '!points',
type: 'chat'
};
}
async handleLeaderboardCommand(message, args) {
const limit = args[0] ? parseInt(args[0], 10) : 5;
const validLimit = isNaN(limit) ? 5 : Math.min(Math.max(limit, 1), 10);
const leaderboard = await this.pointsSystem.getLeaderboard(validLimit, message.type);
let response = `Top ${validLimit} users by points:\n`;
leaderboard.forEach((user, index) => {
response += `${index + 1}. ${user.username}: ${user.points - user.pointsSpent} points (${user.currentStreak}x streak)\n`;
});
return {
success: true,
message: response,
commandName: '!leaderboard',
type: 'chat'
};
}
async handleRewardsCommand(message) {
const commands = await this.getCommands();
const availableRewards = commands.filter(cmd => cmd.cost > 0 && !cmd.isMetaCommand);
if (availableRewards.length === 0) {
return {
success: true,
message: "No rewards are currently available.",
commandName: '!rewards',
type: 'chat'
};
}
let response = "Available rewards:\n";
availableRewards.forEach(reward => {
response += `${reward.commandName} (${reward.cost} points): ${reward.description}\n`;
});
return {
success: true,
message: response,
commandName: '!rewards',
type: 'chat'
};
}
async handleCustomCommand(message, command, args) {
// Handle media commands
if (command.mediaUrl) {
// Send to the points overlay page
const mediaPayload = {
type: 'media',
username: message.chatname,
userType: message.type,
mediaUrl: command.mediaUrl,
mediaType: command.mediaType || 'image',
duration: command.mediaDuration || 5000,
cost: command.cost,
commandName: command.commandName,
timestamp: Date.now()
};
// Add any custom styling or position info
if (command.mediaStyle) mediaPayload.style = command.mediaStyle;
if (command.mediaPosition) mediaPayload.position = command.mediaPosition;
// Trigger the overlay action
sendTargetP2P(mediaPayload, "points");
return {
success: true,
message: command.successMessage || `@${message.chatname} used ${command.commandName} for ${command.cost} points!`,
commandName: command.commandName,
type: 'chat',
mediaTriggered: true
};
}
// Handle chat message commands
if (command.responseMessage) {
let response = command.responseMessage;
// Replace variables in the response
response = response
.replace('{username}', message.chatname)
.replace('{cost}', command.cost)
.replace('{args}', args.join(' '));
return {
success: true,
message: response,
commandName: command.commandName,
type: 'chat'
};
}
// Handle webhook or other custom actions
if (command.actionType === 'webhook' && command.webhookUrl) {
try {
const webhookData = {
username: message.chatname,
userType: message.type,
command: command.commandName,
cost: command.cost,
args: args,
timestamp: Date.now()
};
fetch(command.webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(webhookData)
}).catch(err => console.error('Webhook error:', err));
return {
success: true,
message: command.successMessage || `@${message.chatname} triggered ${command.commandName}!`,
commandName: command.commandName,
type: 'chat'
};
} catch (error) {
console.error('Error triggering webhook:', error);
}
}
// Default success response
return {
success: true,
message: `@${message.chatname} used ${command.commandName} for ${command.cost} points!`,
commandName: command.commandName,
type: 'chat'
};
}
// Method to create a new custom command
async createCustomCommand(commandData) {
// Validate command data
if (!commandData.commandName || !commandData.commandName.startsWith('!')) {
return { success: false, message: "Command name must start with !" };
}
const normalizedName = commandData.commandName.toLowerCase();
// Check if it's trying to override a default command
const existingCommand = this.customCommands.get(normalizedName);
if (existingCommand && existingCommand.isDefault) {
return { success: false, message: "Cannot override default commands" };
}
// Set default values
const command = {
commandName: normalizedName,
cost: commandData.cost || 0,
cooldown: commandData.cooldown || 10000,
description: commandData.description || '',
active: true,
isDefault: false,
isMetaCommand: false,
...commandData
};
// Save the command
await this.saveCommand(command);
return { success: true, message: "Command created", command };
}
// Method to update an existing command
async updateCommand(commandName, updates) {
const normalizedName = commandName.toLowerCase();
const existingCommand = this.customCommands.get(normalizedName);
if (!existingCommand) {
return { success: false, message: "Command not found" };
}
// Don't allow changing core properties of default commands
if (existingCommand.isDefault) {
const protectedProps = ['commandName', 'isDefault', 'isMetaCommand'];
for (const prop of protectedProps) {
if (updates[prop] !== undefined && updates[prop] !== existingCommand[prop]) {
return { success: false, message: `Cannot modify ${prop} of default commands` };
}
}
}
// Apply updates
const updatedCommand = { ...existingCommand, ...updates };
// Save the updated command
await this.saveCommand(updatedCommand);
return { success: true, message: "Command updated", command: updatedCommand };
}
}
// Create and initialize the points actions system
const pointsActions = new PointsActions({
pointsSystem: pointsSystem // Reference to existing points system
});
// Initialize point actions after the points system is ready
async function initializePointsActions() {
try {
await pointsActions.ensureDB();
console.log('Points actions system initialized');
// Hook into message processing to handle commands
const originalAddMessage = messageStoreDB.addMessage;
messageStoreDB.addMessage = async function(message) {
const result = await originalAddMessage.call(this, message);
// Process any points commands
if (message && message.chatmessage && message.chatmessage.startsWith('!')) {
const commandResult = await pointsActions.processCommand(message);
// Handle command response if needed
if (commandResult && commandResult.success && commandResult.message && commandResult.type === 'chat') {
// Create a response message
const responseMessage = {
chatname: 'PointsBot',
chatmessage: commandResult.message,
type: 'bot',
timestamp: Date.now()
};
// Add to message store (which will trigger display)
await originalAddMessage.call(this, responseMessage);
}
}
return result;
};
} catch (error) {
console.error('Failed to initialize points actions system:', error);
}
}
// Start initialization after points system is ready
setTimeout(initializePointsActions, 3000);
// Admin API for managing commands
window.pointsActions = {
// Get all available commands
getCommands: async () => {
return await pointsActions.getCommands();
},
// Create a new command
createCommand: async (commandData) => {
return await pointsActions.createCustomCommand(commandData);
},
// Update an existing command
updateCommand: async (commandName, updates) => {
return await pointsActions.updateCommand(commandName, updates);
},
// Delete a command
deleteCommand: async (commandName) => {
return await pointsActions.deleteCommand(commandName);
},
// Examples of creating different types of commands
createMediaCommand: async (name, cost, mediaUrl, options = {}) => {
return await pointsActions.createCustomCommand({
commandName: name,
cost: cost,
cooldown: options.cooldown || 30000,
description: options.description || `Display media for ${cost} points`,
mediaUrl: mediaUrl,
mediaType: options.mediaType || 'image',
mediaDuration: options.duration || 5000,
mediaStyle: options.style,
mediaPosition: options.position,
successMessage: options.successMessage
});
},
createChatCommand: async (name, cost, responseMessage, options = {}) => {
return await pointsActions.createCustomCommand({
commandName: name,
cost: cost,
cooldown: options.cooldown || 10000,
description: options.description || `Chat response for ${cost} points`,
responseMessage: responseMessage
});
},
createWebhookCommand: async (name, cost, webhookUrl, options = {}) => {
return await pointsActions.createCustomCommand({
commandName: name,
cost: cost,
cooldown: options.cooldown || 60000,
description: options.description || `Trigger webhook for ${cost} points`,
actionType: 'webhook',
webhookUrl: webhookUrl,
successMessage: options.successMessage
});
}
};