MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / addParticipantToLottery

Function addParticipantToLottery

lottery/lottery.ts:361–402  ·  view source on GitHub ↗
(lotteryId: number, participant: any)

Source from the content-addressed store, hash-verified

359 // Check current participant count
360 const countStmt = db.prepare(`SELECT COUNT(*) as count FROM lottery_participants WHERE lottery_id = ?`);
361 const countResult = countStmt.get(lotteryId) as { count: number } | undefined;
362 const currentCount = countResult?.count || 0;
363
364 // Check lottery max participants
365 const lotteryStmt = db.prepare(`SELECT max_participants FROM lottery_config WHERE id = ?`);
366 const lottery = lotteryStmt.get(lotteryId) as { max_participants: number } | undefined;
367
368 if (!lottery || currentCount >= lottery.max_participants) {
369 throw new Error("Lottery full or not found");
370 }
371
372 // Check if user already participated
373 const existsStmt = db.prepare(`SELECT 1 FROM lottery_participants WHERE lottery_id = ? AND user_id = ?`);
374 const exists = existsStmt.get(lotteryId, participant.user_id);
375
376 if (exists) {
377 throw new Error("User already participated");
378 }
379
380 // Add participant
381 const insertStmt = db.prepare(`
382 INSERT INTO lottery_participants (lottery_id, user_id, username, first_name, last_name, joined_at)
383 VALUES (?, ?, ?, ?, ?, ?)
384 `);
385 insertStmt.run(lotteryId, String(participant.user_id), participant.username || null, participant.first_name || null, participant.last_name || null, Date.now());
386 });
387
388 transaction();
389 return true;
390 } catch (error) {
391 console.error("Failed to add participant:", error);
392 return false;
393 }
394}
395
396function getLotteryParticipants(lotteryId: number): any[] {
397 if (!db) return [];
398
399 const stmt = db.prepare(`
400 SELECT * FROM lottery_participants
401 WHERE lottery_id = ? ORDER BY joined_at
402 `);
403 return stmt.all(lotteryId);
404}
405

Callers 1

Calls 3

runMethod · 0.80
errorMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected