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

Function performLotteryDraw

lottery/lottery.ts:705–811  ·  view source on GitHub ↗
(client: TelegramClient, lottery: any)

Source from the content-addressed store, hash-verified

703 } catch (error) {
704 console.warn("Failed to delete original lottery message:", error);
705 }
706 }
707
708 const participants = getLotteryParticipants(lottery.id);
709
710 if (participants.length === 0) {
711 await client.sendMessage(lottery.chat_id, {
712 message: `🎊 <b>开奖结果</b>\n\n🏆 <b>活动名称:</b> ${htmlEscape(lottery.title)}\n\n😅 <b>很遗憾,没有用户参与抽奖</b>\n🙏 感谢大家的关注!`,
713 parseMode: "html",
714 });
715
716 // Mark lottery as completed
717 const stmt = db.prepare(`UPDATE lottery_config SET status = 'completed' WHERE id = ?`);
718 stmt.run(lottery.id);
719 return;
720 }
721
722 const winnerCount = Math.min(lottery.winner_count, participants.length);
723 const shuffled = [...participants];
724
725 // Fisher-Yates shuffle
726 for (let i = shuffled.length - 1; i > 0; i--) {
727 const j = Math.floor(Math.random() * (i + 1));
728 [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
729 }
730
731 const winners = shuffled.slice(0, winnerCount);
732
733 // Distribute prizes and create winner records
734 await distributePrizes(client, lottery, winners);
735
736 // Generate winner display list with status
737 const winnerLines: string[] = [];
738 const updatedWinners = getLotteryWinners(lottery.id);
739
740 for (const winner of updatedWinners) {
741 const statusIcon = getWinnerStatusIcon(winner.status);
742 let displayName = "";
743 let username = "";
744
745 if (winner.first_name || winner.last_name) {
746 displayName = [winner.first_name, winner.last_name].filter(Boolean).join(" ");
747 }
748 if (winner.username) {
749 username = `@${winner.username}`;
750 }
751
752 let formattedName = "";
753 if (displayName && username) {
754 formattedName = `${htmlEscape(displayName)} ${htmlEscape(username)}`;
755 } else if (displayName) {
756 formattedName = htmlEscape(displayName);
757 } else if (username) {
758 formattedName = htmlEscape(username);
759 } else {
760 formattedName = `用户 ${winner.user_id}`;
761 }
762

Callers 2

lotteryFunction · 0.85

Calls 8

getLotteryParticipantsFunction · 0.85
distributePrizesFunction · 0.85
getLotteryWinnersFunction · 0.85
getWinnerStatusIconFunction · 0.85
warnMethod · 0.80
runMethod · 0.80
errorMethod · 0.80
htmlEscapeFunction · 0.70

Tested by

no test coverage detected