[text] mgigmay

Viewer

  1. const tmi = require('tmi.js');
  2. const axios = require('axios');
  3.  
  4. const { channel, username, password } = require('./settings.json');
  5.  
  6. const options = {
  7.   options: { debug: true },
  8.   connection: {
  9.     reconnect: true,
  10.     secure: true,
  11.   },
  12.   identity: {
  13.     username,
  14.     password,
  15.   },
  16.   channels: [channel],
  17. };
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. let odds = 1
  26. //for showing odds at the begging of bot initlaization has no effect on actual odds
  27. let base_odds = 10
  28. daily_odds = (odds*100)
  29. if (odds === 1){
  30.     daily_odds = 0
  31. }
  32.  
  33.  
  34. const client = new tmi.Client(options);
  35.  
  36. // Define a function to fetch emotes from the 7TV API
  37. async function fetchEmotes() {
  38.   try {
  39.     const response = await axios.get('https://api.7tv.app/v2/users/wantep/emotes');
  40.     return response.data.map((emote) => ({
  41.       name: emote.name,
  42.       rarity: assignRarity()
  43.     }));
  44.   } catch (error) {
  45.     console.error(`Failed to fetch emotes: ${error.message}`);
  46.     return [];
  47.   }
  48. }
  49.  
  50.  
  51.  
  52. // Function to assign rarity to emotes
  53. function assignRarity() {
  54.  
  55.   const rand = Math.random();
  56.  
  57.   rarities = [0.6, 0.85, 0.95, 1]
  58.   rarityTitles = ["common", "rare", "epic", "legendary"]
  59.  
  60.   for (let i = 0; i < rarities.length; i++)
  61.   {
  62.       if (rand < rarities[i])
  63.       {
  64.           return rarityTitles[i];
  65.       }
  66.   }
  67. }
  68.  
  69.  
  70.  
  71. let emotes = [];
  72.  
  73. // Fetch emotes from the API and store them in the emotes array
  74. fetchEmotes()
  75.   .then((fetchedEmotes) => {
  76.     emotes = fetchedEmotes;
  77.     console.log(`Fetched ${emotes.length} emotes from the API`);
  78.   })
  79.   .catch((error) => {
  80.     console.error(`Failed to fetch emotes: ${error.message}`);
  81.   });
  82.  
  83. const userTimestamps = {};
  84. const emoteleaderboard = {};
  85.  
  86. setInterval(() => {
  87.   const threshold = 60 * 60 * 1000;
  88.   const now = Date.now();
  89.  
  90.   for (const user in userTimestamps) {
  91.     if (userTimestamps.hasOwnProperty(user)) {
  92.       const timestamp = userTimestamps[user];
  93.  
  94.       if (now - timestamp > threshold) {
  95.         delete userTimestamps[user];
  96.       }
  97.     }
  98.   }
  99. }, 60 * 60 * 1000);
  100.  
  101.  
  102. function updateLeaderboard(username, rarity) {
  103.     if (!emoteleaderboard[username]) {
  104.       emoteleaderboard[username] = 0;
  105.     }
  106.   
  107.     switch (rarity) {
  108.       case 'common':
  109.         emoteleaderboard[username] += 1;
  110.         break;
  111.       case 'rare':
  112.         emoteleaderboard[username] += 5;
  113.         break;
  114.       case 'epic':
  115.         emoteleaderboard[username] += 10;
  116.         break;
  117.       case 'legendary':
  118.         emoteleaderboard[username] += 20;
  119.         break;
  120.     }
  121.   }
  122.  
  123.   // Add a new object to store user points and rods
  124. const userPointsAndRods = {};
  125.  
  126. // Update this function to also update user points based on the emote rarity
  127. function updateLeaderboard(username, rarity) {
  128.     if (!emoteleaderboard[username]) {
  129.       emoteleaderboard[username] = 0;
  130.     }
  131.    
  132.     if (!userPointsAndRods[username]) {
  133.       userPointsAndRods[username] = { points: 0, rod: 1 };
  134.     }
  135.    
  136.     let pointsToAdd;
  137.     switch (rarity) {
  138.       case 'common':
  139.         pointsToAdd = 1;
  140.         break;
  141.       case 'rare':
  142.         pointsToAdd = 5;
  143.         break;
  144.       case 'epic':
  145.         pointsToAdd = 10;
  146.         break;
  147.       case 'legendary':
  148.         pointsToAdd = 20;
  149.         break;
  150.     }
  151.     emoteleaderboard[username] += pointsToAdd;
  152.     userPointsAndRods[username].points += pointsToAdd;
  153.   }
  154.  
  155.   
  156.   function upgradeRod(username) {
  157.     const userData = userPointsAndRods[username];
  158.     if (!userData) {
  159.       userPointsAndRods[username] = { points: 1, rod: 1 };
  160.       return;
  161.     }
  162.    
  163.     const rodCost = userData.rod * 5
  164.     
  165.     
  166.     ;
  167.    
  168.     if (userData.points >= rodCost) {
  169.       userData.points -= rodCost;
  170.       userData.rod += 1;
  171.       return `Upgraded your rod to level ${userData.rod}! Your new balance is ${userData.points} points.`;
  172.     } else {
  173.       return `You need ${rodCost} points to upgrade your rod. You currently have ${userData.points} points.`;
  174.     }
  175.   }
  176.    
  177.   function didYouCatch(){
  178.     const rodLevel = userPointsAndRods[username]?.rod || 1;
  179.     const catchRateMultiplier = 1 + (rodLevel - 1) * 0.1;
  180.     const rand = Math.random();
  181.  
  182.     return (rand < 0.1 * odds * catchRateMultiplier)
  183.  
  184.   }
  185.   
  186.   function catchEmote(emote, username) {
  187.  
  188.     if(didYouCatch()===true){
  189.       const rand = Math.random()
  190.       const rodLevel = userPointsAndRods[username]?.rod || 1;
  191.       const catchRateMultiplier = 1 + (rodLevel - 1) * 0.1;
  192.  
  193.       if (assignRarity() ==="common"){
  194.         return true
  195.       }
  196.       else if (assignRarity() === "rare"){
  197.         return true
  198.       }
  199.       else if (assignRarity() === "epic"){
  200.  
  201.       } else {
  202.         return true
  203.       }
  204.    
  205.     }
  206.     
  207.     
  208.    
  209.     
  210.     
  211.    /* switch (emote.rarity) {
  212.       case 'common':
  213.         return rand < 0.06 * odds * catchRateMultiplier;
  214.       case 'rare':
  215.         return rand < 0.025 * odds * catchRateMultiplier;
  216.       case 'epic':
  217.         return rand < 0.013 * odds * catchRateMultiplier;
  218.       case 'legendary':
  219.         return rand < 0.002 * odds * catchRateMultiplier;
  220.       default:
  221.         return false;
  222.     }
  223.     */
  224.   }
  225.  
  226.   function isBroadcasterOrMod(user) {
  227.     return user.badges?.broadcaster === '1' || user.mod;
  228.   }
  229.    
  230.   function setNewOdds(odds2) {
  231.     if (odds2 >= 0 && odds2 <= 100) {
  232.       return true;
  233.     } else {
  234.       return false;
  235.     }
  236.   }
  237.  
  238. function displayLeaderboard(channel) {
  239.   const sortedLeaderboard = Object.entries(emoteleaderboard)
  240.     .sort((a, b) => b[1] - a[1])
  241.     .slice(0, 10);
  242.  
  243.   let leaderboardMessage = '🎣 Fishing Leaderboard 🎣\n';
  244.  
  245.   sortedLeaderboard.forEach(([username, score], index) => {
  246.     leaderboardMessage += `${index + 1}. ${username} - ${score} points\n`;
  247.   });
  248.  
  249.   client.say(channel, leaderboardMessage);
  250. }
  251.  
  252. client.connect().catch(console.error);
  253. client.on('connected', () => {
  254.  
  255.   client.say(channel, `Fishing has BEGUN! The chances today are increased by ${daily_odds}% chance, now its ${odds*base_odds}% to catch a fish 'type fishing to fish :o`);
  256. });
  257.  
  258.  
  259.  
  260.   
  261.  
  262. client.on('message', (channel, user, message, self) => {
  263.  
  264.   if (self) return;
  265.   const lowerCaseMessage = message.toLowerCase();
  266.   if (message.toLowerCase().startsWith('fishing')) {
  267.     const currentTime = Date.now();
  268.     const lastTimestamp = userTimestamps[user.username];
  269.  
  270.     if (!lastTimestamp || currentTime - lastTimestamp > 100) {
  271.       userTimestamps[user.username] = currentTime;
  272.         
  273.       const randFakeChance = Math.floor(Math.random()*10)+ 1;
  274.       const emoterange = Math.floor(Math.random() * emotes.length);
  275.       const selectedEmote = emotes[emoterange];
  276.  
  277.  
  278.       if (catchEmote(selectedEmote)) {
  279.         updateLeaderboard(user.username, selectedEmote.rarity);
  280.         client.say(channel, `@${user.username}, WOHOOOO YOU CAUGHT A ${selectedEmote.rarity.toUpperCase()} EMOTE! HERE IT IS --> ${selectedEmote.name}`);
  281.       } else {
  282.         client.say(channel, `@${user.username}, No luck... (${randFakeChance} cm. away)`);
  283.       }
  284.     } else {
  285.       client.say(channel, `@${user.username}, please wait a bit longer before triggering the command again.`);
  286.     }
  287.   } else if (message.toLowerCase().startsWith('!emoteleaderboard')) {
  288.     displayLeaderboard(channel);
  289.   } else if (message.toLowerCase().startsWith('!upgraderod')) {
  290.     const upgradeResult = upgradeRod(user.username);
  291.     client.say(channel, `@${user.username}, ${upgradeResult}`);
  292.   } else if (lowerCaseMessage.startsWith('!setodds')) {
  293.     if (isBroadcasterOrMod(user)) {
  294.       // Parse the command and set the new odds
  295.       const [command, enteredOdds] = lowerCaseMessage.split(' ');
  296.  
  297.       // Validate and convert the odds to numbers
  298.       const newEnteredOdds = parseFloat(enteredOdds);
  299.       console.log("newodds",newEnteredOdds)
  300.       if (setNewOdds(newEnteredOdds)) {
  301.         odds = newEnteredOdds;
  302.         client.say(
  303.           channel,
  304.           `@${user.username}, the odds have been updated.`
  305.         );
  306.       } else {
  307.         client.say(
  308.           channel,
  309.           `@${user.username}, failed to update odds. Make sure the odds are between 0 and 100.`
  310.         );
  311.       }
  312.     } else {
  313.       client.say(channel, `@${user.username}, you don't have permission to change the odds.`);
  314.     }
  315.   }
  316.  
  317.   console.log("odds",odds)
  318.   
  319.  
  320. });

Editor

You can edit this paste and save as new:


File Description
  • mgigmay
  • Paste Code
  • 24 Mar-2023
  • 8.21 Kb
You can Share it: