MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / handler

Function handler

plugins/trivia.js:9–63  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

7 description: 'Start a trivia game or answer the question',
8 usage: '.trivia [answer]',
9 async handler(sock, message, args, context) {
10 const { chatId, channelInfo } = context;
11 if (args.length === 0) {
12 if (triviaGames[chatId]) {
13 await sock.sendMessage(chatId, {
14 text: 'A trivia game is already in progress!',
15 ...channelInfo
16 }, { quoted: message });
17 return;
18 }
19 try {
20 const response = await axios.get('https://opentdb.com/api.php?amount=1&type=multiple');
21 const questionData = response.data.results[0];
22 triviaGames[chatId] = {
23 question: questionData.question,
24 correctAnswer: questionData.correct_answer,
25 options: [...questionData.incorrect_answers, questionData.correct_answer].sort(),
26 };
27 await sock.sendMessage(chatId, {
28 text: `🎯 *Trivia Time!*\n\n*Question:* ${triviaGames[chatId].question}\n\n*Options:*\n${triviaGames[chatId].options.join('\n')}\n\nUse .trivia <answer> to answer!`,
29 ...channelInfo
30 }, { quoted: message });
31 }
32 catch (error) {
33 await sock.sendMessage(chatId, {
34 text: 'Error fetching trivia question. Try again later.',
35 ...channelInfo
36 }, { quoted: message });
37 }
38 }
39 else {
40 if (!triviaGames[chatId]) {
41 await sock.sendMessage(chatId, {
42 text: 'No trivia game is in progress. Use .trivia to start one!',
43 ...channelInfo
44 }, { quoted: message });
45 return;
46 }
47 const game = triviaGames[chatId];
48 const answer = args.join(' ');
49 if (answer.toLowerCase() === game.correctAnswer.toLowerCase()) {
50 await sock.sendMessage(chatId, {
51 text: `✅ Correct! The answer is *${game.correctAnswer}*`,
52 ...channelInfo
53 }, { quoted: message });
54 }
55 else {
56 await sock.sendMessage(chatId, {
57 text: `❌ Wrong! The correct answer was *${game.correctAnswer}*`,
58 ...channelInfo
59 }, { quoted: message });
60 }
61 delete triviaGames[chatId];
62 }
63 }
64};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected