Guest User

Untitled

a guest
Sep 18th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let fieldData, contestants = {}, singleQuestion = {};
  2. const getPoints = (user) => {
  3.     return new Promise(resolve => {
  4.         fetch(`https://api.jebaited.net/getPoints/${fieldData.token}/${user}`).then(response => response.text()).then((data) => {
  5.             resolve(data);
  6.         });
  7.     });
  8. };
  9.  
  10. const addPoints = (user, points) => {
  11.     return new Promise(resolve => {
  12.         fetch(`https://api.jebaited.net/addPoints/${fieldData.token}/${user}/${points}`).then(response => response.text()).then((text) => {
  13.             resolve(true);
  14.         })
  15.     });
  16. };
  17.  
  18. const sayMessage = (message) => {
  19.     message = encodeURIComponent(message).replace("%2F", "%252F");
  20.     return new Promise(resolve => {
  21.         fetch(`https://api.jebaited.net/botMsg/${fieldData.token}/${message}`).then(response => response.text()).then((text) => {
  22.             resolve(true)
  23.         })
  24.     });
  25. };
  26.  
  27. const checkPrivileges = (data) => {
  28.     let required = fieldData.privileges;
  29.     let userState = {
  30.         'mod': parseInt(data.tags.mod),
  31.         'sub': parseInt(data.tags.subscriber),
  32.         'vip': (data.tags.badges.indexOf("vip") !== -1),
  33.         'badges': {
  34.             'broadcaster': (data.userId === data.tags['room-id']),
  35.         }
  36.     };
  37.     if (userState.badges.broadcaster) return true;
  38.     else if (required === "mods" && userState.mod) return true;
  39.     else if (required === "vips" && (userState.mod || userState.vip)) return true;
  40.     else if (required === "subs" && (userState.mod || userState.vip || userState.sub)) return true;
  41.     else if (required === "everybody") return true;
  42.     else return false;
  43. };
  44.  
  45. const shuffleArray = (array) => {
  46.     for (let i = array.length - 1; i > 0; i--) {
  47.         const j = Math.floor(Math.random() * (i + 1));
  48.         [array[i], array[j]] = [array[j], array[i]];
  49.     }
  50. }
  51. const getQuestion = (difficulty) => {
  52.     return new Promise(resolve => {
  53.         let url = `https://opentdb.com/api.php?amount=1&category=${fieldData['category']}&difficulty=${difficulty}&type=multiple&encode=url3986`;
  54.         if (fieldData['category'] === "any") url = `https://opentdb.com/api.php?amount=1&difficulty=${difficulty}&type=multiple&encode=url3986`;
  55.         fetch(url).then(response => response.json()).then(questionData => {
  56.             for (let i in questionData.results[0]) {
  57.                 if (i === "incorrect_answers") {
  58.                     for (let j in questionData.results[0][i]) {
  59.                         questionData.results[0][i][j] = decodeURIComponent(questionData.results[0][i][j]);
  60.                     }
  61.                 } else {
  62.                     questionData.results[0][i] = decodeURIComponent(questionData.results[0][i]);
  63.                 }
  64.             }
  65.             //console.log(`Correct one is `, questionData.results[0]['correct_answer'])
  66.             questionData.results[0]['answers'] = [...questionData.results[0]['incorrect_answers'], ...[questionData.results[0]['correct_answer']]];
  67.             shuffleArray(questionData.results[0]['answers']);
  68.             resolve(questionData.results[0]);
  69.         })
  70.     })
  71. };
  72. const wrapText = (message, data) => {
  73.     let answers = `A : ${data.answers[0]} B : ${data.answers[1]} C : ${data.answers[2]} D : ${data.answers[3]}`;
  74.  
  75.     return message.replace("{name}", data.name)
  76.         .replace("{user}", data.name)
  77.         .replace("{stake}", data.stake)
  78.         .replace("{reward}", data.reward)
  79.         .replace("{amount}", data.reward)
  80.         .replace("{difficulty}", data.difficulty)
  81.         .replace("{question}", data.question)
  82.         .replace("{answers}", answers)
  83.         .replace("{answer}", data.correct_answer);
  84. };
  85.  
  86. window.addEventListener('onEventReceived', function (obj) {
  87.     if (obj.detail.listener !== "message") return;
  88.     const data = obj.detail.event.data;
  89.     const user = data["displayName"];
  90.     const message = data["text"];
  91.     if (message.startsWith(fieldData.questionCommand)) {
  92.         const params = message.split(" ");
  93.  
  94.         let difficulty = params[1];
  95.         if (difficulty !== "hard" && difficulty !== "medium") difficulty = "easy";
  96.         let stake = parseInt(params[2]);
  97.         if (!stake) stake = 100;
  98.         if (!stake > fieldData.maxBet) stake = fieldData.maxBet;
  99.         if (fieldData.mode === "singleuser") {
  100.             if (typeof contestants[user] !== "undefined") return;
  101.             getPoints(user).then((userPoints) => {
  102.  
  103.  
  104.                 if (parseInt(userPoints) > stake) {
  105.                     getQuestion(difficulty).then((questionData) => {
  106.                             questionData['name'] = user;
  107.                             questionData['stake'] = stake;
  108.                             questionData['reward'] = parseInt(stake * fieldData[`${difficulty}Multiplier`]);
  109.                             sayMessage(wrapText(fieldData['questionText'], questionData)).then(() => {
  110.                                 contestants[user] = questionData;
  111.                                 contestants[user]['timeout'] = setTimeout(() => {
  112.                                     sayMessage(wrapText(fieldData['wrongAnswerText'], questionData)).then(() => {
  113.                                         addPoints(user, -stake).then(() => {
  114.                                             delete contestants[user];
  115.                                         });
  116.  
  117.                                     });
  118.                                 }, fieldData['timeout'] * 1000)
  119.                             })
  120.                         }
  121.                     );
  122.                 }
  123.             });
  124.         } else {
  125.             if (!checkPrivileges(data)) {
  126.                 return;
  127.             }
  128.             if (typeof singleQuestion['answers'] !== "undefined") return;
  129.  
  130.             singleQuestion['answers'] = []; //placeholder
  131.             getQuestion(difficulty).then((questionData) => {
  132.                 questionData['name'] = user;
  133.                 questionData['stake'] = stake;
  134.                 questionData['reward'] = parseInt(stake * fieldData[`${difficulty}Multiplier`]);
  135.  
  136.                 sayMessage(wrapText(fieldData['questionText'], questionData)).then(() => {
  137.                     singleQuestion = Object.assign({}, questionData);
  138.                     contestants = {};
  139.                     singleQuestion['timeout'] = setTimeout(() => {
  140.                         sayMessage(wrapText(fieldData['wrongAnswerText'], questionData));
  141.                         singleQuestion = {};
  142.                     }, fieldData['timeout'] * 1000)
  143.                 })
  144.             });
  145.         }
  146.  
  147.     } else if (message.startsWith(fieldData.answerCommand)) {
  148.         let answer = message.replace(`${fieldData.answerCommand} `, "");
  149.  
  150.         if (fieldData.mode === "singleuser") {
  151.             if (fieldData.acceptAnswers === "letter") {
  152.                 if (answer.toLowerCase() === "a") answer = contestants[user]['answers'][0]
  153.                 if (answer.toLowerCase() === "b") answer = contestants[user]['answers'][1]
  154.                 if (answer.toLowerCase() === "c") answer = contestants[user]['answers'][2]
  155.                 if (answer.toLowerCase() === "d") answer = contestants[user]['answers'][3]
  156.             }
  157.             if (typeof contestants[user] === "undefined") return;
  158.  
  159.             clearTimeout(contestants[user]['timeout']);
  160.             if (contestants[user]['correct_answer'].toLowerCase() === answer.toLowerCase()) {
  161.                 sayMessage(wrapText(fieldData['correctAnswerText'], contestants[user])).then(() => {
  162.                     addPoints(user, contestants[user]['reward'] - contestants[user]['stake']).then(() => {
  163.                         delete contestants[user];
  164.                     });
  165.                 })
  166.             } else {
  167.                 sayMessage(wrapText(fieldData['wrongAnswerText'], contestants[user])).then(() => {
  168.                     addPoints(user, -contestants[user]['stake']).then(() => {
  169.                         delete contestants[user];
  170.                     });
  171.                 })
  172.             }
  173.         } else {
  174.             if (typeof contestants[user] !== "undefined") return;
  175.             contestants[user] = 1;
  176.             if (fieldData.acceptAnswers === "letter") {
  177.                 if (answer.toLowerCase() === "a") answer = singleQuestion['answers'][0]
  178.                 if (answer.toLowerCase() === "b") answer = singleQuestion['answers'][1]
  179.                 if (answer.toLowerCase() === "c") answer = singleQuestion['answers'][2]
  180.                 if (answer.toLowerCase() === "d") answer = singleQuestion['answers'][3]
  181.             }
  182.  
  183.  
  184.             if (typeof singleQuestion['answers'] !== "undefined") {
  185.                 if (singleQuestion['correct_answer'].toLowerCase() === answer.toLowerCase()) {
  186.                     //console.log(`User: `, user, singleQuestion);
  187.                     clearTimeout(singleQuestion['timeout']);
  188.                     singleQuestion['name'] = user;
  189.                     let questionData = Object.assign({}, singleQuestion);
  190.                     singleQuestion = {};
  191.                     contestants = {};
  192.                     sayMessage(wrapText(fieldData['correctAnswerText'], questionData)).then(() => {
  193.                         //console.log(singleQuestion);
  194.                         addPoints(user, questionData['reward']).then(() => {
  195.                         });
  196.                     })
  197.  
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.  
  204. });
  205.  
  206. window.addEventListener('onWidgetLoad', function (obj) {
  207.     fieldData = obj.detail.fieldData;
  208. });
  209.  
Advertisement
Add Comment
Please, Sign In to add comment